如何在 TestNG.xml 中的類路徑中使用正則表示式?
testNG.xml 具有 <classes> 格式,我們在此定義應執行的所有測試類。在 <classes> 中的類中沒有提供正則表示式的特定方法。但是,當然有一些解決方法,如果您想從類中執行特定的 @Test,這些方法非常有用。TestNG 在 include、exclude 和 package 標籤中支援正則表示式。
以下是在要從測試套件執行的測試類中使用正則表示式的幾種方法。
在 <classes> 內提及所有類名。並且,在類內部,使用 <methods> 和 <include name ="test.*" />。它將排除從給定類中以 test 為名稱開始的所有測試。
如果類存在於巢狀或不同的包中,則在 <packages><package name="common_package.*"> 中使用包名稱直到公共名稱,以及正則表示式。它將執行所有存在於以 common_package 開頭的包名稱內的類。
現在,讓我們詳細討論這兩種場景。
場景 1
使用正則表示式在類中執行特定方法。在這裡,我們將擁有一個包含多個測試方法的類,我們將看到如何配置 testNG.xml 以僅基於正則表示式執行一些測試。我們將執行所有以“test”開頭的 @Test。
解決此問題的方法/演算法 -
步驟 1 - 建立一個名為“NewTestngClass”的 TestNG 類。
步驟 2 - 在類中編寫三個不同的 @Test 方法,起始名稱為 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 下建立兩個包,分別為 com.test 和 com.work
步驟 2 - 建立兩個 TestNG 類:NewTestngClass 在 com.test 包內,OrderofTestExecutionInTestNG 在 com.work 包內
步驟 3 - 在這兩個類中編寫兩個不同的 @Test 方法,起始名稱為 test1 和 test2。
步驟 4 - 現在建立如下所示的 testNG.xml。
步驟 5 - 執行 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 =======================
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP