TestNG筆記(三):testng.xml 配置文件

TestNG提供的annotaions用來輔助定義測試類。
TestNG的testng.xml配置文件用來輔助定義執(zhí)行什么樣的測試,即testng.xml更像是一個測試規(guī)劃。
testng.xml配置文件的元數(shù)據(jù)引用http://testng.org/testng-1.0.dtd,其中定義了testng.xml中的元素、屬性和順序等。

1. 運(yùn)行TestNG測試腳本有兩種方式:

  • IDEA 直接運(yùn)行
  • 從命令行運(yùn)行

2. 創(chuàng)建testng.xml文件

IDEA能直接創(chuàng)建testng.xml文件


image.png

3. 也可以手動創(chuàng)建

  1. 首先聲明一個suite的名字,用于描述腳本要運(yùn)行的腳本測試集,可以根據(jù)自己需要命名,最終可以在測試報告中看到自己的命名。

4. testng.xml文件結(jié)構(gòu)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suitename" junit="false" verbose="3" parallel="false" thread-count="5" configfailurepolicy="<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;">skip</span></span>" annotations="javadoc" time-out="10000" skipfailedinvocationcounts="true" data-provider-thread-count="5" object-factory="classname" allow-return-values="true">  <!-- name參數(shù)為必須 -->
    <suite-files>
        <suite-file path="/path/to/suitefile1"></suite-file>  <!-- path參數(shù)為必須 -->
        <suite-file path="/path/to/suitefile2"></suite-file>
    </suite-files>  
    <parameter name="par1" value="value1"></parameter>  <!-- name, value參數(shù)為必須 -->
    <parameter name="par2" value="value2"></parameter>
    <method-selectors>
        <method-selector>
            <selector-class name="classname" priority="1"></selector-class> <!-- name參數(shù)為必須 -->
            <script language="java"></script>  <!-- language參數(shù)為必須 -->
        </method-selector>
    </method-selectors>
    <test name="testename" junit="false" verbose="3" parallel="false" thread-count="5" annotations="javadoc" time-out="10000" enabled="true" skipfailedinvocationcounts="true" preserve-order="true" allow-return-values="true"> <!-- name參數(shù)為必須 -->
        <parameter name="par1" value="value1"></parameter>  <!-- name, value參數(shù)為必須 -->
        <parameter name="par2" value="value2"></parameter>
        <groups>
            <define name="xxx"> <!-- name參數(shù)為必須 -->
                <include name="" description="" invocation-numbers="" />  <!-- name參數(shù)為必須 -->
                <include name="" description="" invocation-numbers="" />
            </define>
            <run>
                <include name="" />  <!-- name參數(shù)為必須 -->
                <exclude name="" />  <!-- name參數(shù)為必須 -->
            </run>
            <dependencies>
                <group name="" depends-on=""></group> <!-- name,depends-on均為參數(shù)為必須 -->
                <group name="" depends-on=""></group>
            </dependencies>
        </groups>
        <classes>
            <class name="classname"> <!-- name參數(shù)為必須 -->
                <methods>
                    <parameter name="par3" value="value3"></parameter>
                    <include name="" description="" invocation-numbers=""></include>
                    <exclude name=""></exclude>
                </methods>
                <methods></methods>
            </class>
        </classes>  
        <packages>
            <package name="" />  <!-- name參數(shù)為必須 -->
            <package name="">
                <include name="" description="" invocation-numbers=""></include>
                <exclude name=""></exclude>
            </package>
        </packages>
        <listeners>
            <listener class-name="classname1" />  <!-- name參數(shù)為必須 -->
            <listener class-name="classname2" />
        </listeners>
    </test>
    <test></test>
</suite> 

5. testng.xml文件節(jié)點屬性說明

suite屬性說明:
@name: suite的名稱,必須參數(shù)
@junit:是否以Junit模式運(yùn)行,可選值(true | false),默認(rèn)"false"
@verbose:命令行信息打印等級,不會影響測試報告輸出內(nèi)容;可選值(1|2|3|4|5)
@parallel:是否多線程并發(fā)運(yùn)行測試;可選值(false | methods | tests | classes | instances),默認(rèn) "false"
@thread-count:當(dāng)為并發(fā)執(zhí)行時的線程池數(shù)量,默認(rèn)為"5"
@configfailurepolicy:一旦Before/After Class/Methods這些方法失敗后,是繼續(xù)執(zhí)行測試還是跳過測試;可選值 (skip | continue),默認(rèn)"skip"
@annotations:獲取注解的位置,如果為"javadoc", 則使用javadoc注解,否則使用jdk注解
@time-out:為具體執(zhí)行單元設(shè)定一個超時時間,具體參照parallel的執(zhí)行單元設(shè)置;單位為毫秒
@skipfailedinvocationcounts:是否跳過失敗的調(diào)用,可選值(true | false),默認(rèn)"false"
@data-provider-thread-count:并發(fā)執(zhí)行時data-provider的線程池數(shù)量,默認(rèn)為"10"
@object-factory:一個實現(xiàn)IObjectFactory接口的類,用來實例測試對象
@allow-return-values:是否允許返回函數(shù)值,可選值(true | false),默認(rèn)"false"
@preserve-order:順序執(zhí)行開關(guān),可選值(true | false) "true"
@group-by-instances:是否按實例分組,可選值(true | false) "false"

test屬性說明:
@name:test的名字,必選參數(shù);測試報告中會有體現(xiàn)
@junit:是否以Junit模式運(yùn)行,可選值(true | false),默認(rèn)"false"
@verbose:命令行信息打印等級,不會影響測試報告輸出內(nèi)容;可選值(1|2|3|4|5)
@parallel:是否多線程并發(fā)運(yùn)行測試;可選值(false | methods | tests | classes | instances),默認(rèn) "false"
@thread-count:當(dāng)為并發(fā)執(zhí)行時的線程池數(shù)量,默認(rèn)為"5"
@annotations:獲取注解的位置,如果為"javadoc", 則使用javadoc注解,否則使用jdk5注解
@time-out:為具體執(zhí)行單元設(shè)定一個超時時間,具體參照parallel的執(zhí)行單元設(shè)置;單位為毫秒
@enabled:設(shè)置當(dāng)前test是否生效,可選值(true | false),默認(rèn)"true"
@skipfailedinvocationcounts:是否跳過失敗的調(diào)用,可選值(true | false),默認(rèn)"false"
@preserve-order:順序執(zhí)行開關(guān),可選值(true | false) "true"
@group-by-instances:是否按實例分組,可選值(true | false) "false"
@allow-return-values:是否允許返回函數(shù)值,可選值(true | false),默認(rèn)"false"

6. 在maven的pom.xml文件配置testng.xml

需要在pom文件中,指明testng.xml文件的位置。
maven使用surefire這個插件進(jìn)行測試,可以執(zhí)行testng或者Junit腳本。
在pom.xml文件中添加

//填寫testng.xml文件的路徑,如果該文件在根目錄下,則直接填寫文件名
<suiteXmlFile>../${testNgFileName}.xml</suiteXmlFile> 
image.png

在IDEA控制臺Terminal輸入

mvn -f pom.xml clean test  -DxmlFileName=testng.xml  //最后輸入你的testng.xml文件名

執(zhí)行結(jié)束之后會在target文件夾中生成surefire-reports文件夾,文件夾內(nèi)找到你給suite配置的name同名的文件夾,里面會包含html和xml兩種格式的報告。


image.png

image.png

參考文檔:
https://www.cnblogs.com/superbaby11/p/6400400.html
https://blog.csdn.net/taiyangdao/article/details/52136857

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容