When executing TestNG tests, there may be some scenarios where you may have to disable a particular test or a set of tests from getting executed.
For example, consider a scenario where a serious bug exists in a feature due to certain tests belonging to certain scenarios that cannot be executed. As the issue has already been identified we may need to disable the said test scenarios from being executed.
1. How to disable test
Disabling a test in TestNG can be achieved by setting the enabled attribute of the @Test
annotation to false
.
@Test( enabled=false )
This will disable the said test method from being executed as part of the test suite. If this attribute is set for the @Test
annotation at the class level, all the tests inside the class will be disabled.
2. Example of disabling tests
In below test, we have three test methods i.e. testMethodOne()
, testMethodTwo()
and testMethodThree()
. Out of these testMethodTwo()
needs to be disabled.
public class DisableTestDemo { @Test(enabled = true) public void testMethodOne() { System.out.println("Test method one."); } @Test(enabled = false) public void testMethodTwo() { System.out.println("Test method two."); } @Test public void testMethodThree() { System.out.println("Test method three."); } }
Output of above test run is given below:
[TestNG] Running: C:\Users\somepath\testng-customsuite.xml Test method one. Test method three. PASSED: testMethodOne PASSED: testMethodThree =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 ===============================================
As you can see in the previous results, only two methods were executed by TestNG. The method with attribute enabled value as false was ignored from test execution.
By default the attribute value of enabled is true, hence you can see the test method with name testMethodThree()
was executed by TestNG even when the attribute value was not specified.
Happy Learning !!