4

我们使用maven-surefire-plugin来运行我们的 Java 测试。测试分为两类:

  • 快速测试
  • 慢速测试

整个“快速”套件在几秒钟内运行,而慢速测试需要半小时。

在开发过程中,我只想运行快速测试。当我提交时,我也希望能够运行慢速测试,因此运行慢速测试应该是一个选项,而快速测试应该是默认设置。

在 CI 服务器上,我想同时运行两者。

当慢速测试包括快速测试时,这是可以的(甚至是首选)。

我应该如何为这种情况设置 Maven、JUnit 和 Surefire?

4

2 回答 2

3

您应该使用来自 junit 的类别:Junit 类别

第一个解决方案

配置maven-surefire-plugin版本至少 2.11

     <profile>
                    <id>normal</id>
                    <activation>
                            <activeByDefault>true</activeByDefault>
                    </activation>
                    <build>
                            <plugins>
                                    <plugin>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-surefire-plugin</artifactId>
                                            <configuration>
                                                    <excludedGroups>com.test.SlowTests</excludedGroups>
                                            </configuration>
                                    </plugin>
                            </plugins>
                    </build>
            </profile>

第二种解决方案

在配置部分,您可以添加带有文件的正则表达式以仅支持类(默认配置):

           <configuration>
                <includes>
                    <include>**/*Test*.java</include>
                    <include>**/*Test.java</include>
                    <include>**/*TestCase.java</include>
                </includes>
            </configuration>
于 2012-03-02T12:46:22.600 回答
1

在我自己从头开始制作的一个商业项目中,我分别根据用于运行测试的 Surefire 和 Failsafe 插件策略将测试分为单元(我命名为*Test.java)和集成( )。*IT.java当然,IT 的运行速度比 UT 慢得多。

这赋予了使用简单命令运行测试组的能力:mvn test对于 UT 以及mvn integration-test对于 UT 和 IT,以及使用mvn install -DskipITs.

另一件好事是集成测试结果可能会更加宽松,因为它们比单元测试更容易失败,因为环境问题(即数据库启动时间过长,消息代理过早关闭等等) . 默认情况下,Failsafe 测试的失败不会终止构建,除非您明确包含“验证”目标:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
        <!-- Uncomment this in order to fail the build if any integration test fail -->
        <!-- execution> <id>verify</id> <goals><goal>verify</goal></goals> </execution -->
    </executions>
</plugin>
于 2012-03-02T14:25:20.287 回答