如何在命令列中執行 TestNG 中的特定測試組?


組測試是 TestNG 中一個新的創新功能,在 JUnit 框架中不存在。它允許您將方法劃分為適當的部分並執行復雜的測試方法分組。

您不僅可以宣告屬於組的方法,還可以指定包含其他組的組。然後,可以呼叫 TestNG 並要求它包含一組特定的組(或正則表示式),同時排除另一組。

組測試在您如何劃分測試方面提供了最大的靈活性。如果您想連續執行兩組不同的測試,則無需重新編譯任何內容。

組是在您的 testNG.xml 檔案中使用<groups>標籤指定的。它可以在<test><suite>標籤下找到。在<suite>標籤中指定的組適用於其下的所有<test>標籤。

TestNG 允許從命令列 (cmd) 執行測試套件,但是為此必須滿足以下先決條件:

  • 所有依賴的 jar 檔案都應該在專案資料夾內可用。它包括testing.jarjcommander.jar和測試用例中使用的任何其他jar檔案。

  • 編譯後儲存 .class檔案的binout資料夾的路徑。

  • 使用 testNG.xml 執行命令,或者僅針對特定組執行特定類。

在本文中,我們將瞭解如何從命令列執行特定組的測試。

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

  • 步驟 1 - 建立一個測試類,其中包含兩個不同的 @Test 方法和 @BeforeMethods,並使用組名稱,如以下程式部分所示。

  • 步驟 2 - 編譯該類。它將在 IntelliJ 中建立 out 資料夾,在 Eclipse 中建立 bin 資料夾。

  • 步驟 3 - 將所有 jar 檔案放在 lib 資料夾中。

  • 步驟 4 - 現在建立如下所示的 testNG.xml。

    • 注意 - testNG.xml 優先於命令列。因此,即使在命令列中提到了組,但在 testNG.xml 中未正確配置,則執行將不會根據組進行。所有執行都將根據 testNG.xml 進行。

  • 步驟 5 - 開啟cmd

  • 步驟 6 - 使用 cd <project_path>導航到專案路徑。

  • 步驟 7 - 使用以下命令執行 testNG.xml

java -cp <path of lib>; <path of out or bin folder> org.testng.TestNG <path of testng>/testng.xml

或者,使用以下命令僅執行具有組的特定類:

java -cp <path of lib>; <path of out or bin folder> org.testng.TestNG -testclass <ClassName> -groups <groupname>

示例

以下程式碼顯示瞭如何從命令列執行特定組的 TestNG 測試:

src/NewTestngClass.java

import org.testng.annotations.*;
import java.lang.reflect.Method;
public class NewTestngClass {
   // test case 1
   @Test(groups={"test1", "test2"})
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   // test case 2
   @Test(groups={"test2"})
   public void testCase2() {
      System.out.println("in test case 2 of NewTestngClass");
   }
   @BeforeMethod(groups={"test1", "test2"})
   public void name() {
      System.out.println("Before Method");
   }
}

src/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">
      <groups>
         <run>
            <include name = "test1" />
         </run>
      </groups>
      <classes>
         <class name = "NewTestngClass" />
      </classes>
   </test>
</suite>

執行命令

1. 使用 testng.xml 執行特定組

java -cp C:\Users\********\IdeaProjects\TestNGProject\lib\*;C:\Users\********\IdeaProjects\TestNGProjectct\out\production\TestNGProject org.testng.TestNG src/testng.xml

或者,

java -cp .\lib\*;.\out\production\TestNGProject org.testng.TestNG src/testng.xml

輸出

BeforeMethod
in test case 1 of NewTestngClass
===============================================
Command line suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

2. 不使用 testng.xml 使用類名執行特定組

java -cp .\lib\*;.\out\production\TestNGProjectct org.testng.TestNG -testclass NewTestngClass -groups "test1"

輸出

BeforeMethod
in test case 1 of NewTestngClass
===============================================
Command line suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

更新於:2022年3月9日

3K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告