可能有合适的解决方法,但这取决于您的要求,您需要使用可以处理 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);
}
}
然后,您需要配置该类,以便 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>
如果这没有帮助,我会尝试分叉 maven surefire junit 提供程序插件。
顺便说一句,根据定义,单元测试的运行速度应该超过 0.1 秒。如果由于单元测试,您的构建确实需要很长时间,那么您将来必须让它们运行得更快。