如何透過 Maven 排除特定的 TestNG 測試組?


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

Maven 提供了在執行時包含或排除測試組的靈活性。使用者可以使用 surefire 外掛在執行時排除測試組或多個測試組。

在本文中,我們將說明如何在執行時透過 surefire 排除測試組。

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

  • 步驟 1:建立 TestNG 類 - NewTestngClass

  • 步驟 2:在類中編寫 2 個 @Test 方法並指定組名。

  • 步驟 3:現在建立如下所示的 testNG.xml,其中未提及組詳細資訊。

  • 步驟 4:不要在 pom.xml 中的 suiteXMLFiles 後新增需要排除的組名,如程式部分所示。

  • 步驟 5:現在,使用命令列執行它。

示例

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

src/ NewTestngClass.java

import org.testng.annotations.Test;

public class NewTestngClass {

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

    
}  

pom.xml

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

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

如果使用者希望在 pom.xml 中排除組,則他/她必須在 </suiteXmlFiles> 標記後新增 <excludedGroups>group1,group2</excludedGroups>。在這種情況下,使用者無需傳遞命令列引數。

<?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>src/main/java/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>  
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>          
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
    </dependencies>
</project>

執行命令

請注意,如果需要排除多個組,只需用逗號分隔所有組名,例如 -DexcludedGroups=group1,group3

mvn test -DexcludedGroups=group1

輸出

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.sample:TestNGProjectct >---------------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\anandas\IdeaProjects\TestNGProjectct\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct ---
[INFO] No tests to run.
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...
in test case 2 of NewTestngClass

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.461 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.838 s
[INFO] Finished at: 2022-02-08T16:07:17+05:30
[INFO] ------------------------------------------------------------------------

更新於:2023-08-16

270 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告