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

And*_*s_D 6 java junit testng maven jnario

有一段时间,可以配置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>
Run Code Online (Sandbox Code Playgroud)

(取自这里)

这很有效,jUnit测试和testNG测试得到执行.

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

有没有更好的方法来配置surefire,以便两个框架的测试只能由他们的测试运行者执行?

Cas*_*sey 6

和你有同样的问题.零会导致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>
Run Code Online (Sandbox Code Playgroud)

经测试:

<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>
Run Code Online (Sandbox Code Playgroud)

这个配置来自以下方面的想法:
在一次运行中使用Maven Surefire运行TestNG-,JUnit3-和JUnit4-测试,
您的SUREFIRE错误报告,surefire
插件配置

希望能帮助到你.


Mar*_*ler 1

我自己没有这样做,但您可以尝试配置 maven-surefire-plugin 的两个不同执行,并将其中一个执行配置为仅运行 JUnit 测试,另一个仅运行 testNG 测试。为了区分 JUnit 和 testNG 测试,您应该将测试放在不同的目录中,或者使用可区分的名称方案,并使用合适的包含和排除模式配置每个执行。

理论上这应该有效:)