Is using testNg priority good?

516    Asked by Anil Mer in QA Testing , Asked on Apr 27, 2022

 I'm assigning priority to each test method in a specific class under my automation project.


My senior reviewed the script I wrote and mentioned to me that do not use priority instead use the xml to order the execution of the methods.

I wanted to know if using priority for each method is a good approach or not.


I had an issue before regarding the order in which the methods were getting executed. That's why I started using the priority technique.

If someone has suggestions please help me to understand it. I'm new to programming language and automation


Answered by Al German

Using priority to drive order of execution is perfectly fine, but the question is why do you want to execute test cases in a certain order? As a general rule test cases should be atomic and should not be dependent on other test cases. In such cases do you worry about the order of execution? I rather don't pay much attention to order of execution unless it's an explicit requirement. e.g. If my login test case fails then I don't want to execute the rest of the test cases in suite as those will likely fail. In such a situation, I would use hard dependency to drive.


@Test 
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
You can also have methods that depend on entire groups:
@Test(groups = { "init" })
public void serverStartedOk() {}
@Test(groups = { "init" })
public void init Environment() {}
@Test(dependsOnGroups = { "init.*" })
public void method1() {}
You can also use soft dependency to drive execution order. By "alwaysRun=true" in your @Test annotation.
Know more at the TestNG site

Your Answer

Interviews

Parent Categories