如何在 testng.xml 中使用萬用字元執行 TestNG 類?
testng.xml 的格式為 <classes>,我們在其中定義要執行的所有測試類。在 <classes> 中的類中沒有提供正則表示式的特定方法。但是,有一些變通方法可用於執行類中的特定 @Test。TestNG 在 include、exclude 和 package 標籤中支援正則表示式。
以下是幾個在測試套件中執行測試類時使用正則表示式的便捷方法。
在 <classes> 中提及所有類名。並在類中使用 <methods> 和 <include name =”test.*” />。它將排除從給定類開始名稱為 test 的所有測試。
如果類存在於巢狀或不同的包中,則在 <packages><package name=”common_package.*”> 中使用包名稱直到公共名稱以及正則表示式。它將執行在以 common_package 開頭的包名稱記憶體在的所有類。
在本文中,我們將討論 #1 和 #2。
場景 1
使用正則表示式在類中執行特定方法。在這裡,我們將擁有一個包含多個測試方法的類,我們將看到如何配置 testng.xml 以僅基於正則表示式執行少量測試。我們將執行所有以“test.”開頭的 @Test。
解決此問題的方法/演算法
步驟 1:建立一個 TestNG 類 - NewTestngClass。
步驟 2:在類中編寫 3 個不同的 @Test 方法 - NewTestngClass,名稱分別為 test1、test2 和 findingAddition。
步驟 3:現在建立如下所示的 testNG.xml。
步驟 4:現在,執行 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"); } @Test public void findingAddition() { System.out.println("in finding addition of NewTestngClass"); } }
testng.xml
這是一個用於組織和執行 TestNG 測試用例的配置檔案。
當只需要執行有限的測試而不是完整套件時,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1" parallel = "none"> <test name = "test1" preserve-order = "true"> <classes> <class name="NewTestngClass"> <methods> <include name="test.*"/> </methods> </class> </classes> </test> </suite>
輸出
in test case 1 of NewTestngClass in test case 2 of NewTestngClass =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
場景 2
使用正則表示式在類中執行特定方法。在這裡,我們將擁有一個包,其中包含多個具有多個測試方法的類,我們將看到如何配置 testng.xml 以僅基於正則表示式執行特定包。我們將執行存在於以 com 開頭的包中的類。
解決此問題的方法/演算法
步驟 1:在 src 下建立 2 個包,分別為 com.test 和 com.work
步驟 2:建立 2 個 TestNG 類 - NewTestngClass 在包 com.test 中,OrderofTestExecutionInTestNG 在包 com.work 中
步驟 3:在兩個類中編寫 2 個不同的 @Test 方法 - 名稱分別為 test1 和 test2。
步驟 3:現在建立如下所示的 testNG.xml。
步驟 4:現在,執行 testNG.xml 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。
示例
以下程式碼演示瞭如何從大型套件中僅執行 1 個測試方法
src/ com.test.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/com.work.OrderofTestExecutionInTestNG.java
package com.test.exclude.class; 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"> <packages> <package name="com.*"> </package> </packages> </test> </suite>
輸出
in test case 1 of NewTestngClass in test case 2 of NewTestngClass in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 =======================