Priority in TestNG with multiple classes
I'm facing with the following problem: I created two classes which include @Tests with priority attribute:
@Test( priority = 1 )
public void testA1() {
System.out.println("testA1");
}
@Test( priority = 2 )
public void testA2() {
System.out.println("testA2");
}
@Test( priority = 3 )
public void testA3() {
System.out.println("testA3");
}
... and ...
@Test( priority = 1 )
public void testB1() {
System.out.println("testB1");
}
@Test( priority = 2 )
public void testB2() {
System.out.println("testB2");
}
@Test( priority = 3 )
public void testB3() {
System.out.println("testB3");
}
I put both classes under one test in testng.xml but when I run the test, it will order my @Tests based on the priorities from both classes:
testA1
testB1
testA2
testB2
testA3
testB3
I'm expecting the following result:
testA1
testA2
testA3
testB1
testB2
testB3
My question is that how can I prevent to order my @Tests based on both classes and run @Tests only from one class at the same time?
In your suite xml use group-by-instances="true"
Sample, where TestClass1 and TestClass2 has the same content as yours
<suite thread-count="2" verbose="10" name="testSuite" parallel="tests">
<test verbose="2" name="MytestCase" group-by-instances="true">
<classes>
<class name="com.crazytests.dataproviderissue.TestClass1" />
<class name="com.crazytests.dataproviderissue.TestClass2" />
</classes>
</test>
</suite>
I get the output
testA1
testA2
testA3
testB1
testB2
testB3
The most correct way is to use dependsOnMethods.
Priority levels are global for test (don't mix with test-methods which are annotated with @Test). In other words:
when testng runs test (from <test>
tag) it groups methods by priorities and then run it. In your case both testA1 and testB1 have priority=1, so will be executed at the beginning.
You can just provide @Test(testName="test1") / @Test(testName="test2")
at the top of each class, and the priorities will be automatically grouped per class. Of course you keep the existing annotations.