Home > OS >  Junit 5 order tests with @Tag from some classes
Junit 5 order tests with @Tag from some classes

Time:09-14

I have some classes. I all classes, I have some tests. Example:

public class testpage1(){

@test
@Tag("Regression")
void test1(){
   system.out.println("Test1");
}

void test2(){
   system.out.println("Test2");
}

   }

public class testpage2(){

@test
@Tag("Regression")
void test3(){
   system.out.println("Test3");
 }

void test4(){
   system.out.println("Test4");
   }

}

public class testpage3(){

@test
@Tag("Regression")
void test5(){
   system.out.println("Test5");
   }

void test6(){
   system.out.println("Test6");
  }

}

I have other class to run :

@RunWith(value = JunitPlatform.class)
@IncludeTags(value = {"Regression"})

When I run this class, The test run without any order,

How can I order the test to run in order that I want?

CodePudding user response:

with Junit normally the test methods will be executed in the order of their names, sorted in ascending order.

or you can specify an order using annotation @Order @Order(1) @Order(2) ...

CodePudding user response:

jUnit5 allows you to set the tests execution order with @Order() annotation.
So, you can add these annotations in your code as following:

public class testpage1(){

@test
@Order(1)
@Tag("Regression")
void test1(){
   system.out.println("Test1");
}

void test2(){
   system.out.println("Test2");
}

   }

public class testpage2(){

@test
@Order(2)
@Tag("Regression")
void test3(){
   system.out.println("Test3");
 }

void test4(){
   system.out.println("Test4");
   }

}

public class testpage3(){

@test
@Order(3)
@Tag("Regression")
void test5(){
   system.out.println("Test5");
   }

void test6(){
   system.out.println("Test6");
  }

}

This should make these tests executed correspondingly

CodePudding user response:

Thank, I have all the page @order for all the tests , per test, I forgot to note it at the first.

  • Related