Open In App

How to Ignore a Class in TestNG?

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In TestNG, ignoring a class can be done using various methods like the @Test(enabled = false) annotation, excluding it in the testng.xml file, or using groups to control test execution. These techniques give you flexibility in managing your test suite, ensuring that only relevant tests are executed. By mastering these methods, you can maintain an efficient and focused testing process.

Methods to Ignore a Class in TestNG

Following are the methods to ignore a class in TestNG

1. Using the @Test annotation with enabled attribute

If you want to skip specific tests within a class, you can set the enabled attribute of the @Test annotation to false. This will prevent the test methods from running.

Example of @Test annotation with enabled attribute

Following are the steps to ignore a class in TestNG using the @Test annotation with enabled attribute.

Step 1: Create a Java Class and write the below code

Java
package testNG;

import org.testng.annotations.Test;

@Test(enabled = false)
public class IgnoredClass {
    public void testMethod1() {
        System.out.println("Test Case 1");
    }

    public void testMethod2() {
    	System.out.println("Test Case 1");
    }
}

Step 2: Create a TestNG.xml file and write the below code

XML
<?xml version = "1.0" encoding = "UTF-8"?>

<suite name="Suite">
    <test name="Test Suite">
        <classes>
           
            <!-- This class will be ignored -->
            <class name="testNG.IgnoredClass">

            </class>
        </classes>
    </test>
</suite>

Step 3: Run the Test Class

To run this Test Class

  • Right-click on the IgnoredClass in your IDE.
  • Select Run As > TestNG Test.

Explanation:

  1. Import Statement: This imports the @Test annotation from the TestNG library, which is used to denote that the methods in the class are test methods.
  2. Class Annotation: This annotation is applied to the class IgnoredClass. In TestNG, applying the @Test annotation at the class level means that all methods within this class are considered as test methods. The enabled = false attribute in the @Test annotation indicates that the test class should be disabled. This means none of the test methods within this class will be executed by TestNG.
  3. Test Methods: These are the test methods within the IgnoredClass. They contain simple print statements for demonstration.

Output

Ignored_Class
Output

2. Using @Ignore Annotation

Although TestNG does not have an @Ignore annotation, if you are mixing TestNG and JUnit tests, you might use @Ignore for JUnit tests. This will not affect TestNG tests but is useful if you have JUnit tests in the same project.

Example of @Ignore Annotation

Following are the steps to ignore a class in TestNG using the @Test annotation with enabled attribute.

Step 1: Crate a Java Class and write the below code

Java
package testNG;

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

@Ignore
public class IgnoredClass {
	@Test
    public void testMethod1() {
        System.out.println("Test Case 1");
    }
	@Test
    public void testMethod2() {
    	System.out.println("Test Case 1");
    }
}

Step 2: Create a TestNG.xml file and write the below code

XML
<?xml version = "1.0" encoding = "UTF-8"?>

<suite name="Suite">
    <test name="Test Suite">
        <classes>
           
            <!-- This class will be ignored -->
            <class name="testNG.IgnoredClass">

            </class>
        </classes>
    </test>
</suite>

Step 3: Run the Test Class

To run this Test Class

  • Right-click on the IgnoredClass in your IDE.
  • Select Run As > TestNG Test.

Explanation

  1. Import Statement: This imports the @Ignore annotation from the TestNG library.
  2. Class Annotation: The @Ignore annotation is applied to the IgnoredClass. This annotation tells TestNG to skip or ignore the entire class during test execution.
  3. Test Methods: Each method is annotated with @Test, indicating that they are TestNG test methods.

Output:

Ignored

Conclusion

In TestNG, you can skip tests in a class using the @Test(enabled = false) annotation, which disables all test methods within the class. Alternatively, although TestNG does not natively support the @Ignore annotation, you can use it if your project combines TestNG and JUnit. Both methods allow you to efficiently manage which tests are executed, helping you maintain a focused testing process.


Next Article

Similar Reads