Ignore Test in TestNG (enabled=false)

In this blog post, we are going to learn about one of the important capability of TestNG Framework, and that is IgnoreTest property.

We might across situations when we do not want a particular test method to be executed, along with the other test methods. And it is possible that the particular test method might be used in multiple TestNG suites, so removing/ commenting the call to that test method from all the places would be daunting and boring task.

TestNG provides a mechanism, using that we can make a particular test method disabled, so that the piece of code inside that method would not be executed while running the TestNG suite.

Let us understand the capability with working example.

Step 1. Create a TestNG class file, and name it as IgnoreTest.class.

package com;

import org.testng.annotations.Test;

public class IgnoreTestTestNG {

@Test(enabled = false)
public void ignoreMethod1() {
System.out.println(“Inside Test Method 1.”);
}

@Test
public void testMethod1() {
System.out.println(“Inside Test Method 1.”);
}

}

Step 2. Create/ Edit TestNG.xml, as below.

 <?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite”>
<test name=”TestSuiteDemo”>
<classes>
<class name=”com.IgnoreTestTestNG” />
</classes>
</test>
</suite>

Step 3. Run the TestNG suite, and notice the output on console.

Output:

Inside Test Method 1.

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

If you notice the above output, the ignoreMethod1() which is marked as enable=false, is not called during the suite execution.

So, this is how we can ignore any test method while execution, especially when the test method is still under development.

That is it from this blogpost, hope it helps you!

Cheers 🙂

Advertisements