使用maven surefire在第一次错误/失败后停止测试执行

Chi*_*kol 5 junit maven maven-surefire-plugin

我正在使用maven surefire插件来执行我的应用程序的junit测试.

我想在第一次失败或错误后停止执行.在我的例子中,这些是修改应用程序状态的集成测试,因此我需要知道失败后的确切系统状态(如果执行隔离,我们会遇到一个测试通过的奇怪问题,但如果在整个套件中执行则没有).

可能吗?我在这里的插件文档中找不到一个选项.

Chi*_*kol 7

实际上,事实证明这与maven-surefire-plugin无法做到.

我在这里找到了答案.

我实际上最终使用@mhaller提出的解决方案

所以我实现了一个这样的junit监听器:

package br.com.xpto;

import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import br.com.caelum.brutal.integration.scene.AcceptanceTestBase;

public class FailFastListener extends RunListener {

    public void testFailure(Failure failure) throws Exception {
        System.err.println("FAILURE: " + failure);
        AcceptanceTestBase.close();
        System.exit(-1);
    }

    @Override
    public void testFinished(Description description) throws Exception {
        AcceptanceTestBase.close();
        System.exit(-1);
    }
}
Run Code Online (Sandbox Code Playgroud)

并像这样配置maven-surefire:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>surefire-integration</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>none</exclude>
                </excludes>
                <includes>
                    <include>**/scene/**/*Test.java</include>
                </includes>
                <forkMode>once</forkMode>
                <properties>
                    <property>
                        <name>listener</name>
                        <value>br.com.caelum.brutal.integration.util.FailFastListener</value>
                    </property>
                </properties>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <excludes>
            <exclude>**/*</exclude>
        </excludes>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)