Eli*_*cum 37 junit maven-2 surefire
我目前正在使用maven开发一个java项目.我们使用maven surefire插件来运行我们的junit套件作为构建过程的一部分.
我们的测试套件在覆盖范围和执行时间都在快速增长.当您在测试的第一分钟内等待十分钟以发现测试失败时,执行时间非常令人沮丧且耗时.
我想找到一种方法,使构建过程在测试套件中的第一个错误/失败时失败.我知道这对于其他构建工具是可行的,但是我一直无法找到使用maven surefire来做到这一点的方法.
我知道在surefire jira 中有一个未解决的此功能的票证,但我希望有一个现有的解决方案.
mha*_*ler 11
可能有一个合适的解决方法,但这取决于您的要求,您需要使用可以处理jvm进程返回码的CI服务器.
基本思想是完全停止Maven的JVM进程,并让操作系统知道进程已意外停止.然后,像Jenkins/Hudson这样的持续集成服务器应该能够检查非零退出代码并让您知道测试失败了.
第一步是确保在第一次测试失败时退出JVM.您可以使用自定义RunListener(将其放在src/test/java中)使用JUnit 4.7或更高版本执行此操作:
package org.example
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
public class FailFastListener extends RunListener {
public void testFailure(Failure failure) throws Exception {
System.err.println("FAILURE: " + failure);
System.exit(-1);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要配置该类,以便surefire将其注册到JUnit 4 Runner.编辑pom.xml并将listener配置属性添加到maven-surefire-plugin.您还需要配置surefire以不分叉新的JVM进程来执行测试.否则,它将继续下一个测试用例.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<forkMode>never</forkMode>
<properties>
<property>
<name>listener</name>
<value>org.example.FailFastListener</value>
</property>
</properties>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
如果这没有帮助,我会尝试分叉maven surefire junit提供程序插件.
顺便说一下,根据定义,单元测试的运行速度应该超过0.1秒.如果您的构建由于单元测试确实需要很长时间,那么将来必须使它们运行得更快.
您可以maven使用--fail-fast选项运行:
该
-ff选项对于运行交互式构建并希望在开发周期中获得快速反馈的开发人员非常有用。
一个例子可以是:
mvn clean test -ff
http://books.sonatype.com/mvnref-book/reference/running-sect-options.html