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?
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>.
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.
For example, @beforeSuite, @afterSuite, @beforeTest, @afterTest, @beforeClass, @afterClass, @beforeMethod, @afterMethod. Step 6 − Now create the testNG.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With