# 3.xml方式運行
TestNG也可以以xml的方式運行
```
<suite> 套件,根標簽,通常由幾個<test組成>
屬性:
name 套件的名稱,必須屬性;
verbose 運行的級別或詳細程度;
parallel 是否運行多線程來運行這個套件;
thread-count 如果啟用多線程,用于指定開戶的線程數;
annotations 在測試中使用的注釋類型;
time-out 在本測試中的所有測試方法上使用的默認超時時間;
<test> 測試用例,name為必須屬性;
<classes> 用例中包含的類,子標簽為<class name=”className”>;
<class> 測試類,其中屬性name為必須屬性;;
<packages> 用例中包含的包,包中所有的方法都會執行,子標簽為<package name=”packageName”>;
<package> 測試包,name為必須屬性;
<methods> 指定測試類中包含或排除的方法,子類為<include>,<exclude>;
<include> 指定需要測試的方法,name為必須屬性;
<exclude> 指定類中不需要測試的方法,name為必須屬性;
<groups> 指定測試用例中要運行或排除運行的分組,子標簽為<run>,<run>下包含<include>,<exclude>標簽,<include>,<exclude>的name指定運行、不運行的分組;
```
在項目下新建一個testng.xml文件,模板如下:
```
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Nopackage" >
<classes>
<class name="NoPackageTest" />
</classes>
</test>
<test name="Regression1">
<classes>
<class name="test.sample.ParameterSample"/>
<class name="test.sample.ParameterTest"/>
</classes>
</test>
</suite>
```

## 3.1?鼠標右擊testng.xml運行
TestTest3的代碼如下:
```
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.*;
public class TestTest3 {
@Test(groups = "group1")
public void testC03() {
System.out.println("testC03");
Assert.assertTrue(true);
}
@Test(groups = "group1")
public void testC04() {
System.out.println("testC04");
Assert.assertTrue(true);
}
@Test(groups = "group2")
public void testC05() {
System.out.println("testC05");
Assert.assertTrue(true);
}
@BeforeTest
public void beforeTest() {
System.out.println("beforeTest");
}
@AfterTest
public void afterTest() {
System.out.println("afterTest");
}
@BeforeClass
public void beforeClass() {
System.out.println("beforeClass");
}
@AfterClass
public void afterClass() {
System.out.println("afterClass");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("beforeSuite");
}
@AfterSuite
public void afterSuite() {
System.out.println("afterSuite");
}
@BeforeGroups(groups = {"group1", "group2"})
public void beforeGroups() {
System.out.println("beforeGroups");
}
@AfterGroups(groups = {"group1", "group2"})
public void afterGroups() {
System.out.println("afterGroups");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("beforeMethod");
}
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod");
}
}
```
TestTest4的代碼如下:
```
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.*;
public class TestTest4 {
@Test(groups = "group1")
public void testD03() {
System.out.println("testD03");
Assert.assertTrue(true);
}
@Test(groups = "group1")
public void testD04() {
System.out.println("testD04");
Assert.assertTrue(true);
}
@Test(groups = "group2")
public void testD05() {
System.out.println("testD05");
Assert.assertTrue(true);
}
@BeforeTest
public void beforeTest() {
System.out.println("beforeTest");
}
@AfterTest
public void afterTest() {
System.out.println("afterTest");
}
@BeforeClass
public void beforeClass() {
System.out.println("beforeClass");
}
@AfterClass
public void afterClass() {
System.out.println("afterClass");
}
@BeforeGroups(groups = "group1")
public void beforeGroups() {
System.out.println("beforeGroups");
}
@AfterGroups(groups = "group1")
public void afterGroups() {
System.out.println("afterGroups");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("beforeMethod");
}
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod");
}
}
```
修改testng.xml的內容如下:
```
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="none">
<test name="Test">
<!-- <groups>-->
<!-- <run>-->
<!-- <include name="group1"/>-->
<!-- </run>-->
<!-- </groups>-->
<classes>
<class name="TestTest3"/>
</classes>
</test> <!-- Test -->
<test name="Test2">
<classes>
<class name="TestTest4"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
```
鼠標右鍵testng.xml文件,運行結果如下:
```
[TestNG] Running:
C:\Users\HP\IdeaProjects\MavenTest\testng.xml
beforeSuite
beforeTest
beforeClass
beforeGroups
beforeMethod
testC03
afterMethod
beforeMethod
testC04
afterMethod
afterGroups
beforeGroups
beforeMethod
testC05
afterMethod
afterGroups
afterClass
afterTest
beforeTest
beforeClass
beforeGroups
beforeMethod
testD03
afterMethod
beforeMethod
testD04
afterMethod
afterGroups
beforeMethod
testD05
afterMethod
afterClass
afterTest
afterSuite
===============================================
Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
```
## 3.2 使用maven運行
需要在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>
```
**運行測試腳本**
進入到項目工程的根目錄,使用?mvn clean test -Dtestng.xml?命令。