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

时间:2013-04-05 17:02:14

标签: maven junit maven-surefire-plugin

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

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

有可能吗?我在插件文档here中找不到选项。

2 个答案:

答案 0 :(得分:7)

实际上,事实证明这与maven-surefire-plugin无法实现。

我找到了答案here

我实际上最终使用了@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);
    }
}

并像这样配置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>

答案 1 :(得分:1)

首先,对于集成测试,您应该使用maven-failsafe-plugin而不是 maven-surefire-plugin。

此外,如果您的集成测试失败,通常在CI环境中完成。之后,您可以通过

运行失败的集成测试
mvn -Dit.test=NameOfTheFailedIntegrationTest verify

分开。

相关问题