如何在TestNG + Maven中使用註解分離單元測試和整合測試?


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

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

此功能可用於分離單元測試和整合測試。使用者可以將所有與單元測試相關的測試歸類為“unit”,將整合測試歸類為“integration”,如果某些測試屬於兩者,則可以將其定義為“unit”和“integration”。

在本教程中,我們將瞭解如何分離單元測試和整合測試。

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

  • 步驟 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或testng.xml中新增任何組名。

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

示例

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

src/ NewTestngClass.java

import org.testng.annotations.Test;

public class NewTestngClass {

    @Test(groups = { "unit", "integration" })
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
   @Test(groups = { "integration" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
    }
   @Test(groups = { "unit" })
    public void testCase3() {
        System.out.println("in test case 3 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=unit

輸出

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.sample:TestNGProjectct >---------------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGProjectct ---
[INFO] Deleting C:\Users\anandas\IdeaProjects\TestNGProjectct\target
[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 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 8 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\classes
[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] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct ---
[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 1 of NewTestngClass
in test case 3 of NewTestngClass
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.639 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------
[INFO] Total time:  3.840 s
[INFO] Finished at: 2022-02-11T19:41:23+05:30
[INFO] --------------------------------------------------------

Process finished with exit code 0

更新於: 2023年8月17日

167 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告