How to run test methods in order with Junit

后端 未结 4 1791
一生所求
一生所求 2021-01-12 04:34

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(         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 05:04

    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
        }
    
    }
    

提交回复
热议问题