0

Suppose there are 10 tests in a test class . I want to run them in a particular order. I can use priority attribute to set the priority of test cases. Is there any other way to set priority of test cases.

Aruna14
  • 11
  • possibly duplicate https://stackoverflow.com/questions/40105037/order-of-execution-in-testng-without-using-priority – SeleniumUser Oct 29 '19 at 18:13
  • 1
    You need to think long and hard about why you actually need to do this. Best practice is to not run automation in a particular order because it can hide issues. Tests should not be dependent on each other, each one should be isolated. – JeffC Oct 29 '19 at 19:49

2 Answers2

0

Scenario : Test classes have one or more @Test methods defined with priorities import org.testng.annotations.Test;

public class MyTests1 {

@Test(priority = 1)
public void test1() {
    System.out.println("test1 from " + getClass().getSimpleName() + " class");
}

@Test(priority = 2)
public void test2() {
    System.out.println("test2 from " + getClass().getSimpleName() + " class");
}

}

Also work this way too.... @Test public void Test1() {

}

@Test (dependsOnMethods={"Test1"}) public void Test2() {

}

@Test (dependsOnMethods={"Test2"}) public void Test3() {

}

0

TestNG uses Priority to "suggest" an order of execution, based on the priority you give to the test. This is not the same as setting an order.

A strict way to establish an order on certain tests is using Test Dependencies. If TestA has priority=1 and TestB has priority=2, but A depends on B, then TestNG will run B first, ignoring the priority, otherwise A would fail.

A combination of the two practices, will give you something similar to an "order of execution".

I would correct what JeffC says: He is right to say it's a good practice to have your tests as independent of each other as possible. But this is always true ONLY in unit testing.

For example: You might have a regression suite like:

@Test (priority=2)
public void validateAddingMilkToShoppingCart(){
  putMilkInCart();
  validateMilkIsInCart();
}

@Test (priority=1, dependsOnMethods = {"validateAddingMilkToShoppingCart"})
public void validateRemovingMilkToShoppingCart(){
  verifyMilkIsInCart();
  removeMilkFromCart();
  validateCartIsEmpty();
}

In this scenario, "validateRemovingMilkToShoppingCart" might have a higher priority because the Sprint is working on emptying the shopping cart, or because it had a bug associated recently. But you should only run that test, if you can put the milk in the cart in the first place, otherwise, you'll spend time and resources in running a test that you already know it will fail based on a previous test. Plus, by doing this, you'll report will look cleaner showing a Skip if the feature couldn't be tested because of a bug in a previous test.

Hope this answers your question.

Rodrigo Vaamonde
  • 463
  • 1
  • 6
  • 23