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

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.


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.


If you really need an order of your tests than you should use testng instead of JUnit where you can define dependencies between tests and based on that a particular order of tests. I know that in practice their are times where the independent paradigm does not work.


There is a Maven Surefire plugin that allows you to specify test order.

On the off chance that your tests need to be run in order because they depend on each other, I would strongly recommend against that. Each test should be independent and able to be run by itself. And if each test is independent then it doesn't matter what order they run in. Having independent tests also means you can run a single test repeatedly without having to rerun the entire test chain. This is a huge time savings.

Tags:

Testing

Maven