[英]TestNG - Order of Tests execution in selenium script
我正在使用 selenium 3.8.1 和 TestNG 6.9.2 版本,而在完成 @Test 方法之前測試執行另一個 @Test 方法開始,因此我在完成測試用例執行后在 selenium 腳本中遇到錯誤。
一班
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);
}
}
二等艙
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);
}
}
測試文件
<?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="com.tests.day.modules.LoginPage"/>
<class name="com.tests.day.modules.HomePage"/>
</classes>
</test>
</suite>
在完成登錄頁面類的 test2 之前使用 testng.xml 文件執行上述后,test3 正在啟動 HomePage,因此我得到異常,無法找到元素。
注釋中提到了TestNG的preserve-order
屬性如下:
默認情況下,TestNG 將按照在 XML 文件中找到的順序運行您的測試。 如果您希望此文件中列出的類和方法以不可預測的順序運行,請將保留順序屬性設置為 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"); } }
測試文件
<?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 秒,但您仍然可以觀察到實例名稱的差異,例如instance:testng_order_of_tests_execution.HomePage@5419f379
和instance:testng_order_of_tests_execution.LoginPage@735b5592
。 由於我們的測試是單線程測試,因此我們可以得出結論,執行順序是正確的,符合預期。 但是控制台輸出混淆了。
在 testng.xml 的test標簽內使用group-by-instances="true"
定義您的 xml測試標簽,如下所示:
<test name="Test" group-by-instances="true">
或者,您也可以檢查以下代碼行:
<test name="Test" preserve-order="true" group-by-instances="true">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.