@Test
testNG1.java:
import org.testng.annotations.Test; public class testNG1 { @Test public void testNg() { System.out.println("this is testNG1"); } }
testNG2.java:
import org.testng.annotations.Test; public class testNG2 { @Test public void testNg() { System.out.println("this is testNG2"); } }
testNG3.java:
import org.testng.annotations.Test; public class testNG3 { @Test public void testNg() { System.out.println("this is testNG3"); } }
testng.xml:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1" > <classes> <class name="testNG2" /> <class name="testNG1" /> <class name="testNG3" /> </classes> </test> </suite>
运行testng.xml,执行结果:
this is testNG1 this is testNG2 this is testNG3
preserve-order="true"
默认testng.xml是按字典的顺序执行,如果想要按照xml中class的顺序执行,可以在test节点加上preserve-order="true",修改testng.xml如下:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1" preserve-order="true" > <classes> <class name="testNG2" /> <class name="testNG1" /> <class name="testNG3" /> </classes> </test> </suite>
再次运行testng.xml,执行结果:
this is testNG2 this is testNG1 this is testNG3
preserve-order="true"属性同样适用于class中的方法
testNG4.java
import org.testng.annotations.Test; public class testNG4 { @Test public void testNgMethond1() { System.out.println("this is testNgMethond1"); } @Test public void testNgMethond2() { System.out.println("this is testNgMethond2"); } @Test public void testNgMethond3() { System.out.println("this is testNgMethond3"); } }
testng.xml:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1"> <classes> <class name="testNG4" /> <methods> <include name="testNgMethond3"></include> <include name="testNgMethond2"></include> <include name="testNgMethond1"></include> </methods> </classes> </test> </suite>
运行testng.xml,执行结果:
this is testNgMethond1 this is testNgMethond2 this is testNgMethond3
如果想要按照xml中方法的顺序执行,同样在test节点加上preserve-order="true",修改testng.xml如下:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1" preserve-order="true"> <classes> <class name="testNG4" /> <methods> <include name="testNgMethond3"></include> <include name="testNgMethond2"></include> <include name="testNgMethond1"></include> </methods> </classes> </test> </suite>
this is testNgMethond3 this is testNgMethond2 this is testNgMethond1
总之,如果希望xml中的class或者method按照顺序执行的话,就在test节点加上preserve-order="true"