How to force end an entire test suite from the BeforeSuite annotation if a condition is met in TestNG?

TestNGRest AssuredDynamic Programming

TestNG supports multiple ways to skip or ignore a @Test execution. Based on requirement, a user can skip a complete test without executing it at all from BeforeSuite, if a condition is met. If the condition meets at the time of execution, it skips the running of @Test methods.

Conditional Skip is a proper way to force-end an entire test suite, if a condition is met in @BeforeSuite method.

  • Conditional Skip − User can have a condition check. If the condition is met, it will throw a SkipException and skip the rest of the code.

In this article, we will demonstrate how to force-end an entire test suite based on a condition.

Approach/Algorithm to solve this problem:

  • Step 1 − Create a TestNG class, NewTestngClass.

  • Step 2 − Write two different @Test methods in the class and one @BeforeSuite, NewTestngClass, as shown in the programming code section below.

    1st and 2nd @Test Method − It will skip execution based on @BeforeSuite condition.

    @BeforeTest Method − It is conditional skip. The code checks whether the DataAvailable parameter is True or False. If it is False, it throws SkipException and skips the test. But, if DataAvailable is True, it won't throw the SkipException and continue the execution. Make sure SkipException is not caught in the catchcatch block. Otherwise, it will throw the

    SkipException

    and continue the execution.


  • Step 3 − Create the testNG.xml as given below to run the TestNG classes.

  • Step 4 − Finally, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Program Code

Use the following code for the common TestNG class, NewTestngClass

src/ NewTestngClass.java

import org.testng.SkipException;
import org.testng.annotations.*;
public class NewTestngClass {
   @Test()
   public void testcase1(){
      System.out.println("Testcase 1 - executed");
   }
   @Test()
   public void testcase2(){
      System.out.println("Testcase 2 - executed");
   }
   @BeforeSuite
   public void beforesuite(){
      boolean DataAvailable=false;
      System.out.println("BeforeSuite - Conditional Skip");
      if(!DataAvailable)
      throw new SkipException("Skipping this exception");
      System.out.println("Executed Successfully");
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

BeforeSuite - Conditional Skip
===============================================
Default Suite
Total tests run: 2, Passes: 0, Failures: 0, Skips: 2
Configuration Failures: 0, Skips: 1
===============================================
Process finished with exit code 0
Test ignored.
Test ignored.
raja
Updated on 12-Jan-2022 13:07:22

Advertisements