【问题标题】:Stop tests execution after first error/failure with maven surefire [duplicate]使用maven surefire在第一次错误/失败后停止测试执行[重复]
【发布时间】:2013-04-05 17:02:14
【问题描述】:

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

我想在第一次失败或错误后停止执行。在我的情况下,这些是修改应用程序状态的集成测试,所以我需要知道失败后的确切系统状态(我们有一个奇怪的问题,如果单独执行测试通过,但如果与整个套件一起执行则不会)。

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

【问题讨论】:

    标签: maven junit maven-surefire-plugin


    【解决方案1】:

    其实事实证明,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>
    

    【讨论】:

      【解决方案2】:

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

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

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

      分开。

      【讨论】:

      • 实际上这是一个maven-surefire-plugin问题,因为maven failsafe不执行测试,它只是验证结果。此外,仅执行失败的测试也没有意义,因为测试在隔离执行时通过(请参阅我的答案和更新)
      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 2021-03-19
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 2020-03-25
      • 1970-01-01
      相关资源
      最近更新 更多