多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 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> ``` ![](https://img-blog.csdnimg.cn/20200616152540397.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xvdmVkaW5nZA==,size_16,color_FFFFFF,t_70) ## 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 命令。