Hello,
Welcome to Selenium Tutorial, Today we are going to discuss very silent feature of TestNG which allow us to enable or disable our test case based on our requirement.
Why to Disable Selenium Test cases?
Before moving forward you should have Eclipse TestNG setup ready if still not configured then please do Selenium Eclipse setup now.
Once our test suite size will increase day by day then chances are high that you don’t want to execute some test cases.
Ok so let’s consider a scenario that you have written 10 testcases but now you want to execute only 5 testcase because other 5 are working correctly
How to Disable Selenium Test cases?
In TestNG we can achieve this by simply adding enable attribute and can set value to true/false.
@Test(enable=false)
If test case enable as false them while running test cases TestRunner simply will ignore this testcase and will only run testcase whose enable is set to true.
Note- By default @Test is set to enable
Scenario – I have 3 test case but I want to execute only 2 test case.
Precondition- TestNG should be installed
Program for – Disable Selenium Test cases
package demotestcase; import org.testng.annotations.Test; public class TestEnableTC { @Test public void testLoginApp(){ System.out.println("User is able to login successfully"); } @Test(enabled=false) public void testRegisteruser(){ System.out.println("User is able to register successfully"); } @Test public void testLogoutApp(){ System.out.println("User is able to logout successfully"); } }
Lets check the output
Above program is executing only 2 testcases which has been passed. One test case in not executed because that test case we have enable =false.
Hope this post helpful for you. Keep sharing with your friends. Comment below if you finding any issue while working with Selenium or TestNG.
Have a nice day 🙂
Like Facebook Page for more updates-Learn-Automation facebook page
Join Selenium Group for discussion and queries- Learn-Automation group
Gaurav Khurana says
That so easy.. Easiest tutorial till date i think. Just a single parameter explained in the best way
Good for unit testing we can use this when we need to avoid commenting the code
Thanks
sam says
Hi,
@Test(enabled=false)
or
@Test(enable=false)
which among them?
Mukesh Otwani says
Hi Sam
@Test(enabled=false) is correct one
Mukesh says
Hi Mukesh,
I got a case..For suppose i have 100 test cases,in that 100 i want to execute only 20 test cases.Is there any way to skip the 80 test cases,rather using enable=false,for all the 80 test cases.. Please provide the solution.Btw million Thanks for your work man
.
Mukesh Otwani says
Hi Mukesh,
Grouping of test case will work for you.
Create group of test cases then decide which group to run which do not has to run via XML file(testng.xml).