← Back to libraryQuestion 277 of 468
🔬TestNG & JUnitBeginner

Disabling and Ignoring Tests

📌 Definition:

You can temporarily exclude tests: TestNG uses @Test(enabled = false); JUnit 4 uses @Ignore and JUnit 5 uses @Disabled (optionally with a reason). Disabled tests are reported as skipped, not run.

📖 Detailed Explanation:

Disabling is for tests that are broken, flaky, or gated on unfinished features — better than deleting or commenting out, because the test remains visible and reported as skipped with (ideally) a reason. TestNG: @Test(enabled = false). JUnit 5: @Disabled("reason") on a method or class; JUnit 5 also has conditional disabling (@EnabledOnOs, @EnabledIf, @DisabledIfEnvironmentVariable) to skip based on environment. Always add a reason and a tracking ticket so disabled tests don't rot. A suite full of silently disabled tests is a red flag — track and re-enable them.

🔑 Key Points:
  • TestNG @Test(enabled=false); JUnit5 @Disabled('reason')
  • Reported as SKIPPED, kept visible (better than commenting out)
  • JUnit5 conditional disabling: @EnabledOnOs/@DisabledIf...
  • Always add a reason + ticket; audit disabled tests
🌍 Real-World Example:

A test for an unreleased feature is @Disabled("JIRA-123: pending payment API") so it stays in the report as skipped with context, and is re-enabled when the API ships — rather than deleted and forgotten.

🎯 Scenario-Based Interview Question:

A colleague comments out a flaky test to make the build green. Why is @Disabled/@Test(enabled=false) with a reason better?