1. 程式人生 > 實用技巧 >【testNG學習】testng.xml檔案

【testNG學習】testng.xml檔案

簡單介紹

執行TestNG測試指令碼有兩種方式:一種是直接通過IDE執行(例如使用eclipse中的“Run TestNG tests”),另一種是從命令列執行(通過使用xml配置檔案)。當我們想執行某個包或者某個類中的一部分測試指令碼的時候,使用xml配置檔案是非常便利的。在xml配置檔案裡,不僅可以選擇某些需要執行的測試指令碼,還可以排除某些不需要執行的測試指令碼。

建立testng.xml檔案

建立xml檔案很容易,只需要在其中填充一些內容。

1)首先要宣告一個suite的名字,用於描述將要執行的測試指令碼集,可以根據自己需要任意命名,最終這個名字會在testng的測試報告中看到。

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteName" verbose="1" >  
    <test name="TestName" >

2)如果選擇的測試指令碼是基於組的(使用了@Test (groups={"student"})這樣的註解),那麼接下來需要宣告如何使用這些組:包含或者排除。如果使用了include標籤標註某些組,那麼在選擇的測試指令碼中,只有屬於那些組的測試指令碼會被執行。那些未被選中的測試指令碼,或者被選中卻不屬於某些組的測試指令碼都不會被執行。需要注意,一個測試指令碼可以屬於很多個組,只要有一個組被include標籤標註,那麼它就會被執行。如果使用了exclude標籤標註某些組,那麼在選擇的指令碼中,只有不屬於那些組的測試指令碼會被執行。如果同時使用了include標籤和exclude標籤,那麼擁有被include標註的組的那些指令碼會被執行,擁有被exclude標註的指令碼不會被執行。有一個例外是,一個組同時被include和exclude標註了,那麼擁有這個組的指令碼會被執行。include和exclude標籤的使用方式如下:

<groups>
  <run>
     <include name = "includedGroupName" />
     <exclude name = "excludedGroupName" />
  </run>
</groups>

3)選擇測試指令碼可以從包、類、方法三個層級進行。

選擇一個包

<packages>
    <package name = "packageName" />
</packages>

選擇一個類

<classes>
    <class name = "className" />
</classes>

選擇一個方法

<classes>
    <class name = "className" />
       <methods>
          <include name = "methodName" />
       </methods>
    </class>
</classes>

xml檔案樣例

1)選擇一個包中的全部測試指令碼(包含子包)

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="First suite" verbose="1" >
<test name = "allTestsInAPackage" >
   <packages>
      <package name = "whole.path.to.package.* />
   </packages>
</test>
</suite>

2)選擇一個類中的全部測試指令碼

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Second suite" verbose="1" >
<test name = "allTestsInAClass" >
   <classes>
  <class name="whole.path.to.package.className />
   </classes>
</test>
</suite>

3)選擇一個類中的部分測試指令碼

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Third suite" verbose="1" >
<test name = "aFewTestsFromAClass" >
   <classes>
  <class name="whole.path.to.package.className >
      <methods>
         <include name = "firstMethod" />
         <include name = "secondMethod" />
         <include name = "thirdMethod" />
      </methods>
  </class>
   </classes>
</test>
</suite>

4)選擇一個包中的某些組

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Fourth suite" verbose="1" >
<test name = "includedGroupsInAPackage" >
   <groups>
      <run>
         <include name = "includedGroup" />
      </run>
   </groups>
   <packages>
      <package name = "whole.path.to.package.* />
   </packages>
</test>
</suite>

5)排除一個包中的某些組

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Fifth suite" verbose="1" >
<test name = "excludedGroupsInAPackage" >
   <groups>
      <run>
         <exclude name = "excludedGroup" />
      </run>
   </groups>
   <packages>
      <package name = "whole.path.to.package.* />
   </packages>
</test>
</suite>

在maven的pom.xml檔案中配置testng.xml

需要在pom檔案中,指明testng.xml檔案的位置。

maven使用surefire這個外掛進行測試,可以執行testng或者Junit指令碼。

語法為<suiteXmlFile>src/test/resources/testNGFilesFolder/${testNgFileName}.xml</suiteXmlFile>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>//該檔案位於工程根目錄時,直接填寫名字,其它位置要加上路徑。
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

執行測試指令碼

方法一:在IDE,例如IntellJ IDEA中,滑鼠右擊testng.xml檔案,選擇run即可。

方法二:進入到專案工程的根目錄,使用mvn clean test -Dtestng.xml命令,結果如下:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ArtifactIdWang 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
.
.
.
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
now start test
Test started runningtemplistener1at:1487125608088
i'm listenerTest1
Result success
Test started runningtemplistener2at:1487125608660
i'm listenerTest2
Result success
now finish test
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.125 sec - in TestSuite

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:13 min
[INFO] Finished at: 2017-02-15T10:26:51+08:00
[INFO] Final Memory: 22M/182M
[INFO] ------------------------------------------------------------------------
原文:https://www.cnblogs.com/wangyinghao/p/10133109.html