如何透過Maven執行特定的TestNG測試組?


Maven是一個專案管理和理解工具,它提供了一個完整的構建生命週期框架。使用者幾乎可以立即自動化專案的構建基礎設施,因為Maven使用標準的目錄佈局和預設的構建生命週期。

總而言之,Maven簡化和標準化了專案構建過程。它可以無縫地處理編譯、分發、文件、團隊協作和其他任務。Maven提高了可重用性,並負責處理大多數與構建相關的任務。

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

使用者可以從testng.xml或pom.xml執行測試。要從pom.xml執行測試,使用者需要提及testng.xml的路徑,並需要maven-surefire-plugin來執行。除了testng.xml,Maven還提供了透過Maven或命令列執行特定組的功能。透過命令列執行時,使用者可以在執行時提及組名,而無需更改testng.xml或pom.xml中的任何內容。

在本文中,我們將瞭解如何透過Maven執行特定的TestNG測試組。

場景1

在這個場景中,我們將說明如何在pom.xml中傳遞組名以執行特定的測試組,而無需更改testng.xml中的任何內容。

解決此問題的步驟/演算法

  • 步驟1:建立TestNG類 - NewTestngClass

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

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

  • 步驟4:在pom.xml中新增testng.xml的路徑,如下所示。

<configuration>
     <suiteXmlFiles>
          <suiteXmlFile>Path of testng.xml</suiteXmlFile>
     </suiteXmlFiles>
 </configuration>
  • 在配置部分,提到了testng的路徑。需要記住兩點,第一,此配置應與maven-surefire-plugin一起保留;第二,切勿忘記新增testng的依賴項。詳細的pom.xml可以在程式部分找到。

  • 步驟5:在pom.xml中,在suiteXMLFiles之後新增應執行的組名,如程式部分所示。在這裡,我們將只執行具有group2組名的@Test。

  • 步驟6:現在,在IDE中執行pom.xml,或者使用命令列編譯並執行它。

示例

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

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

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”/>
	   </classes>
    </test>
</suite>

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>src/main/java/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
		     <groups>group2</groups>
                </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>

輸出

[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] ------------------------------------------------------------------------

場景2

在這個場景中,我們將說明如何在命令列中傳遞組名以執行特定的測試組,而無需更改testng.xml或pom.xml中的任何內容。

解決此問題的步驟/演算法

  • 步驟1:建立TestNG類 - NewTestngClass

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

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

  • 步驟4:在pom.xml中新增testng.xml的路徑,如下所示。

    在配置部分,提到了testng的路徑。需要記住兩點,第一,此配置應與maven-surefire-plugin一起保留;第二,切勿忘記新增testng的依賴項。

    詳細的pom.xml可以在程式部分找到。

  • 步驟5:我們不會在pom.xml或testng.xml中新增任何組名。

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

示例

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

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

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”/>
	   </classes>
    </test>
</suite>

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>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>

執行命令

mvn test -Dgroups=group2

輸出

[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年8月18日

1K+ 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始
廣告