如何在Maven 2中的两个测试套件之间切换? [英] How can I switch between two test suites in Maven 2?

查看:93
本文介绍了如何在Maven 2中的两个测试套件之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We're using the maven-surefire-plugin to run our Java tests. The tests fall into two categories:

  • 快速测试
  • 测试缓慢

整个快速"套件可以在几秒钟内运行,而慢速测试则需要半个小时.

The whole "fast" suite runs in a couple of seconds while the slow tests take half an hour.

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

During development, I want to run only the fast tests. When I commit, I'd like to be able to run the slow tests as well, so running the slow tests should be an option while the fast tests should be the default.

在CI服务器上,我都希望同时运行.

On the CI server, I want to run both.

慢速测试包括快速测试就可以了(甚至是首选).

It's OK (and even preferred) when the slow tests include the fast ones.

在这种情况下,我应该如何设置Maven,JUnit和Surefire?

How should I set up Maven, JUnit and Surefire for this scenario?

推荐答案

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

In a commercial project I've made from scratch on my own, I divided tests into unit (which I named *Test.java), and integration (*IT.java), according to, respectively, Surefire and Failsafe plugin policies, which I used for running the tests. ITs run, of course, much slower than UTs.

这提供了使用简单命令运行测试组的能力:对于UTs mvn test,对于UTs和ITs mvn integration-test,以及仅使用mvn install -DskipITs跳过ITs的可能性.

This gives the power of running the group of tests with simple commands: mvn test for UTs and mvn integration-test for both UTs and ITs, as well as the possiblity of skipping only ITs with mvn install -DskipITs.

另一件事是,由于环境问题(例如,数据库启动时间太长,消息代理关闭得太早以及由于环境问题),集成测试结果失败的可能性更大,因为它们比单元测试失败的频率更高.很快).默认情况下,除非您明确包含验证"目标,否则Failsafe测试失败不会杀死该构建:

One more good thing is a possibility to be more lax with integration tests results, as they fail more often than unit tests, because of problems with environment (i.e. database taking too long to start, message broker shutting down too early, and so on). By default, failure of a Failsafe test does not kill the build, unless you include the "verify" goal explicitly:

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

这篇关于如何在Maven 2中的两个测试套件之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆