11

我正在使用 JUnit 类别将集成测试与单元测试分开。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 是正确的吗?

4

2 回答 2

16

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

<configuration>
    <includes>
        <include>**/*.java</include>
    </includes>
    <groups>com.mycompany.test.IntegrationTest</groups>
    <excludedGroups>com.mycompany.test.UnitTest</excludedGroups>
</configuration>
于 2013-08-22T07:08:51.770 回答
1

解决方法

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

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

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

使用一些字符串为集成测试添加前缀或后缀,并在包含中给出。默认情况下,故障安全将所有以 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 - 当模块中没有测试时有一个错误,它将寻找 failsafe-summary.xml 并失败 https://issues.apache.org/jira/browse/SUREFIRE-901
  • 于 2012-07-19T00:23:40.153 回答