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:

Output:

[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
===============================================

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

 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: