如何使用testng.xml從測試套件中排除一個測試類?


testng.xml 的格式為 <classes>,我們在這裡定義所有應該執行的測試類。在 <classes> 中沒有專門排除類的方法,但是如果不想在測試套件中執行特定的類,有一些非常有用的變通方法。

以下是從測試套件中排除測試類執行的一些便捷方法。

  • 像往常一樣,只需提及需要執行的類名,並刪除不需要執行的類名。

  • <classes> 內提及所有類名,包括不應該執行的類名。並且,在應該排除的類內,使用 <methods><exclude name =.* />。這將排除此類中的所有測試,並執行所有其他類。

  • 如果類存在於巢狀的或不同的包中,則在 <packages><package name=common_package><exclude name= common_package.exclude_package_name /> 中使用包名稱直到公共名稱。這將排除包中提到的類名。但是,如果同一個包中有多個檔案,則上述方法很有用。

在本文中,我們將討論如何實現最後兩種方法。

場景1

排除類中的所有方法以從套件中排除類執行。這裡,我們將有兩個包含多個測試方法的類,我們將看到如何配置 testng.xml 以僅執行一個類“NewTestngClass”並排除另一個類。

解決此問題的方法/演算法

  • 步驟1 - 建立兩個 TestNG 類:NewTestngClassOrderofTestExecutionInTestNG

  • 步驟2 - 在這兩個類中編寫兩種不同的 @Test 方法。

  • 步驟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");
   }
}

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" />
         <class name = "OrderofTestExecutionInTestNG" >
            <methods>
               <exclude name=".*" />
            </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

如果類存在於巢狀的或不同的包中,則使用標籤 <packages> 從測試套件中排除測試類。

包名應採用 common_packages.excluding_class_package 的格式。這裡,我們將有兩個包含多個測試方法的類,我們將看到如何配置 testng.xml 以僅執行一個類“NewTestngClass”並排除另一個類。

解決此問題的方法/演算法:

  • 步驟1 - 在 src 下建立一個名為 com.test 的公共包。

  • 步驟2 - 在 com.test 內建立另一個名為 exclude.class 的包。

  • 步驟3 - 建立兩個 TestNG 類:NewTestngClasscom.test 包中,OrderofTestExecutionInTestNGexclude.class 包中。

  • 步驟4 - 在這兩個類中編寫兩種不同的 @Test 方法:NewTestngClassOrderofTestExecutionInTestNG

  • 步驟5 - 現在建立如下所示的 testng.xml

  • 步驟6 - 執行 testng.xml 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。

示例

以下程式碼顯示瞭如何從大型套件中僅執行 1 個測試方法:

src/ com.test.NewTestngClass.java

package com.test;
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.test.exclude.class.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.test.*">
            <exclude name="com.test.exclude.class" />
         </package>
      </packages>
   </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
===============================================

更新於:2022年3月9日

5K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告