Whenever we specify priority
and dependsOnMethods
on a @Test
annotated method, the order of execution of test methods is not according
All independent methods (that do not have @dependsOnMethods dependency) will be executed first. Then methods with dependency will be executed. If there is ambiguity in execution order even after this ordering, priority comes into picture.
This is the ordering scheme:
Now all ambiguity is resolved since no two methods can have the same name.
Can someone help me to understand the below sequence of execution?
public class Testpract {
@Test
public void setup()
{
System.out.println("Setup");
}
@Test(priority=1)
public void gotopage()
{
System.out.println("gottopage");
}
@Test(priority=2, dependsOnMethods="gotopage")
public void verifytitle()
{
System.out.println("verifytitle");
}
@Test(dependsOnMethods="login")
public void verifyimage()
{
System.out.println("verifyimage");
}
@Test(dependsOnMethods="verifyhomepage", priority =10)
public void login()
{
System.out.println("login");
}
@Test(priority=7)
public void verifyhomepage()
{
System.out.println("verifyhomepage");
}
output is
Setup
gottopage
verifyhomepage
PASSED: setup
PASSED: gotopage
PASSED: verifyhomepage
@Test
public void setup()
{
System.out.println("Setup");
}
@Test(priority=1)
public void gotopage()
{
System.out.println("gottopage");
}
@Test(priority=2, dependsOnMethods="gotopage")
public void verifytitle()
{
System.out.println("verifytitle");
}
@Test(dependsOnMethods="login", priority =3)
public void verifyimage()
{
System.out.println("verifyimage");
}
@Test(dependsOnMethods="verifyhomepage", priority =10)
public void login()
{
System.out.println("login");
}
@Test(priority=7)
public void verifyhomepage()
{
System.out.println("verifyhomepage");
}
output is
Setup
gottopage
verifytitle
verifyhomepage
login
verifyimage
PASSED: setup
PASSED: gotopage
PASSED: verifytitle
PASSED: verifyhomepage
PASSED: login
PASSED: verifyimage
I've encountered the same problem today.
At first, I was using only priority
for my tests, but then I needed to add dependsOnMethods
as well.
Initially i've added the dependsOnMethods
only to some of my @Test
methods.
As a result the execution order of my tests has scrambled.
I've read a lot of articles and discussions related to this topic and it turned out, that the using of priority
and dependsOnMethods
attributes togeter brings a lot of uncertainty into the whole picture and the behaviour of TestNG will never be predictable and well defined in this situation.
My solution was to add the dependsOnMethods
to ALL of my test methods, while I kept the priority
also for ALL of the mehtods.
Now their execution order is back to normal and at the same time I benefit from the capabilities of the dependsOnMethods
.
i.e. The first failing test method in the chain, causes all subsequent test methods to be skipped and show correct in the reports.
Here is fragment from my tests class:
@Test(priority = 2, dependsOnMethods= {"Meganav_Point_C1_and_Click_C3"})
public void Click_product_in_Category_result_page() throws Throwable {
Grid.clickProduct(1, 1);
}
@Test(priority = 3, dependsOnMethods= {"Click_product_in_Category_result_page"})
public void PDP_setQty() throws Throwable {
ProductDetailsPage.setQty(2);
}
@Test(priority = 4, dependsOnMethods= {"PDP_setQty"}, alwaysRun= true)
public void PDP_click_Add_To_Basket() throws Throwable {
ProductDetailsPage.addToBasket();
}
Hope this helps.
Regards, Veselin Petrov