TestNG 中測試的執行順序是什麼?
一個 TestNG 類可以包含不同的測試,例如 test1、test2、test3 等。當用戶執行包含各種測試的 TestNG 類時,它會根據提供的名稱按字母順序執行測試用例。但是,使用者可以為這些測試分配優先順序,以便這些測試可以按照使用者的優先順序執行。優先順序從 0 開始(最高優先順序),隨著我們移動到 1、2、3 等,逐漸降低。
預設順序
TestNG 按字母順序執行不同的測試。預設情況下,test1 將首先執行,然後是 test2,最後是 test3。預設情況下,如果使用者未定義優先順序,TestNG 會為所有測試分配優先順序 0。由於所有測試都具有相同的優先順序,因此它會按字母順序執行。
解決此問題的方法/演算法
步驟 1 − 匯入 org.testng.annotations.Test 用於 TestNG。
步驟 2 − 編寫註釋為 @test
步驟 3 − 對 test2 和 test3 重複這些步驟。
步驟 4 − 對 test2 和 test3 重複這些步驟。
步驟 5 − 現在建立如下所示的 testng.xml。
步驟 6 − 現在,執行 testng.xml 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。
示例
以下程式碼建立一個 TestNG 類並顯示其預設執行順序:
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { @Test public void test1() { System.out.println("Starting execution of TEST1"); } @Test public void test2() { System.out.println("Starting execution of TEST2"); } @Test public void test3() { System.out.println("Starting execution of TEST3"); } }
testng.xml
這是一個配置檔案,用於組織和執行 TestNG 測試用例。當只需要執行有限的測試而不是完整的套件時,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
輸出
Starting execution of TEST1 Starting execution of TEST2 Starting execution of TEST3
廣告