简体   繁体   English

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

[英]Howto have maven surefire execute JUnit and TestNG test properly?

Since a while it is possible to configure maven surefire to execute jUnit tests and testNG tests in one build.一段时间以来,可以将 maven surefire 配置为在一个构建中执行 jUnit 测试和 testNG 测试。 I won't expand on the reasons why I'm doing that (ok, hint: testNG is our standard framework but some frameworks like jnario require jUnit).我不会详细说明我这样做的原因(好吧,提示:testNG 是我们的标准框架,但像jnario这样的一些框架需要 jUnit)。

The general way to do it is to configure the surefire plugin like this:这样做的一般方法是像这样配置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) (取自这里)

This works quite well, jUnit tests and testNG tests get executed.这很有效,jUnit 测试和 testNG 测试得到执行。

BUT - now I see that testNG tries to execute the jUnit tests too (and maybe vice-versa) - with no success, of course, because it won't see any of its annotations, but it looks like it re-marks the tests to "passed"... anyway, some reporting tools don't show test fails in jUnit tests anymore unless I comment the second dependency entry.但是 - 现在我看到 testNG 也尝试执行 jUnit 测试(反之亦然) - 当然没有成功,因为它看不到任何注释,但看起来它重新标记了测试“通过”...无论如何,除非我评论第二个依赖项,否则某些报告工具不再显示 jUnit 测试中的测试失败。

Is there any better way to configure surefire so that tests from both frameworks are executed ONLY by their test runners?有没有更好的方法来配置surefire,以便来自两个框架的测试仅由它们的测试运行器执行?

Had the same problem as you.和你有同样的问题。 Zero results in TestNG reports but looks like tests were runned. TestNG 报告中的结果为零,但看起来像是运行了测试。 Finally get this work on two separate executions of maven-surefire-plugin最后在maven-surefire-plugin两个单独执行上完成这项工作

  1. First remove the dependencies section from plugin.首先从插件中删除dependencies部分。
    • otherwise if you use <testNGArtifactName> (and we'll need it) tests will be executed by TestNG anyway and will fail on java.lang.NullPointerException否则,如果您使用<testNGArtifactName> (我们将需要它),无论如何测试将由 TestNG 执行,并且会在java.lang.NullPointerException失败
  2. Have some separation of TestNG tests and JUnit tests. TestNG 测试和 JUnit 测试有一些分离。 In our case NG test class names ends with TestNg在我们的例子中,NG 测试类名称以TestNg
  3. skip default execution跳过默认执行
  4. define two <executions> like below:定义两个<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>

Tested with:测试:

<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>

This configuration is the marge of ideas from:此配置是来自以下方面的想法的边缘:
Running TestNG-, JUnit3- and JUnit4-tests with Maven Surefire in one run , 在一次运行中使用 Maven Surefire 运行 TestNG-、JUnit3- 和 JUnit4-tests
your SUREFIRE bug report ,您的 SUREFIRE 错误报告
surefire plugin configuration 万无一失的插件配置

Hope it helps.希望能帮助到你。

I haven't done it myself, but you could try to configure two different executions of the maven-surefire-plugin and configure one of these executions to only run JUnit tests and the other to only run testNG tests.我自己没有做过,但是您可以尝试配置 maven-surefire-plugin 的两个不同执行,并将其中一个执行配置为仅运行 JUnit 测试,另一个仅运行 testNG 测试。 To differentiate between JUnit und testNG tests you should either put the tests in different directories or use a distinguishable name scheme and configure each of the executions with suitable inclusion and exclusion patterns .要区分 JUnit 和 testNG 测试,您应该将测试放在不同的目录中,或者使用可区分的名称方案并使用合适的包含和排除模式配置每个执行。

In theory this should work :)理论上这应该有效:)

不确定页面底部的运行 TestNG 和 JUnit 测试部分是否在 2013 年存在,但在surefire-testng提供程序中设置junit=false现在似乎可以回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM