如何使Maven Surefire正確執行JUnit和TestNG測試?
Maven是一個專案管理和理解工具,它提供了一個完整的構建生命週期框架。使用者幾乎可以立即自動化專案的構建基礎設施,因為Maven使用標準的目錄佈局和預設的構建生命週期。
總而言之,Maven簡化並標準化了專案構建過程。它可以無縫地處理編譯、分發、文件、團隊協作和其他任務。Maven提高了可重用性,並處理了大多數與構建相關的任務。
TestNG和JUnit是測試框架,可以使用Maven作為構建工具。它有助於在一個地方(pom.xml)維護依賴項及其版本。
使用者可以使用surefire外掛在Maven中執行JUnit和TestNG測試用例。但是,它提供了使用相同構建單獨執行它們的靈活性。
在本教程中,我們將瞭解如何透過surefire在同一個專案中執行JUnit和TestNG測試用例。
解決此問題的方法/演算法
步驟1:建立一個JUnit類 - JunitTest
步驟2:建立一個TestNG類 - newTest
步驟3:在兩個類中編寫@Test方法。
步驟4:現在在pom.xml中新增TestNG和JUnit依賴項。
步驟5:在pom.xml中新增兩個執行部分。一個用於TestNG,另一個用於JUnit,如程式程式碼部分所示。
步驟6:現在,執行所需的執行以執行JUnit或TestNG測試用例。
命令語法應為
mvn groupId:artifactId:versionId:goal@excution-id
步驟7:使用者可以透過執行mvn test一次執行TestNG和JUnit用例,但結果是分開的。
示例
以下程式碼展示瞭如何在Maven中執行JUnit和TestNG測試用例。
src/ test/java/JunitTest.java
import org.junit.Test;
public class JunitTest {
@Test
public void testcase1() {
System.out.println("in test case 1 of junit");
}
}
src/ test/java/newTest.java
import org.testng.annotations.Test;
public class newTest {
@Test(groups = { "unit", "integration" })
public void testCase1() {
System.out.println("in test case 1 of newTest");
}
@Test(groups = { "integration" })
public void testCase2() {
System.out.println("in test case 2 of newTest");
}
@Test(groups = { "unit" })
public void testCase3() {
System.out.println("in test case 3 of newTest");
}
}
pom.xml
這是一個Maven配置檔案,用於組織依賴項、外掛和執行JUnit和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>
<!-- configured to work with JUnit and TestNg in same project separatelly -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- skip the default execution, we have separate for TestNG and for JUnit -->
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>TestNGRun</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>%regex[.*newTest.*]</include>
</includes>
<!-- used to skip JUnit provider -->
<junitArtifactName>null:null</junitArtifactName>
<!-- to continue on next execution in case of failures here -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</execution>
<execution>
<id>JUnitRun</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<!-- used to skip TestNg provider -->
<testNGArtifactName>null:null</testNGArtifactName>
<excludes>
<exclude>%regex[.*newTest.*]</exclude>
</excludes>
</configuration>
</execution>
</executions>
</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
執行TestNG測試用例的命令
mvn org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5::test@TestNGRun
輸出
[INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.sample:TestNGProjectct >--------------------- [INFO] Building TestNGProjectct 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (TestNGRun) @ TestNGProjectct --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running newTest in test case 1 of NewTestngClass in test case 2 of NewTestngClass in test case 3 of NewTestngClass [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.547 s - in newTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] -------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------- [INFO] Total time: 1.854 s [INFO] Finished at: 2022-02-15T16:48:13+05:30 [INFO] -------------------------------------------------------- Process finished with exit code 0
執行JUnit測試用例的命令
mvn org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5::test@JUnitRun
輸出
[INFO] [INFO] -------------< com.sample:TestNGProjectct >------------- [INFO] Building TestNGProjectct 1.0-SNAPSHOT [INFO] ------------------------[ jar ]------------------------- [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (JUnitRun) @ TestNGProjectct --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running JunitTest in test case 1 of junit [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 s - in JunitTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] -------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------- [INFO] Total time: 1.430 s [INFO] Finished at: 2022-02-15T17:01:57+05:30 [INFO] -------------------------------------------------------- Process finished with exit code 0
同時執行TestNG和JUnit測試用例的命令
mvn test
輸出
[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] 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] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (TestNGRun) @ TestNGProjectct --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running newTest in test case 1 of NewTestngClass in test case 2 of NewTestngClass in test case 3 of NewTestngClass [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.575 s - in newTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (JUnitRun) @ TestNGProjectct --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running JunitTest in test case 1 of junit [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.068 s - in JunitTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] -------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------- [INFO] Total time: 3.927 s [INFO] Finished at: 2022-02-15T17:05:07+05:30 [INFO] -------------------------------------------------------- Process finished with exit code 0
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP