How to re-run ignored tests in TestNG

Posted on

When running tests in TestNG, sometimes we mark some tests with @Test(enabled = false) to ignore them temporarily. However, later we may want to re-run these ignored tests dynamically without modifying the test code.

In this blog, we will see how to re-run ignored tests in TestNG programmatically using a custom IAnnotationTransformer.

Why Re-run Ignored Tests?

By default, TestNG does not execute ignored tests. However, we might want to:

  • Run ignored tests dynamically based on conditions
  • Avoid modifying test files manually
  • Execute tests selectively in CI/CD pipelines

Steps to re-run ignored tests

Step 1: Create a Test Class with Ignored Test Cases

First, let's create a test class with some ignored tests:

Java
Copy
import org.testng.annotations.Test;

public class SampleTest {

    @Test
    public void test1() {
        System.out.println("Test 1 is running!");
    }

    @Test(enabled = false)  // Ignored test
    public void ignoredTest() {
        System.out.println("Ignored Test is running!");
    }
}

Here, ignoredTest() will not execute normally since it is marked with @Test(enabled = false).

Step 2: Create an IAnnotationTransformer to Re-run Ignored Tests

To override the ignored status, we use IAnnotationTransformer:

Java
Copy
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class IgnoreTransformer implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        // Set ignored tests to enabled
        if (!annotation.isEnabled()) {
            annotation.setEnabled(true);
        }
    }
}

Step 3: Configure TestNG to Use IgnoreTransformer


Now, update testng.xml to include the IgnoreTransformer:

Java
Copy
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MySuite">
    <listeners>
        <listener class-name="IgnoreTransformer"/>
    </listeners>
    <test name="MyTest">
        <classes>
            <class name="SampleTest"/>
        </classes>
    </test>
</suite>

Step 4: Run testng.xml


Run testng.xml or execute it programmatically using this Java class:

Java
Copy
import org.testng.TestNG;
import java.util.Arrays;

public class RunTestNG {
    public static void main(String[] args) {
        TestNG testng = new TestNG();
        testng.setTestSuites(Arrays.asList("testng.xml"));
        testng.run();
    }
}

Expected Output


When you execute the program, you will see that the ignoredTest() now runs successfully:

Test 1 is running!
Ignored Test is running! // Previously ignored, now executed
===============================================
MySuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================