如何使用 TestNG.xml 從大型測試套件中執行單個測試?
testNG.xml 非常靈活,可以作為執行測試用例的測試框架檔案。它將開發和執行彼此分離。使用者可以在 testNG 中開發“N”個測試用例,但可以根據 testNG.xml 中的配置執行有限數量的測試方法。
在本文中,讓我們看看如何從大型 TestNG 套件中只執行一個測試方法。
要只執行一個測試方法,我們將使用 TestNG 中的'include'關鍵字。在 testNG.xml 中,首先我們將定義包含該方法的類名,然後在使用'include'關鍵字的同時提及要執行的測試名稱。
解決此問題的方法/演算法 -
步驟 1 - 在類中匯入 org.testng.annotations.* 用於 TestNG。
步驟 2 - 寫一個 @test 註釋
步驟 3 - 為 @test 註釋建立一個方法,例如test1。
步驟 4 - 對test2和test3重複上述步驟。
步驟 5 - 同樣,建立“n”個類並新增多個測試。在本例中,我們將建立 2 個類,每個類包含 2 個測試方法。
步驟 6 - 現在建立如下所示的 testNG.xml。
步驟 7 - 執行 testNG.xml 或直接在 IDE 中執行 testNG 類,或者使用命令列編譯並執行它。
示例
以下程式碼顯示瞭如何從大型套件中只執行 1 個測試方法 -
src/ NewTestngClass.java
import org.testng.annotations.Test;
public class NewTestngClass {
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
@Test
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
}
}src/OrderofTestExecutionInTestNG.java
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
// test case 1
@Test
public void testCase3() {
System.out.println("in test case 3 of OrderofTestExecutionInTestNG");
}
// test case 2
@Test
public void testCase4() {
System.out.println("in test case 4 of OrderofTestExecutionInTestNG");
}
}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 = "NewTestngClass"> <methods> <include name="testCase1" /> </methods> </class> </classes> </test> </suite>
輸出
in test case 1 of NewTestngClass =============================================== Suite1 Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP