如何在沒有testng.xml檔案的情況下執行Testng和Maven?


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

Maven提供了使用surefire外掛執行測試的靈活性。它允許使用者執行testng.xml以及直接執行testng類而無需使用testng.xml。實現此目標需要一些先決條件。

  • 所有testng類都應在src/test/java下建立。如果類未在這些目錄下建立,則使用者必須在pom.xml檔案中傳遞testng.xml。

  • 預設情況下,maven surefire外掛識別以下類:

    • "**/Test*.java" - 包括所有子目錄和所有以"Test"開頭的Java檔名。

    • "**/*Test.java" - 包括所有子目錄和所有以"Test"結尾的Java檔名。

    • "**/*Tests.java" - 包括所有子目錄和所有以"Tests"結尾的Java檔名。

    • "**/*TestCase.java" - 包括所有子目錄和所有以"TestCase"結尾的Java檔名。

在本文中,我們將說明如何在不使用testng.xml的情況下使用maven執行TestNG類。

解決這個問題的方法/演算法

  • 步驟1:在src/test/java下建立一個TestNG類 - NewTest

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

  • 步驟3:確保從pom.xml中刪除suiteXMLFiles標籤。

  • 步驟4:現在,使用命令列mvn test執行它。它將執行專案中滿足上述條件的所有類。

示例

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

src/test/java/NewTest.java

import org.testng.annotations.Test;

public class NewTest {

    @Test()
    public void testCase1() {
        System.out.println("in test case 1 of NewTest");
    }
   @Test()
    public void testCase2() {
        System.out.println("in test case 2 of NewTest");
	   
    }
}  

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

輸出

[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 1 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] 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 newTest
in test case 1 of NewTest 
in test case 2 of NewTest 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.73 s - in newTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.916 s
[INFO] Finished at: 2022-02-10T08:32:07+05:30
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0 

更新於:2023年8月17日

887 次檢視

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告