TestNG Tutorials 67 : Difference Between @Ignore & enabled Attribute of @Test Method in TestNG

Hello Folks,

In last post, we have learnt about new annotation of TestNG , named @Ignore which is used to ignore @Test annotated methods at different levels i.e. Method, Class and Package.

We have another attribute of Test annotation named “enabled” which is also used to serve the same purpose. So what are the differences between both the ways and when to use? We will going to see about them in this post.

At Test method level:

At this level, @Ignore and “enabled=false” both are same. Both will do the same task i.e. will ignore the Test. So at this level, you can use either.

At Class level:

You will start getting the advantages of @Ignore here. Consider, You need to ignore all Test method of a class and its subclass. You have many ways to achieve that. You can exclude class from testng xml or you can go to each Test method and put “enabled” as false. Your task will be easier if you have @Test annotation at class level as you can set “enabled” as false at class level itself. But it will be overridden if Test method of class has @Test annotation.  See an example below:

package IgnoreExamples;


import org.testng.annotations.Test;

/*
 * At class level we are setting enabled as false but at Test method level, it is overriding the behavior. 
 * So, it will be difficult to do modification at each method level. 
 */
@Test(enabled=false)
public class TestAtClassLevel {

	
	
	@Test
	public void method1()
	{
		System.out.println("Method1");
	}
	
	@Test
	public void method2()
	{
		System.out.println("Method2");
	}
}



Output:

[java]
[RemoteTestNG] detected TestNG version 6.14.3
Method1
Method2
PASSED: method1
PASSED: method2

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

[/java]

At this case, @Ignore will help. Put @Ignore at class level all Test method will be ignored as it takes higher priority. The @Ignore annotation has a higher priority than individual @Test method annotations. When @Ignore is placed on a class, all the tests in that class and its subclass will be disabled.

At Package level:

You can not use @Test annotation at package level which demolish the chances of setting “enabled” attribute as false. You have other way around to remove package from testng xml. The best way will be usage of @Ignore attribute which you can see in previous post.

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 *