Stop tests execution after first error/failure with maven surefire [duplicate]
Asked Answered
D

2

7

I'm using maven surefire plugin to execute the junit tests of my application.

I want to stop the execution after the first failure or error. In my case, these are integration tests that modify the application state, so I need to know the exact system state after the failure (we're having a strange issue that a test passes if executed isolated, but not if executed with the whole suite).

Is it possible? I couldn't find a option in the plugin docs here.

Dukie answered 5/4, 2013 at 17:2 Comment(0)
D
7

Actually, it turns out that this is not possible to do with maven-surefire-plugin.

I found the answer here.

I actually end up using the solution proposed there by @mhaller

So I implemented a junit listener like this:

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);
    }
}

And configure maven-surefire like this:

<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>
Dukie answered 5/4, 2013 at 17:45 Comment(0)
U
2

First for integration tests you should use the maven-failsafe-plugin and not the maven-surefire-plugin.

Furthermore if you have integration tests which fail which is usually done on a CI environment. Afterwards you can run your failing integration test via

mvn -Dit.test=NameOfTheFailedIntegrationTest verify

separately.

Unfetter answered 5/4, 2013 at 17:18 Comment(1)
actually it is a maven-surefire-plugin issure, since maven failsafe does not execute the tests, it just verify the results. Also its not of interest to execute only the failing tests, since the tests are passing when executed isolated (see my answer and update)Dukie

© 2022 - 2024 — McMap. All rights reserved.