SELENIUM INTERVIEW QUESTION 6 – All Ways to Exclude/Ignore/Disable a Test Method in TestNG

Refer all Selenium interview question here.

Learn and share to reach it maximum.

When we integrate Selenium with TestNG, we create test methods using @Test annotation provided by TestNG. You can create test methods as many as you want. But sometimes you may need to ignore or disable test to exclude it while running as a suite , method or class level. We will see all ways to ignore a test method in this post.

We have three ways to ignore a test method:-

  1. Using @Ignore annotation of TestNG
  2. By setting “enabled” attribute as false for @Test annotated method
  3. By using “exclude” tag in testng xml

Let’s see every option in details.

Using @Ignore annotation of TestNG:

This is this latest feature provided by TestNG. I have already covered about it here. So I will not repeat it again here.

Ignore Annotation in TestNG

Difference between @Ignore and enabled attribute in TestNG

Ask me in comments if you have any doubt.

By setting “enabled” attribute as false for @Test annotated method:

This is traditional way and can be used if you are using older version of TestNG. “enabled” is an attribute of @Test annotation. Default value is always true to include it in run. You can set it false to exclude a particular test from running.

package TestNGEx;

import org.testng.annotations.Test;

public class EnabledAttribute {
	
	@Test
	public void Method1()
	{
		System.out.println("Will be running");
	}
	
	@Test(enabled=false)
	public void Method2()
	{
		System.out.println("Ignored using enabled as false");
	}

}

Output:-

By using “exclude” tag in testng xml:

Above two ways are good if you are doing at individual method or class level. But when you are running in a suite, it will be really difficult to manage at individual level. You may need to keep enabling or disabling tests. In that case you can use testng xml to manage it externally rather than modifying on method or class level. You can achieve easy scalability and modification if use enabling disabling test using testng xml.

Testng xml:-




	
		
			
				
				
					
				
			
		
	 
 

Output:-

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Leave a Reply

Your email address will not be published. Required fields are marked *