1.class執行順序控制---testng.xml之preserve-order
preserve-order:用來控制<test>里面所有<classes>的執行順序。<test>中默認的preserve-order為true,表示<test>下所有<classes>按照順序執行,如:
<span style="font-size:12px;">1. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="suite1"> <test name="test" preserve-order="true"> <classes > <class name="com.pack.ClassTwo"> <span style="white-space:pre;"> </span><class name="com.pack.ClassThree"> <span style="white-space:pre;"> </span><class name="com.pack.ClassOne"> </classes > </test> </suite></span>
執行順序:按照ClassTwo,ClassThree,ClassOne執行
【注】 一個<class>類里面可能存在多個測試方法(被@Test注解的方法),這些方法的執行順序不受preserve-order控制。默認測試方法的執行順序是按照方法名的首字母升序排序執行的。
2.@test執行順序控制
(1) 使用priority指定執行順序(默認值為0),數值越小,越靠前執行,如:
<span style="font-size:12px;">@Test(priority = 0) public void testMethod1() { } @Test(priority = 1) public void testMethod2() { } @Test(priority = 2) public void testMethod3() { }</span>
執行順序:按照testMethod1、testMethod2、testMethod3順序執行
(2) 方法名稱按首字母排序
因為默認執行順序是按照方法名的首字母升序排序執行。那么,有目的地去定制方法名稱,就可以讓方法按照我們要求的順序執行。如:
<span style="font-size:12px;"> @Test public void B() { } @Test public void A() { } @Test public void C() { }</span>
執行順序:按照A、B、C順序執行。
(3) 在xml里面使用<include>指定需要執行的方法和順序,如:
<span style="font-size:12px;"> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="Preserve order test runs"> <test name="test" preserve-order="true"> <classes> <class name="com.pack.ClassOne"> <methods> <include name="B" /> <include name="A" /> </methods> </class> </classes> </test></suite></span>
執行順序:ClassOne執行兩個測試方法,先執行B,然后執行A。