How to run test methods in order with Junit

Ragnarsson

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

Jared Hooper

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
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically run JUnit test methods

From Dev

JUnit run the methods in the same order as in file

From Dev

JUnit run the methods in the same order as in file

From Dev

How to test methods efficiently with JUnit

From Dev

How to test methods efficiently with JUnit

From Dev

How to run JUnit test in Eclipse

From Dev

How to run Junit test classes in a particular order in Maven without creating suits?

From Dev

how to write junit test case for inner methods?

From Dev

Generating reports for junit when test methods run individually

From Dev

Generating reports for junit when test methods run individually

From Dev

How to run Spring Shell scripts in a JUnit test

From Dev

How to run a single JUnit test method in Eclipse?

From Dev

How to run jUnit test in specific folder by gradle?

From Dev

How to run all modified JUnit test classes?

From Dev

How to run a specific junit test in ant with parameter?

From Dev

JUnit test methods are not executing

From Dev

How to run JUnit 5 and JUnit 4 test suites in Gradle?

From Dev

Confusing execution order of JUnit methods

From Dev

Confusing execution order of JUnit methods

From Dev

How do I reuse JUnit test methods for different testing contexts?

From Dev

How do I reuse JUnit test methods for different testing contexts?

From Dev

JUnit Test Cases for Generalized Methods

From Dev

Execute order for test suite in junit

From Dev

How to run a simple JUnit4 test in Android Studio 1.1?

From Dev

How to run only one junit test case in maven?

From Dev

Eclipse: How to run a single inherited JUnit test method

From Dev

How do I run JUnit 4.11 test cases with SBT?

From Dev

How do I run a junit test only after the build?

From Dev

How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

Related Related

  1. 1

    Dynamically run JUnit test methods

  2. 2

    JUnit run the methods in the same order as in file

  3. 3

    JUnit run the methods in the same order as in file

  4. 4

    How to test methods efficiently with JUnit

  5. 5

    How to test methods efficiently with JUnit

  6. 6

    How to run JUnit test in Eclipse

  7. 7

    How to run Junit test classes in a particular order in Maven without creating suits?

  8. 8

    how to write junit test case for inner methods?

  9. 9

    Generating reports for junit when test methods run individually

  10. 10

    Generating reports for junit when test methods run individually

  11. 11

    How to run Spring Shell scripts in a JUnit test

  12. 12

    How to run a single JUnit test method in Eclipse?

  13. 13

    How to run jUnit test in specific folder by gradle?

  14. 14

    How to run all modified JUnit test classes?

  15. 15

    How to run a specific junit test in ant with parameter?

  16. 16

    JUnit test methods are not executing

  17. 17

    How to run JUnit 5 and JUnit 4 test suites in Gradle?

  18. 18

    Confusing execution order of JUnit methods

  19. 19

    Confusing execution order of JUnit methods

  20. 20

    How do I reuse JUnit test methods for different testing contexts?

  21. 21

    How do I reuse JUnit test methods for different testing contexts?

  22. 22

    JUnit Test Cases for Generalized Methods

  23. 23

    Execute order for test suite in junit

  24. 24

    How to run a simple JUnit4 test in Android Studio 1.1?

  25. 25

    How to run only one junit test case in maven?

  26. 26

    Eclipse: How to run a single inherited JUnit test method

  27. 27

    How do I run JUnit 4.11 test cases with SBT?

  28. 28

    How do I run a junit test only after the build?

  29. 29

    How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

HotTag

Archive