Enabling and Disabling TestNG Test
In some scenarios you may need NOT to Execute/Run some of the tests or set of tests from getting executed in a particular class. For example, consider a scenario where a serious bug exists in application 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.
Disabling a test can be achieved in 2 ways. One is from testng.xml configuration file and the second is from the Test annotation property/attribute (i.e. enabled). These will disable the said test method from being executed as part of the test suite. If this attribute is set to false for the Test annotation at class level, all the public methods inside the class will be disabled.
1.Using Test annotation enabled property/attribute:
Will see sample program for how to disable and enable particular tests in a class using enabled property of Test annotation. If the enabled=true then the test will execute and if enabled=false then the test will NOT execute.
import org.testng.annotations.Test; public class DisableAndEnableTests { /** * This test will be executed as the enabled property is true. */ @Test(enabled=true) public void firstTest() { System.out.println("In First Test"); } /** * This test will NOT be executed as the enabled property is false. */ @Test(enabled=false) public void secondTest() { System.out.println("In Second Test"); } /** * This test will NOT be executed as the enabled property is false. */ @Test(enabled=false) public void thirdTest() { System.out.println("In Third Test"); } }
Now Execute the above code using below xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="DisableAndEnableTests"/> </classes> </test> </suite>
Output for the above program is:
2.Using Include and Exclude tags in the testng.xml file:
Will see sample program for how to disable and enable particular tests in a class using include and exclude tags from testng.xml file.
import org.testng.annotations.Test; public class DisableAndEnableTests { @Test public void firstTest() { System.out.println("In First Test"); } @Test public void secondTest() { System.out.println("In Second Test"); } @Test public void thirdTest() { System.out.println("In Third Test"); } }
Now Execute the above code using below xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="DisableAndEnableTests"> <methods> <include name="firstTest"/> <exclude name="secondTest"/> <exclude name="thirdTest"/> </methods> </class> </classes> </test> </suite>
Output for the above program is:
In the above programs we have three tests in the class. But after execution we are seeing only one test is getting executed(i.e. firstTest) and remaining tests (i.e. secondTest and thirdTest) are skipped from the execution.
This way we can disable or enable some of the tests from the execution according to our requirement.