如何在 Jenkins 中使用 Maven 從多個測試套件執行特定的 TestNG 套件?


TestNG 是一個測試框架,可以使用 Maven 作為構建工具。它有助於在一個地方(pom.xml)維護依賴項及其版本。

Maven 提供了使用 surefire 外掛執行的靈活性。如果使用者有多個 testng.xml 檔案(請注意,一個 testng 檔案只包含一個測試套件),他/她可以根據需要執行特定的套件。Maven 提供了將 suiteXMLFiles 定義為變數並在執行時使用命令列傳遞變數值的靈活性。

Jenkins 是一個 CI/CD 工具,它也提供相同的靈活性,讓我們可以從多個套件中執行特定的 testng.xml。

在本教程中,我們將說明如何在 Jenkins 中使用 maven 執行特定的 TestNG 套件。

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

  • 步驟 1:建立 TestNG 類 - NewTestngClass

  • 步驟 2:在類中編寫 2 個 @Test 方法。

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

  • 步驟 4:在 pom.xml 中新增如下所示的屬性。

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <suiteFile></suiteFile>
</properties>
  • 步驟 5:在 pom.xml 中新增 suiteXMLFiles 標籤,併為 testng.xml 檔案路徑指定一個變數名 ${ suiteFile},如程式部分所示。它應該與屬性中提到的相同。

  • 步驟 6:現在,使用 Jenkins 設定構建,然後轉到構建 -> 目標和選項。

  • 步驟 7:傳遞以下值:

    clean test -DsuiteFile=src/test/main/testng.xml 它將執行提到的 testng.xml。

示例

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

import org.testng.annotations.Test;

public class NewTestngClass {

    @Test()
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
   @Test(groups = { "group2", "group3" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
	   
    } 
}  

pom.xml

這是一個 Maven 配置檔案,用於組織依賴項、外掛和執行 TestNG 測試用例。

當需要執行有限的測試而不是完整套件時,它非常方便。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>TestNGProjectct</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                  <suiteXmlFiles>                        <suiteXmlFile>${suiteFile}</suiteXmlFile>
                    </suiteXmlFiles>  
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <suiteFile></suiteFile>
    </properties>
    <dependencies>          
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
    </dependencies>
</project>

執行特定套件的命令

請注意,如果需要排除多個套件,只需用逗號分隔所有套件名稱即可。

clean test -DsuiteFile=src/main/java/testng.xml

輸出

[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...

in test case 1 of NewTestngClass 
in test case 2 of NewTestngClass 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.314 s 

更新於:2023-08-18

710 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告