How to disable a TestNG test based on a condition?
TestNG stands for the Test Next Generation, it is an open-source and automated framework that is used for testing, it is used for the testing of codes in Java programming language, Cedric Beust.
TestNG includes lots of test categories such as integration, functional and unit testing, etc. and it allows the users to write the test cases and also organize them to generate the test reports in detail.
How to disable a TestNG test based on a condition?
We can use a popular method known as @Test annotation and its "enabled" attribute which will allow us to disable a test case in TestNG, but first, let's understand the algorithm behind this method.
Algorithm for conditional skipping in TestNG
- Step 1: The first step is to create a TestNG class.
- Step 2: In this step, write two separate @Test methods which will be executed (testAlwaysDisabled, testConditionally) and the TestNG framework will print output.
- Step 3: Next, create a testNG.xml file to run the testNG classes.
- Step 4: run the testNG.xml file.
Example to Disable TestNG on a Condition
Step 1: Create a ConditionalTest.xml file
We can look at a simple TestNG example, here we are using a class name "ConditionalTest" with some simple code in it (code is written in Java language), and save the file will be located inside src / ConditionalTest.java, following is the code for this file:
src/ConditionalTest.java
import org.testng.annotations.Test;
public class ConditionalTest {
// Condition method
public boolean shouldRunTest() {
String env = System.getenv("RUN_TEST");
return "true".equalsIgnoreCase(env);
}
@Test(enabled = false)
public void testAlwaysDisabled() {
System.out.println("This test is always disabled");
}
@Test(enabled = true)
public void testConditionally() {
if (!shouldRunTest()) {
throw new SkipException("Skipping the test due to condition");
}
System.out.println("This test is running because the condition was met!");
}
}
Step 2: Configure testng.xml file
The testng.xml configuration is a file which will be used for running the testNG test cases and organizing the test cases, we will need to simply add the class name along with some other simple code:
<?xml version = "1.0" encoding = "UTF-8"?>
<suite name="Suite">
<test name="Test Suite">
<classes>
<!-- This class will be ignored -->
<class name="testNG.ConditionalTest">
</class>
</classes>
</test>
</suite>
Step 3: Set the Environment Variable
This step is added due to example condition. In order to disable test taken in example we need add to configure the environment variable. The method shouldRunTest()
checks the environment variable RUN_TEST
. If RUN_TEST
is not set, or if it is set to anything other than "true"
, the test will be skipped, and you won't see any message printed.
You can set the environment variable in the run configuration:
- Go to
Run
>Run Configurations...
.- Select your TestNG configuration.
- Go to the
Environment
tab and add a new variableRUN_TEST
with the valuefalse
.

Step 4: Running the Correct Test Class
To run this Test Class
- Right-click on the
ConditionalTest
class in your IDE. - Select
Run As
>TestNG Test
.
Output
After running the test class, you got the following on console and on log.
Output on Console

Log of SkipException
When a test is skipped using SkipException
, TestNG does not print the exception message to the standard output by default. Instead, it logs that the test was skipped.

Conclusion
It is simple to disable a TestNG test case on the basis of a given condition, we can simply use java language and write some if conditions upon which the test cases are supposed to run or disable. it is important to understand how we can disable a particular TestNG test case upon receiving a particular condition because it can be necessary to disable some test cases from time to time.