Enable and Disable Test in TestNG
Introduction
To enable or disable a test we can use the enabled attribute. To enable a test we can use the flag “enabled=true”. We can disable the test in TestNG by using the attribute “enabled=false” for the @Test method. We will see how to disable a test with a code example.
Disable a Test
public class TestNGDisableTest { @Test(enabled=false) public void disabledMethod() { System.out.println("In disabledMethod.. This never runs..!"); } }
Note that for the enabled method the attribute switch (enabled=true) is by default. So you may skip the switch for enabled method.
One of the most common error that is usually made is adding a dependency on a disabled test. In the next section, we will see what happens when we add the dependency on a disabled test.
import org.testng.annotations.Test; public class TestNGDisableTest { @Test(enabled=false) public void disabledMethod() { System.out.println("In disabledMethod"); } @Test(dependsOnMethods={"disabledMethod"}) public void dependentMethod() { System.out.println("In dependentMethod"); } }
If we run the above test class we would get exception as below.
org.testng.TestNGException:
com.testingdocs.testng.sample.TestNGDisableTest.dependentMethod() is depending on method public void com.testingdocs.testng.sample.TestNGDisableTest.disabledMethod(), which is not annotated with @Test or not included.
TestNG Tutorials on this website can be found at:
https://www.testingdocs.com/testng-framework-tutorial/
For more details on the TestNG Framework, visit the official website of TestNG at: