java - 如何让 maven surefire 正确执行 JUnit 和 TestNG 测试?

标签 java maven junit testng

一段时间以来,可以配置 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>

(taken from here)

这工作得很好,jUnit 测试和 testNG 测试得到执行。

但是 - 现在我看到 testNG 也尝试执行 jUnit 测试(也许反之亦然) - 没有成功,当然,因为它看不到它的任何注释,但它看起来像重新 -将测试标记为“已通过”...无论如何,一些报告工具不再显示 jUnit 测试中的测试失败,除非我注释第二个依赖项。

是否有更好的方法来配置 surefire,以便来自两个框架的测试仅由它们的测试运行器执行?

最佳答案

遇到了和你一样的问题。 TestNG 报告中的结果为零,但看起来测试已运行。 最后在 maven-surefire-plugin 的两个单独执行上完成这项工作

  1. 首先删除 dependencies插件部分。
    • 否则如果你使用<testNGArtifactName> (我们将需要它)无论如何测试都将由 TestNG 执行,并且会在 java.lang.NullPointerException 上失败
  2. 将 TestNG 测试和 JUnit 测试分开。在我们的例子中,NG 测试类名称以 TestNg 结尾
  3. 跳过默认执行
  4. 定义两个<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/

相关文章:

java - Mockito 空指针异常

java - 主类在 JUnit 线程错误中运行异常

java - 由于缺少库,从命令行运行可执行 jar 失败

java - 如何包含具有 Maven 依赖项的本地 Artifact :go-offline

java - 生命周期配置未涵盖插件执行 : com. mycila.maven-license-plugin

java - 从类型转换操作返回模拟,Mockito,Java

Java如何删除字符串中两个子字符串之间的所有内容

java - Jersey 应用程序 404 未找到错误

java - 在两个正方形之间绘制平行线

java - 模块java类之间的循环依赖