</>

Technology

TestNG

Difficulty

Beginner

Interview Question

What is @Test(enabled=false) in TestNG and when should you use it?

Answer

@Test(enabled=false) in TestNG

enabled=false disables a @Test method — TestNG completely skips it (not even counted as skipped in most reporters).

Basic Usage

Java
@Test(enabled = false) // disabled — will not run
public void brokenFeatureTest() {
    // Known defect: JIRA-456 — payment gateway returns 500
    // Re-enable when JIRA-456 is fixed
    driver.findElement(By.id("payBtn")).click();
    Assert.assertEquals(getPaymentStatus(), "Success");
}

@Test // enabled = true is default
public void workingTest() {
    Assert.assertTrue(driver.getTitle().contains("Home"));
}

When to Use enabled=false

Java
// 1. Known production bug — test would always fail, blocking CI
@Test(enabled = false, description = "Disabled: JIRA-789 — search returns wrong results")
public void searchFilterTest() { ... }

// 2. Feature in development — not ready yet
@Test(enabled = false, description = "WIP: Checkout flow not implemented")
public void checkoutTest() { ... }

// 3. Test is environment-specific — not applicable to current environment
@Test(enabled = false, description = "Only runs on staging — disabled for prod")
public void dataResetTest() { ... }

Conditional Enable/Disable (Dynamic)

Better than hardcoding enabled=false — use IAnnotationTransformer to control at runtime:

Java
public class ConditionalEnableTransformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,
                          Constructor testConstructor, Method testMethod) {

        String env = System.getProperty("env", "staging");

        // Disable prod-data-reset tests on production
        if (testMethod != null && testMethod.isAnnotationPresent(ProdOnly.class)
                && !env.equals("production")) {
            annotation.setEnabled(false);
        }
    }
}

enabled=false vs Groups Exclusion

ApproachVisibleFlexibleReport
enabled=falseIn codeNo — needs code changeNot shown
groups="broken" + excludeIn code + XMLYes — XML/Maven flagShown as excluded
@Test(enabled=false)In codeNoNot shown
Comment out testHiddenWorstNot shown

Best Practice for Disabled Tests

Always add documentation:

Java
@Test(
    enabled = false,
    description = "DISABLED: JIRA-1234 — dropdown broken on Chrome 120. Re-enable after hotfix."
)
public void dropdownTest() { ... }

This way, any team member can search for enabled = false in the codebase and find all disabled tests with their reasons.

Follow AutomateQA

Related Topics