Asked  3 Months ago    Answers:  5   Viewed   17 times

I'm wondering if the Maven surefire plugin either runs tests multi-threaded by default (and if so can the number of threads be controlled? ) or if it runs tests from the Test classes in a random order or predictable order, or if the order can dictated by some means.

I haven't verified this yet (I'll do so tomorrow just looking for some heads up guidance and verification at this point), but it looks as if my various JUnit Test classes are getting the tests run in some intermixed order. Which makes it a real pain to orchestrate the creating of the test resources (which are quite hefty in my case).

Its probably a classic problem I run my suite with the Eclipse JUnit runner and everything runs very linear and plays nice. I go to Maven cmd line and things seems to be stepping all over each other.

 Answers

91

By default, Maven runs your tests in a separate ("forked") process, nothing more (this can be controlled using the forkMode optional parameter).

If you are using TestNG or Junit 4.7+ (since SUREFIRE-555), it is possible to run tests in parallel (see the parallel and the threadCount optional parameters) but that's not a default.

Now, while I'm not sure if the surefire plugin behaves the same as JUnit, it is possible to get some control by manually creating a TestSuite and specify the order in which tests are executed:

TestSuite suite= new TestSuite();
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero")); 

You are however strongly advised never to depend upon test execution order, unit tests really should be indeed independent.

P.S.: Just in case, there is also this request SUREFIRE-321 (to run tests in alphabetical order) that you might want to vote for.

Friday, August 13, 2021
 
EnTee
answered 3 Months ago
99

There is an open issue for this, so there's no elegant way to do this.

It would be far simpler for you to pick a framework and stick with it.

Edit: My previous answer doesn't work because you can't specify dependencies in the execution. I've tried a few approaches, but the best I can manage is to create a profile for the TestNG dependency so you can toggle between TestNG and JUnit testing, there doesn't seem to be a means to run both TestNG and Junit 4 tests.

One other point to note: You can launch your JUnit tests from TestNG, but I think this only works for JUnit 3 tests.

Wednesday, July 28, 2021
 
PLPeeters
answered 3 Months ago
13

There's a change by Janinko at https://github.com/janinko/maven-surefire/tree/feature/dryrun Which adds this option:

-Dtest.dryrun=true

Hopefully it will get to upstream.

UPDATE 2021: Note that this fork of maven-surefire has not been updated for 9 years.

Tuesday, August 17, 2021
 
Tanaike
answered 3 Months ago
98

You need to add the maven surefire plugin to run the tests. The configuration can be found here.

Here's a configuration that I've been using with specs/junit.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.8.1</version>
    <configuration>
        <includes>
            <include>**/*Spec.*</include>
            <include>**/*Test.*</include>
        </includes>
    </configuration>
</plugin>

The naming convention is Test, so change PropertyTests to PropertyTest.

Tuesday, September 21, 2021
 
hr0m
answered 1 Month ago
81

As I've explained in question, any mention of groups either in pom.xml or on command line resulted in reduction of executed tests count. Only way I've managed to avoid this is by using mavens profiles like this:

<profiles>
    <profile>
        <id>test-slow</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <groups>slow</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

and then running tests with

mvn -P test-slow test
Tuesday, September 28, 2021
 
smogg
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :