Hello Folks,
TestNG provides an attribute called “enabled” which can be used to ignore a test in a running suite. If we use this attribute at class level with @Test annotation, it will ignore all tests of that class.
Now TestNG is providing a new annotation called “@Ignore”, which can be used to ignore :
- A specific @Test annotated method.
- All @Test annotated methods of a class and its child class
- All @Test annotated methods of a package and its sub package.
Ignoring/disabling a specific @Test annotated method:
We can ignore a particular @Test annotated method setting “enabled” as false at @Test annotation. Now the same we can achieve using @Ignore attribute. Example is shown below:
package IgnoreExamples; import org.testng.annotations.Ignore; import org.testng.annotations.Test; public class IgnoreSpecificTest { @Test public void activeTest() { System.out.println("I am an active test."); } // This test method is ignored/disabled using @Ignore attribute. The same can be achieved using enabled=false. @Test @Ignore public void inactiveTest() { System.out.println("I am an inactive test."); } }
Output:
[java]
[RemoteTestNG] detected TestNG version 6.14.2
I am an active test.
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[/java]
Ignoring/disabling all @Test annotated methods of a class and its subclass:
Now we can easily ignore all @Test annotated method of a Class and its subclass using @Ignore annotation. However same can be achieved using @Test(enabled= false) annotation at class level. An example is below:
Super Class:
package IgnoreExamples; import org.testng.annotations.Ignore; import org.testng.annotations.Test; @Ignore public class IgnoreBaseClass { @Test public void method1() { System.out.println("Method1"); } @Test() public void method2() { System.out.println("Method2"); } }
Sub Class:
package IgnoreExamples; import org.testng.annotations.Test; public class IgnoreChildClass extends IgnoreBaseClass { @Test public void method3() { System.out.println("Method3"); } @Test public void method4() { System.out.println("Method4"); } }
You can notice I put @Ignore annotation at super class. It will disable test from base class and its child class. NOte here that this feature is available in TestNG Release 6.14.3 onwards.
Output:
[java]
[RemoteTestNG] detected TestNG version 6.14.3
===============================================
Default test
Tests run: 0, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
[/java]
Ignoring/disabling all @Test annotated methods of a package and its subpackages:
Like method and class, we can not put @Ignore annotation on top of package statement of a class as Package does not belong to class. So to ignore all @Test annotated method of a package and its subpackage, we need to mention @Ignore annotation in “package-info.java”. You can create a “package-info.java” file while creating a package by selecting a checkbox or you can create manually and place inside package.
You need to place @Ignore annotation on top of package statement as shown below:
@Ignore package IgnoreExamples; import org.testng.annotations.Ignore;
It will ignore all @Test annotated methods of package named “IgnoreExamples”.
Hope you must have learnt new thing of TestNG today.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium