Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of execution of tests in TestNG

How to customize the order of execution of tests in TestNG?

For example:

public class Test1 {   @Test   public void test1() {       System.out.println("test1");   }    @Test   public void test2() {       System.out.println("test2");   }    @Test   public void test3() {       System.out.println("test3");   } } 

In the above suite, the order of execution of tests is arbitrary. For one execution the output may be:

test1 test3 test2 

How do I execute the tests in the order in which they've been written?

like image 720
Badri Avatar asked Apr 19 '10 17:04

Badri


People also ask

What is the correct order in TestNG xml?

The hierarchy in the testng xml file is very simple to understand. Very first tag is the Suite tag<suite>, under that it is the Test tag<test> and then the Class tag<classes>.

How do you run TestNG tests sequentially?

parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified. parallel="tests": TestNG will run all the methods in the same tag in the same thread, but each tag will be in a separate thread.

What is the order of TestNG annotation?

For example, @beforeSuite, @afterSuite, @beforeTest, @afterTest, @beforeClass, @afterClass, @beforeMethod, @afterMethod. Step 6 − Now create the testNG.


1 Answers

This will work.

@Test(priority=1) public void Test1() {  }  @Test(priority=2) public void Test2() {  }  @Test(priority=3) public void Test3() {  } 

priority encourages execution order but does not guarantee the previous priority level has completed. test3 could start before test2 completes. If a guarantee is needed, then declare a dependency.

Unlike the solutions which declare dependencies, tests which use priority will execute even if one test fails. This problem with dependencies can be worked around with @Test(...alwaysRun = true...) according to documentation.

like image 119
user1927494 Avatar answered Sep 23 '22 04:09

user1927494