Sometimes we want to ignore a TestNG test method, there are many ways to achieve this.
Table of Contents
TestNG @Test enable parameter
We can disable or ignore a test method by adding enabled=false
to @Test
annotation. Let’s look at a simple example of ignoring a test method using this.
package com.journaldev.utils;
import org.testng.annotations.Test;
public class TestNGExample {
@Test
public void foo() {
System.out.println("Running foo test");
}
@Test(enabled=false)
public void bar() {
System.out.println("Running bar test");
}
@Test(groups="zoo")
public void zoo() {
System.out.println("Running zoo test");
}
@Test(groups="test")
public void base() {
System.out.println("Running base test");
}
}
When above test class is executed, we get following output confirming that bar() method didn’t get executed.
[RemoteTestNG] detected TestNG version 6.14.3
Running base test
Running foo test
Running zoo test
PASSED: base
PASSED: foo
PASSED: zoo
This method has a huge demerit, every time we want to disable a method we need to do code change and compile our classes again.
TestNG Disable Tests in XML Suite
TestNG XML suite files provide a lot of options. We can exclude test methods as well as groups from the execution. Let’s create a simple TestNG XML suite file where we will exclude foo
method. We will also exclude zoo
from the test execution.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Excludes Test Suite" guice-stage="DEVELOPMENT">
<groups>
<run>
<exclude name="zoo"></exclude>
</run>
</groups>
<test thread-count="5" name="TestNG Excludes Test" verbose="2">
<groups>
<run>
<exclude name="zoo"></exclude>
</run>
</groups>
<classes>
<class name="com.journaldev.utils.TestNGExample">
<methods>
<exclude name="foo"></exclude>
</methods>
</class>
</classes>
</test>
</suite>
Run above XML file as TestNG Suite and it will produce following output.
Notice that we can exclude groups from execution at suite level as well as test level. Above XML code is showing both ways, however, in this case, it’s redundant and we can remove any one of those configurations.