Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 744
Chico Sokol Avatar asked Apr 05 '13 17:04

Chico Sokol


2 Answers

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>
like image 148
Chico Sokol Avatar answered Nov 15 '22 08:11

Chico Sokol


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.

like image 29
khmarbaise Avatar answered Nov 15 '22 07:11

khmarbaise