通常來講 Java 的 Unit Test 只會用一個 Framework 來跑,
例如使用JUnit5、或著是使用
TestNg 等,一次只會用一種 Framework,
不過 JUnit5 提供一個 JUnit Platform
可供其他第三方的 Unit Test framework去 implement,
讓 JUnit5 可以跑其他的
Unit Test framework,
這樣我們就可以同時跑專案裡的 JUnit5 和 TestNg
的測試案例了。
下面展示範例:
Maven Dependency 的 pom.xml:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>hugo</groupId> <artifactId>junittest</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>11</maven.compiler.target> <maven.compiler.source>11</maven.compiler.source> </properties> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>3.1.2</version> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.10.3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.10.2</version> <scope>test</scope> </dependency> <!-- 使用 testng-engine 讓 JUnit5 可以跑 TestNg 的 Test Case --> <!-- https://mvnrepository.com/artifact/org.junit.support/testng-engine --> <dependency> <groupId>org.junit.support</groupId> <artifactId>testng-engine</artifactId> <version>1.0.5</version> <scope>test</scope> </dependency> </dependencies> </project>
兩個 Test Case Class,一個使用 JUnit5、另一個使用 TestNg:
Juit5Test.java (使用 JUnit5):
package junittest; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class Juit5Test { @BeforeAll static void beforeAll() { System.out.println("Junit5: beforeAll"); } @Test void test() { assertEquals(1, 1, "Should be equal."); System.out.println("Junit5: test1"); } @Test void test2() { assertEquals(1, 1, "Should be equal."); System.out.println("Junit5: test2"); } }
TestNgTest.java (使用 TestNg):
package junittest; import static org.testng.Assert.assertEquals; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestNgTest{ @BeforeTest static void beforeAll() { System.out.println("TestNg: beforeAll"); } //TestNg 可以使用 dependsOnMethods 來設定要在哪個 Test methods 之後才執行 @Test(dependsOnMethods = {"test2"}) void test() { assertEquals(1, 1, "Should be equal."); System.out.println("TestNg: test1"); } @Test void test2() { assertEquals(1, 1, "Should be equal."); System.out.println("TestNg: test2"); } }
接下來我們就可以使用 Maven 的 test 指令來跑 Test Case:
mvn clean test
以下是跑完的結果,可以看到 JUnit5 和 TestNg 的 Test Case 都有被成功執行
[INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. [INFO] Running junittest.Juit5Test Junit5: beforeAll Junit5: Junit5: OK1 Junit5: Junit5: OK2 [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 s -- in junittest.Juit5Test [INFO] Running junittest.TestNgTest TestNg: beforeAll TestNg: TestNg: OK2 TestNg: TestNg: OK1 [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s -- in junittest.TestNgTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
The engine’s main intention is integration with build tools like Gradle and Maven. Hence, custom suites specified via testng.xml files are not supported.
沒有留言 :
張貼留言