一段时间以来,可以配置 maven surefire 以在一个构建中执行 jUnit 测试和 testNG 测试。我不会详细说明我这样做的原因(好吧,提示:testNG 是我们的标准框架,但一些框架如 jnario 需要 jUnit)。
一般的做法是像这样配置 surefire 插件:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
这工作得很好,jUnit 测试和 testNG 测试得到执行。
但是 - 现在我看到 testNG 也尝试执行 jUnit 测试(也许反之亦然) - 没有成功,当然,因为它看不到它的任何注释,但它看起来像重新 -将测试标记为“已通过”...无论如何,一些报告工具不再显示 jUnit 测试中的测试失败,除非我注释第二个依赖项。
是否有更好的方法来配置 surefire,以便来自两个框架的测试仅由它们的测试运行器执行?
最佳答案
遇到了和你一样的问题。 TestNG 报告中的结果为零,但看起来测试已运行。
最后在 maven-surefire-plugin
的两个单独执行上完成这项工作
- 首先删除
dependencies
插件部分。- 否则如果你使用
<testNGArtifactName>
(我们将需要它)无论如何测试都将由 TestNG 执行,并且会在java.lang.NullPointerException
上失败
- 否则如果你使用
- 将 TestNG 测试和 JUnit 测试分开。在我们的例子中,NG 测试类名称以
TestNg
结尾 - 跳过默认执行
- 定义两个
<executions>
如下所示:
<plugin>
<!-- configured to work with JUnit and TestNg in same project separatelly -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- whatever you need... -->
<!-- skip the default execution, we have separate for Ng and for JUnit -->
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>TestNg-execution</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- overwrite skip from default config -->
<skip>false</skip>
<includes>
<include>%regex[.*TestNg.*]</include>
</includes>
<!-- used to skip JUnit profider -->
<junitArtifactName>dev:null</junitArtifactName>
<!-- to continue on next execution in case of failures here -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</execution>
<execution>
<id>JUnit-execution</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<!-- used to skip TestNg profider -->
<testNGArtifactName>dev:null</testNGArtifactName>
<excludes>
<exclude>%regex[.*TestNg.*]</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
测试:
<junit-version>4.11</junit-version>
<maven-surefire-plugin-version>2.16</maven-surefire-plugin-version>
<org.testng-version>6.8.7</org.testng-version>
此配置来自于:
Running TestNG-, JUnit3- and JUnit4-tests with Maven Surefire in one run ,
your SUREFIRE bug report ,
surefire plugin configuration
希望对您有所帮助。
关于java - 如何让 maven surefire 正确执行 JUnit 和 TestNG 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17816150/