Wednesday, October 5, 2016

TestNG Prioritizing/Sequencing and Skipping a Test Case

In TestNG "Priority" is used to schedule the test cases. When there are multiple test cases, we want to execute test cases in order. Like First we need to execute a test case "Registration" before login.
In order to achive, we use need to add annotation as @Test(priority=??).

If you don't mention the priority, it will take all the test cases as "priority=0" and execute.
If we define priority as "priority=", these test cases will get executed only when all the test cases which don't have any priority as the default priority will be set to "priority=0"

Let's create a new TestNG class 'MultipleTestCases' without setting Priority and execute it without setting.
Note - By default, methods annotated by @Test are executed alphabetically. Take a look over the next topic to see how to prioritize @Test.

package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
  @Test
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test
  public void testFour(){
      System.out.println("This is test four");
  }
}


Update TestNG.xml and execute the test.

<suite name = "Test - Suite">
    <test name="website QA">
        <classes>
            <class name="automationFramework.MultipleTestCases" />
        </classes>
    </test>
</suite>


The output will be like this -


So like I mentioned above, Without setting priority Test cases are executed in alphabetical order.
Now let's set Priority in the above example and run the test again.

 package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
  
  @Test(priority = 0)
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test(priority = 1)
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test(priority=2)
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test(priority = 3)
  public void testFour(){
      System.out.println("This is test four");
  }
}





Skipping Test Case

Sometimes, it happens that our code is not ready and the test case written to test that method/code fails. In such cases, annotation @Test(enabled = false) helps to disable this test case.
If a test method is annotated with @Test(enabled = false), then the test case that is not ready to test is bypassed.
Now, let's see @Test(enabled = false) in action.

To use two or more parameters in a single annotation, separate them with a comma:
@Test(priority = 3, enabled = false)

Let's continue to use 'MutipleTestCases' class and SKIP testcase 1 and testcase 2 and see the output.

package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
   
  @Test(priority = 0)
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test(priority = 1, enabled = false)
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test(priority=2, enabled = false)
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test(priority = 3)
  public void testFour(){
      System.out.println("This is test four");
  }
}


 And the output is -


1 comment:


  1. A nice article here with some useful tips for Testers. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.

    I like to suggest you and your viewers please also check mine and encourage me, Selenium Updated Topics and
    Free selenium tutorials

    ReplyDelete