9

I am using JUnit and Selenium Webdriver. I want to run my test methods in order as how I write them in my code, as below:

@Test
public void registerUserTest(){
    // code
}

@Test
public void welcomeNewUserTest(){
    // code
}

@Test
public void questionaireNewUserTest(){
    // code
}

But it doesn't work, it always executes my test methods in this order:

welcomeNewUserTest()
registerUserTest()
questionaireNewUserTest()

I read an answer somewhere if I name my method with suffix Test, then JUnit would execute them in order as how I order them in code. Apparently, this doesn't work.

Any help? Thanks

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74
  • 3
    If I understood your scenario correctly, that is a bad approach to testing - your tests should be independent from each other. – Dmitry Zaytsev Mar 17 '16 at 16:45
  • @DmitryZaitsev: yeah, I know. Because I write acceptance test, and it is really big, I just try to break it down into small test methods. Try to see if Junit could run them in order – Ragnarsson Mar 17 '16 at 16:47
  • 1
    Possible duplicate of [How to run test methods in specific order in JUnit4?](http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4) – Jared Hooper Mar 17 '16 at 16:56

4 Answers4

8

So for tests like these - where the steps are dependent on each other - you should really execute them as one unit. You should really be doing something like:

@Test
public void registerWelcomeAndQuestionnaireUserTest(){
    // code
    // Register
    // Welcome
    // Questionnaire
}

As @Jeremiah mentions below, there are a handful of unique ways that separate tests can execute unpredictably.

Now that I've said that, here's your solution.

If you want separate tests, you can use @FixMethodOrder and then do it by NAME_ASCENDING. This is the only way I know.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestMethodOrder {

    @Test
    public void testA() {
        System.out.println("first");
    }
    @Test
    public void testC() {
        System.out.println("third");
    }
    @Test
    public void testB() {
        System.out.println("second");
    }
}

will execute:

testA(), testB(), testC()

In your case:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ThisTestsEverything{

    @Test
    public void T1_registerUser(){
        // code
    }

    @Test
    public void T2_welcomeNewUser(){
        // code
    }

    @Test
    public void T3_questionaireNewUser(){
        // code
    }

}
Jared Hooper
  • 881
  • 1
  • 10
  • 31
  • 1
    Thanks. I actually did like you said, execute them as one unit. I just wanted to know if there is an opportunity to do this. And @FixMethodOrder works. It's like what I looked for. Thanks. I didn't know about it – Ragnarsson Mar 17 '16 at 16:57
  • @LouisT Haha well it's great you both changed your approach and that you now have your solution if you ever need it. – Jared Hooper Mar 17 '16 at 16:59
  • 1
    One thing to remember when using the ordering annotations is that it can still misbehave depending on how the tests are executed. For example, if you use SureFire you'll need to define RunOrder in that configuration: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#runOrder Also, if you use an execution tool that can introduce multi-threading into your tests (such as Maven) your tests may again execute out of order. I'm not sure of this point, but it's worth keeping in mind if you start seeing unexpected results. – Jeremiah Mar 20 '16 at 00:53
2

You can not run your test methods in order as how they are written. The point is test must be independent each other. JUnit doesn't encourage dependent tests.

But if you are very want...

There is the @FixMethodOrder annotation. Please, read the following Annotation Type FixMethodOrder

Alexey
  • 1,198
  • 1
  • 15
  • 35
1

You can sort methods with @FixMethodOrder(MethodSorters.NAME_ASCENDING) annotation. Like,

@FixMethodOrder(MethodSorters.DEFAULT)
public class DefaultOrderOfExecutionTest {

private static StringBuilder output = new StringBuilder("");

@Test
public void secondTest() {
    output.append("b");
}

@Test
public void thirdTest() {
    output.append("c");
}

@Test
public void firstTest() {
    output.append("a");
}

@AfterClass
public static void assertOutput() {
    assertEquals(output.toString(), "cab");
}
}

You can perform sorting in 3 ways:

  1. MethodSorters.DEFAULT- This default strategy compares test methods using their hashcodes. In case of a hash collision, the lexicographical order is used.
  2. MethodSorters.JVM- This strategy utilizes the natural JVM ordering – which can be different for each run.
  3. MethodSorters.NAME_ASCENDING- This strategy can be used for running test in their lexicographic order.

For more details please refer:The Order of Tests in JUnit

Kavita Patil
  • 1,784
  • 1
  • 17
  • 30
-1

Use the following command above the class from which you will execute your tests

@FixMethodOrder(MethodSorters.JVM)
public class TestMethodOrder {

    @Test
    public void signup() {
        System.out.println("Signup");
    }

    @Test
    public void login() {
        System.out.println("Login");
    }

    @Test
    public void navigate() {
        System.out.println("Navigate");
    }
}

The MethodSorters.JVM annotation will execute your tests in the way that you have actually written in your file. (Just as the same way that Java code executes, line by line)

  • 1
    This is wrong. The order of methods returned by the JVM can be different each run. In the documentation (https://junit.org/junit4/javadoc/4.12/org/junit/runners/MethodSorters.html#JVM): **public static final MethodSorters JVM**: Leaves the test methods in the order returned by the JVM. Note that the order from the JVM may vary from run to run – Jared Hooper May 06 '19 at 13:58