How to Ignore a Class in TestNG?


TestNG supports multiple ways to ignore all @Test execution. Based on requirement, user can ignore a complete test without executing it at all.

TestNG supports following level to ignore all @Test:

  • In a class

  • In a particular package

  • In a package and all of it’s child package

User has to use @Ignore annotation at required level to disable tests. The @Ignore annotation takes higher priority than individual @Test annotation.

To disable all @Test in a class just write @Ignore before class name. It will disable all @Test present inside the class.

In this article, we will illustrate how to disable entire test in a class.

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG class − NewTestngClass.

  • Step 2: Write 2 different @Test method in the class − NewTestngClass and place @Ignore annotation just above class name as shown in programming code section.

  • Step 3: Now create the testNG.xml as given below to run the TestNG classes.

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

Example

The following code for common TestNG class − NewTestngClass:

src/ NewTestngClass.java

import org.testng.SkipException;
import org.testng.annotations.Test;

@Ignore
public class NewTestngClass {

    @Test(enabled=false)
    public void testcase1(){
        System.out.println("Testcase 1 - Not executed");
    }
    @Test
    public void testcase2(){
        System.out.println("Testcase 2 - skip exception example");
        throw new SkipException("Skipping this exception");
    }

    @Test
    public void testcase3(){
        boolean DataAvailable=false;
        System.out.println("Test Case3 - 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 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

===============================================
Suite1
Total tests run: 0, Passes: 0, Failures: 0, Skips: 0
===============================================

Updated on: 17-Aug-2023

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements