1
votes

In the testNG.xml file, I have 10+ test classes (within a test-suite tag) for regression testing. I, then, have ordered the automated tests of several test classes in a particular sequence by using the priority=xxx in @Test annotation. The priority values within a particular class are sequential - but each test class has different ranges. For example:

testClass1 : values are from 1-10 
testClass2 : values are from 11-23    
testClass3 : values are from 31-38 
. 
. 
. 
lastTestClass : values are from 10201-10215

The purpose of this is to have a particular sequence in which the 10+ test-classes are executed. There is one test-class that I need to be executed towards the end of the test execution - so, the priorities in that class range from 10201-10215. However, this particular test-class gets tested right after the 1st class with priorities from 1-10.

2

2 Answers

0
votes

Instead of using priority, I would recommend you to use dependencies. They will run your tests in a strict order, never running the depended before the dependent, even if you are running in parallel.

I understand you have the different ranges in different classes, so in dependOnMethods you would have to specify the root of the test you are referencing:

@Test(  description = "Values are from 1-10")
public void values_1_10() {
    someTest();
}

@Test(  description = "Values are from 21-23",
        dependsOnMethods = { "com.project.test.RangeToTen.values_1_10" })
public void values_21_23() {
    someTest();
}


If you have more than one test in each range then you can use dependsOnGroups:

@Test(  enabled = true,
        description = "Values are from 1-10")
public void values_1_10_A() {
    someTest();
}

@Test(  enabled = true,
        description = "Values are from 1-10")
public void values_1_10_B() {
    someTest();
}

@Test(  enabled = true,
        description = "Values are from 1-10",
        dependsOnGroups = { "group_1_10" })
public void values_21_23_A() {
    someTest();
}

@Test(  enabled = true,
        description = "Values are from 1-10",
        dependsOnGroups = { "group_1_10" })
public void values_21_23_B() {
    someTest();
}


You can also do the same with more options from the testng.xml: https://testng.org/doc/documentation-main.html#dependencies-in-xml

Another option you have is to use the "preserve order": https://www.seleniumeasy.com/testng-tutorials/preserve-order-in-testng

But as Anton mention, that could bring you troubles if you ever want to run in parallel, so I recommend you using dependencies.

0
votes

Designing your tests to be run in specific order is a bad practice. You might want to run tests in parallel in future - and having dependencies on order will stop you from doing that.

Consider using TestNG listeners instead:

It looks like you are trying to implement some kind of tearDown process after tests. If this is the case - you can implement ITestListener and use onFinish method to run some code after all of your tests were executed.

Also, this TestNG annotation might work for your case:

org.testng.annotations.AfterSuite