TestNG提供了很多执行方式,下面做简单介绍。
1.XML指明测试类,按照类名执行,其中可以指定包名,也可指定无包名:
带包名,运行ParameterSample类和ParameterTest类
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite1" verbose="1" > <test name="Regression1"> <classes> <class name="test.sample.ParameterSample"/> <class name="test.sample.ParameterTest"/> </classes> </test> </suite>
不带包名,不建议使用
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite1" verbose="1" > <test name="Nopackage" > <classes> <class name="NoPackageTest" /> </classes> </test> </suite>
2.XML指定包名,执行包内的所有测试类
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite1" verbose="1" > <test name="Regression1" > <packages> <package name="test.sample" /> </packages> </test> </suite>
3.XML中指定某个类的某些方法
<test name="Test1"> <classes> <class name="example1.Test1"> <methods> <include name=".*enabledTestMethod.*"/> <exclude name=".*brokenTestMethod.*"/> </methods> </class> </classes> </test>
4.XML指定组,需要在代码里面设定组
代码:
@Test(groups={"g2"}) public void test4() { System.out.println("this is test4"); } @Test(groups={"g1"}) public void test5() { System.out.println("this is test5"); } @Test(groups={"g1"}) public void test10() { System.out.println("this is test10"); }
XML:
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite"> <test name="Test"> <groups> <run> <exclude name="g2" /> <include name="g1" /> </run> </groups> <classes> <class name="test.testng.TestOrder"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
5.排除某个不执行的测试方法,下面的示例中testMethod2包含broken,在xml中排除了包含broken的方法,所以testMehodd2则不会执行。
@Test(groups = {"checkintest", "broken"} ) public void testMethod2() { }
<test name="Simple example"> <groups> <run> <include name="checkintest"/> <exclude name="broken"/> </run> </groups> <classes> <class name="example1.Test1"/> </classes> </test>
也可以使用@Test(enabled=false)来设定。
6.XML指定组中组
代码:
@Test(groups={"g2"}) public void test4() { System.out.println("this is test4"); } @Test(groups={"g1"}) public void test5() { System.out.println("this is test5"); } @Test(groups={"g1","g3"}) public void test10() { System.out.println("this is test10"); }
XML:定义一个name=fun的组,包含g3方法,再定义一个name=all的组,包含以g开头的所有方法
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="true"> <test name="Test"> <groups> <define name="fun"> <include name="g3"/> </define> <define name="all"> <include name="g.*" /> </define> <run> <include name="all" /> </run> </groups> <classes> <class name="test.testng.TestOrder"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
print:
this is test10
this is test5
this is test4