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. Disabling a Test

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 )

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

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

2. 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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.