How to Ignore a Class in TestNG?
Last Updated :
03 Sep, 2024
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:
- 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.
- 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.
- Test Methods: These are the test methods within the IgnoredClass. They contain simple print statements for demonstration.
Output
Output2. 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
- Import Statement: This imports the @Ignore annotation from the TestNG library.
- Class Annotation: The @Ignore annotation is applied to the IgnoredClass. This annotation tells TestNG to skip or ignore the entire class during test execution.
- Test Methods: Each method is annotated with @Test, indicating that they are TestNG test methods.
Output:
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.
Similar Reads
How to Ignore a File in Git?
In this article, we are going to discuss how to ignore files in Git. Essentially, you can ignore files by specifying them in the ".gitignore" file. Moreover, you can also ignore directories using this method. Throughout this article, we'll cover everything from creating a GitHub account to setting u
3 min read
How to run multiple test classes in TestNG?
TestNG makes it easy to run multiple test classes in one go, which helps test different parts of an application efficiently. By running multiple classes together, you save time and ensure more thorough testing. You can run these classes using a TestNG XML file, directly from Eclipse, or through the
2 min read
How to run TestNG from command line?
Running TestNG from the command line is a powerful technique for managing your automated tests efficiently. TestNG is a popular testing framework that provides various features for running and configuring tests. While IDE-based execution is common, using the command line offers greater flexibility a
4 min read
How to Use Soft Asserts in TestNG?
TestNG is an open source automation framework that is used to perform software testing more easily, it provides various annotations and is used to perform parallel testing and produce the test reports. TestNG provides asserts to check the actual results with the expected results, Asserts are necessa
2 min read
How to Automate TestNG in Selenium?
TestNG is an open-source test automation framework for Java, designed to make the process of testing more efficient and effective. Standing for "Next Generation," TestNG offers advanced features like annotations, data-driven testing, and parallel execution. When combined with Selenium, it provides a
4 min read
How to Create Test Suite in TestNG?
In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups. Table of Content What is a Te
11 min read
How to set priority to the test cases in TestNG?
TestNG â Test Next Generation â is a testing framework based on the JUnit and NUnit, but with added functionality. It is designed to cover all categories of tests: The test types include unit, functional, end-to-end and integration tests. When it comes to organizing the test cases, the usage of Test
5 min read
How to use Regex in TestNG?
In this article, we will see how to use Regex in TestNG. In TestNG, we can use Regular Expressions for two purposes: To Include Test CasesTo Exclude Test Cases To include test cases we use the include the keyword in the testng.xml configuration file.To Exclude test cases we use the exclude keyword i
5 min read
How to run single test in a TestNG class from IntelliJ IDE?
TestNG is a widely-used testing framework in the Java programming language. Often, when developing or debugging your code, you may need to run a specific test method rather than executing the entire test suite. Fortunately, IntelliJ IDEA makes this task straightforward with its integrated support fo
3 min read
How to skip TestNG test at runtime?
With TestNG, there are situations where some tests are needed to be skipped during runtime based on certain conditions. Dynamic skipping of tests provides more flexibility while executing the test at run-time, depending upon some external factor like configuration, system states, or the outcome of s
3 min read