6

I have a maven project and several test classes.

I want to run these tests in a specific order with the plug-in surefire.

For example, I have:

  • ClassTest1.java
  • ClassTest2.java
  • ClassTest3.java
  • ClassTest4.java

I want to run the Class 1, then 2, then 3 and finally 4.

How can I specify this in the pom.xml?

Marlon Bernardes
  • 13,265
  • 7
  • 37
  • 44
Kazman
  • 155
  • 2
  • 4
  • 12
  • Besides all the answers already given, you might also have a look at TestNG, which supports some test ordering. – SiKing Jun 16 '14 at 22:10

5 Answers5

8

One workaround is to set the runOrder parameter to alphabetical and then rename your tests to have alphabetical order.

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

This isn't recommended, though - unit tests should be independent of each other. The execution order shouldn't matter.

Marlon Bernardes
  • 13,265
  • 7
  • 37
  • 44
  • And if i have set priority for each methods like priority=0, 1, 2. In that case it runs the classes randomly. Even though I have renamed the classes to TC_000, TC_001, TC_002 like this. And every classes - methods starts with priority=0 and so on. – avidCoder Oct 03 '19 at 11:07
2

You can do this with the runOrder parameter.

From the documentation:

Defines the order the tests will be run in. Supported values are "alphabetical", "reversealphabetical", "random", "hourly" (alphabetical on even hours, reverse alphabetical on odd hours), "failedfirst", "balanced" and "filesystem".

See the full description here.

Here is an example

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.17</version>
      <configuration>
        <runOrder>alphabetical</runOrder>
    </configuration>
</plugin>
jlars62
  • 7,183
  • 7
  • 39
  • 60
  • How can I use this from command line? `mvn test -Drunorder=alphabetical : is this correct?` – biligunb Feb 02 '18 at 03:06
  • @biligunb No. It should be `mvn test -Dsurefire.runOrder=alphabetical` referring to the User Property at https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#runOrder – Instein Dec 06 '22 at 01:49
2

I would suggest you to define the order in testng.xml and then create profile via maven and run it. Here is a code sample you can try out:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium Automation" parallel="false">
    <test name="Test1">
        <classes>
            <class name="some.package.Class1"/>
            <class name="some.package.Class2"/>
            <class name="some.package.Class3"/>
        </classes>
    </test>
</suite>

And then in POM.xml you can create a profile as below and refer to the testNG.xml which you want to execute.

<profile>
    <id>any id</id>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin/>
</profile>
Bruno Krebs
  • 3,059
  • 1
  • 24
  • 36
Sachin
  • 51
  • 5
0

You could use a JUnitCore to execute the tests in a specific order. Here is a basic implementation.

public static void main(String[] args)
{
    List<Class> testCases = new ArrayList<Class>();

    //Add test cases
    testCases.add(Class1.class);
    testCases.add(Class2.class);
    for (Class testCase : testCases)
           {
                runTestCase(testCase);
           }
}

private static void runTestCase(Class testCase)
{
    Result result = JUnitCore.runClasses(testCase);
    for (Failure failure : result.getFailures())
    {
        System.out.println(failure.toString());
    }
}

Is there a reason you need them executed in a specific order? The tests should ideally not have dependencies on each other.

meanderingmoose
  • 250
  • 1
  • 3
  • 10
0

You can create a testsuite class with junit and then use surefire to run this class instead, it will run with the order you give, for example:

import org.junit.runner.RunWith; 
import org.junit.runners.Suite; 
import org.junit.runners.Suite.SuiteClasses; 

@RunWith(Suite.class) 
@SuiteClasses({ 
Test1.class
Test2.class
Test3.class
Test4.class
}) 

public class TestSuite { 

} 
Todd Zhang
  • 11
  • 3