TestNG – Disable or Ignore Tests

When executing TestNG tests, there may be some scenarios where you may have to disable a particular test or a test suite from getting executed.

For example, consider a scenario where a serious bug exists in a feature due to certain tests belonging to certain scenarios that cannot be executed. As the issue has already been identified we may need to disable the said test scenarios from being executed.

1. Skip a Test with @Ignore

TestNG provides @Ignore annotation that is used to ignore or skip the test executions. The @Ignore can be used in 4 places:

  • In a test method
  • In a class
  • In a particular package
  • In a package and all of its child packages

1.1. Test Method Level

Using @Ignore at the method level is functionally equivalent to @Test(enabled=false).

@Ignore
public void someTest() {
      //test code
}

1.2. Test Class Level

Use @Ignore at the class level to ignore all tests within a class.

@Ignore
public class TestcaseSample {
 

}

1.3. At Package Level (and sub-packages)

To ignore all tests in a particular package and all of its sub-packages, we can create package-info.java and add the @Ignore annotation to it. For example, the following will ignore all the tests in package com.howtodoinjava.demo.

@Ignore
package com.howtodoinjava.demo;
 
import org.testng.annotations.Ignore;

2. Disable a Test with @Test( enabled=false )

To disable a test in TestNG, we should set the enabled attribute of the @Test annotation to false. The default attribute value of enabled is true.

@Test( enabled=false )

2.1. Test Method Level

The following test will be disabled and excluded from the test suite.

@Test( enabled=false )
public void someTest() {
      //test code
}

2.2. Test Class Level

We can apply the @Test annotation to the class, as well. If enabled attribute is set for the @Test annotation at the test class level, all the tests inside the class will be disabled.

@Test(enabled = false)
public class IgnoreTestDemo {

  @Test
  public void someTest() {
      //test code
  }
}

3. Excluding Groups

TestNG allows us to include groups as well as exclude them. The simplest way is to tag all such tests with a common name such as – broken.

@Test(groups = {"broken"} )
public void testMethod() {
}

Now we can exclude this group from the run in the test suite file.

<test name="App Tests">
  <groups>
    <run>
      <exclude name="broken"/>
    </run>
  </groups>
</test>

We can also pass the excluded group names in the command line if we can execute specific suite files.

$ java org.testng.TestNG testng1.xml -excludegroups "broken"

4. Demo

In the below test, we have three test methods i.e. testMethodOne(), testMethodTwo() and testMethodThree(). Out of these testMethodTwo() needs to be disabled.

@Test(enabled = true)
public class DisableTestDemo
{
	@Test(enabled = true)
	public void testMethodOne() {
		System.out.println("Test method one.");
	}

	@Test(enabled = false)
	public void testMethodTwo() {
		System.out.println("Test method two.");
	}

	@Test
	public void testMethodThree() {
		System.out.println("Test method three.");
	}
}

The output of the above test run is given below:

[TestNG] Running:  C:\Users\somepath\testng-customsuite.xml

Test method one.
Test method three.

PASSED: testMethodOne
PASSED: testMethodThree

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

As you can see in the previous results, only two methods were executed by TestNG. The method with attribute enabled value as false was ignored from test execution.

By default the attribute value of enabled is true, hence you can see the test method with name testMethodThree() was executed by TestNG even when the attribute value was not specified.

Happy Learning !!

Sourcecode on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial