How do I control the order of execution of tests in Maven?

I need to run tests in order. I fail to find this adequately documented anywhere. I would prefer to do this from command line. Something like

 mvn -Dtest=test1,test2,test3,test5 test

How do I do this?


Solution 1:

You can't specify the run order of your tests.

A workaround to do this is to set the runOrder parameter to alphabetical.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <runOrder>alphabetical</runOrder>
    </configuration>
</plugin>

and then you need to have rename your tests to obtain the expected order.

However it isn't a good idea to have dependent tests. Unit tests must be fIrst.

Solution 2:

You could create a test suite which runs all of your tests and run that.

With junit 4: -

@RunWith(Suite.class)
@Suite.SuiteClasses({Test1.class,
                     Test2.class,
                     Test3.class,
                     Test4.class,
                     Test5.class
})
public class TestSuite
{
}

That will run them in the correct order.