Alex
Alex

Reputation: 2179

How to run tests with maven twice?

I want to run maven tests twice, the second run must be straight after the first one. Is there any command to fire mvn tests twice?

Upvotes: 0

Views: 1074

Answers (2)

Vinay Veluri
Vinay Veluri

Reputation: 6865

Here is an easy way to do that

public class RepeatTests extends TestCase {

public static Test suite() {
    TestSuite suite = new TestSuite(RepeatTests.class.getName());

    for (int i = 0; i < 10; i++) {              
    suite.addTestSuite(YourTest.class);             
    }

    return suite;
}
}

With that you can run any number of times the test cases you want.

EDIT:

As explained here, if you have any common naming between the test cases, this works

import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.runner.RunWith;

@RunWith(ClasspathSuite.class)
@ClassnameFilters({".*UnitTest"})
public class MySuite {
}

Upvotes: 1

Benjamin
Benjamin

Reputation: 1846

Why do you run them twice?

What you can do is declaring a second execution of the surefire plugin. Beware of modifying the configuration (e.g.: changing the reportsDirectory), else maven will skip the 2nd execution:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <executions>
                <execution>
                    <id>test-second-run</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <phase>test</phase>
                    <configuration>
                        <reportsDirectory>${project.build.directory}/surefire-reports-2</reportsDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upvotes: 1

Related Questions