Annotations中提到了TestNG的preserve-order
属性如下:
默认情况下,TestNG 将按照它们在 XML 文件中的顺序运行您的测试。如果您希望此文件中列出的类和方法以不可预知的顺序运行,请将 preserve-order 属性设置为 false
我执行了与您的代码块类似的相同测试testng.xml
,如下所示:
登录页面
package testng_order_of_tests_execution;
import org.testng.annotations.Test;
public class LoginPage
{
@Test(priority=0)
public void test1(){
System.out.println("First Test");
}
@Test(priority=1)
public void test2(){
System.out.println("Second Test");
}
}
主页
package testng_order_of_tests_execution;
import org.testng.annotations.Test;
public class HomePage
{
@Test(priority=0)
public void test3(){
System.out.println("first test");
}
@Test(priority=1)
public void test4(){
System.out.println("second test");
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test" preserve-order="true">
<classes>
<class name="testng_order_of_tests_execution.LoginPage"/>
<class name="testng_order_of_tests_execution.HomePage"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
我在控制台上发现的输出与您的类似,如下所示:
First Test
first test
Second Test
second test
这Console Output
显然给我们的印象是执行顺序是:
test1() -> test3() -> test2() -> test4()
但实际上没有
查看运行套件的结果,您将获得如下图所示的实际执行顺序:
所以很明显,实际的顺序是:
test1() -> test2() -> test3() -> test4()
琐事
您可以通过以下方式进行更细致的观察testng-results.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" ignored="0" total="4" passed="4">
<reporter-output>
</reporter-output>
<suite name="Suite" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
<groups>
</groups>
<test name="Test" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
<class name="testng_order_of_tests_execution.HomePage">
<test-method status="PASS" signature="test3()[pri:0, instance:testng_order_of_tests_execution.HomePage@5419f379]" name="test3" duration-ms="4" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test3 -->
<test-method status="PASS" signature="test4()[pri:1, instance:testng_order_of_tests_execution.HomePage@5419f379]" name="test4" duration-ms="1" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test4 -->
</class> <!-- testng_order_of_tests_execution.HomePage -->
<class name="testng_order_of_tests_execution.LoginPage">
<test-method status="PASS" signature="test1()[pri:0, instance:testng_order_of_tests_execution.LoginPage@735b5592]" name="test1" duration-ms="14" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test1 -->
<test-method status="PASS" signature="test2()[pri:1, instance:testng_order_of_tests_execution.LoginPage@735b5592]" name="test2" duration-ms="2" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test2 -->
</class> <!-- testng_order_of_tests_execution.LoginPage -->
</test> <!-- Test -->
</suite> <!-- Suite -->
</testng-results>
在testng-results.xml中,您将观察到所有测试都从 2017-12-25T12:57:12Z 开始,到2017-12-25T12:57 : 12Z结束。尽管测试执行所用的时间甚至少于 1 秒,但您仍然可以观察到 instancename和的差异。由于我们的测试是单线程测试,因此我们可以得出结论,执行顺序是正确的并且符合预期。但是控制台输出混淆了。instance:testng_order_of_tests_execution.HomePage@5419f379
instance:testng_order_of_tests_execution.LoginPage@735b5592