问题 将JUnit类与Maven Failsafe插件一起使用


我正在使用JUnit Categories将集成测试与单元测试分开。 Surefire插件配置有效 - 它会跳过使用我的标记接口IntegrationTest注释的测试。

但是,Failsafe插件不会选择集成测试。我甚至尝试指定junit47提供程序,但是在集成测试阶段运行零测试。

这是pom.xml片段:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.12</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <groups>com.mycompany.test.IntegrationTest</groups>
                <excludedGroups>com.mycompany.test.UnitTest</excludedGroups>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.12</version>
                </dependency>
            </dependencies>
        </plugin>

这是日志的故障安全部分:

[INFO] --- maven-failsafe-plugin:2.12:integration-test (default) @ MyProject.war ---
[INFO] Failsafe report directory: /home/stoupa/MyProject/war/target/failsafe-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Concurrency config is parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

提供者org.apache.maven.surefire.junitcore.JUnitCoreProvider是否可以在日志输出中看到正确的一个?


5322
2017-07-18 13:27


起源

为什么不使用命名约定来进行maven-failsafe-plugin的集成测试?可能会更简单。 - khmarbaise
我在这里也遇到了同样的问题。用surefire替换故障安全时一切正常...... - Boaz


答案:


默认情况下,failsafe插件会排除各种文件。你必须覆盖它。因此,请将配置部分更改为以下内容:

<configuration>
    <includes>
        <include>**/*.java</include>
    </includes>
    <groups>com.mycompany.test.IntegrationTest</groups>
    <excludedGroups>com.mycompany.test.UnitTest</excludedGroups>
</configuration>

13
2017-08-22 07:08



是的,默认情况下,它仅包含与用于集成测试的某些模式匹配的Java文件(例如“* IT.java”)。如果使用类别而不是文件名匹配,则需要删除文件名匹配条件。 - Scott McIntyre


替代方法

@Categories会给你带来痛苦,因为你必须标记每个集成测试。

尝试创建如下所述的inttests配置文件,并跳过surefire执行。

 <profile>
        <id>inttests</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <!-- skip the unit tests -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <configuration>
                        <includes>
                            <include>**/IT*.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

使用某些字符串对Integration测试进行前缀或后缀,并在包含中给出。 默认情况下,failsafe会将所有以IT为前缀的测试作为集成测试。 参考: 故障安全包含和排除

使用maven profile命令运行它

mvn verify -P inttests

注意: 在上面提到的方法中,我们需要在构建期间运行单元测试,然后单独进行inttests。

更新:使用故障安全插件的JUnit4类别

  • 2.12 - 分叉的虚拟机终止而没有说再见。 VM崩溃或System.exit调用? https://issues.apache.org/jira/browse/SUREFIRE-827
  • 2.12.1 - 无法在插件工件列表中找到surefire-booter https://issues.apache.org/jira/browse/SUREFIRE-896
  • 2.12.2 - 当模块中没有测试时会出现bug,它会查找failsafe-summary.xml并失败 https://issues.apache.org/jira/browse/SUREFIRE-901

  • 2
    2017-07-19 00:23



    你不需要标记每个测试,足以创建一个带注释的超类并从中继承。无论如何,我不知道如何将每个类注释成为一个让它们命名为IT *的痛苦 - hithwen
    这个答案并没有真正回答这个问题,这是一个解决方法。我遇到了同样的问题。这是一个错误吗? - Jörgen Lundberg
    同样的问题在这里......有人知道解决方案吗? <excludeGroups>然而有效! - tk2000
    @hithwen我提到了解决我们所面临的问题的痛苦,因为类别注释具有故障保护功能。我刚刚编辑了这个解决方法,这就是我试图解决它的方法。 - raksja