【问题标题】:Is there a way to separate long running (e.g. stress tests) out so they're not run by default in Maven 2?有没有办法将长时间运行(例如压力测试)分开,以便它们在 Maven 2 中默认不运行?
【发布时间】:2010-09-20 02:03:51
【问题描述】:
我们在这里一直存在需求,我不知道如何使用现有的 Maven 2 工具和文档来解决。
我们的一些开发人员有一些运行时间很长的 JUnit 测试(通常是压力测试),在任何情况下都不应将其作为构建过程/夜间构建的常规部分运行。
当然,我们可以使用 surefire 插件的排除机制,然后将它们从构建中剔除,但理想情况下,我们希望开发人员可以通过 Maven 2 随意运行它们。
【问题讨论】:
标签:
java
maven-2
build-process
junit
build-automation
【解决方案1】:
通常你会在你的 maven 配置中添加一个配置文件来运行一组不同的测试:
使用 mvn -Pintegrationtest install 运行它
<profile>
<id>integrationtest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-client -Xmx896m -XX:MaxPermSize=192m</argLine>
<forkMode>once</forkMode>
<includes>
<include>**/**/*Test.java</include>
<include>**/**/*IntTest.java</include>
</includes>
<excludes>
<exclude>**/**/*SeleniumTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<activation>
<property>
<name>integrationtest</name>
</property>
</activation>
</profile>
【解决方案2】:
添加到 krosenvold 的回答中,为确保不会出现意外行为,请确保您还有一个默认情况下处于活动状态的默认配置文件排除集成或对您进行压力测试想在您的特殊配置文件中运行。
<profile>
<id>normal</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/**/*IntTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
您需要像这样创建一个配置文件,只需在配置文件之外列出 surefire-plugin 将覆盖配置文件,如果它被选择:
mvn -P integrationtest clean install
【解决方案3】:
使用诸如Super Helpful Integration Test Thingy 之类的集成测试插件将集成测试(长期运行的、系统的)与单元测试分开(纯粹主义者说所有真正的单元测试最多运行 30 秒)。为您的单元测试和集成测试制作两个 Java 包。
然后不要将此插件绑定到一个阶段(正常的 maven 生命周期),并且仅在它被显式调用为目标时才运行它,如下所示:
mvn shitty:clean shitty:install shitty:test
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>shitty-maven-plugin</artifactId>
</plugin>
</plugins>
这样,您的普通开发人员不会受到影响,并且您将能够按需运行集成测试。
【解决方案4】:
另一种选择是让压力测试检测它是否在 maven 中运行并且只运行一次或两次。即变成常规的功能测试。这样可以检查代码还是不错的,但是运行时间长了。