A TestNG class carries a class-level `@Test(enabled = false)`, yet its methods with their own `@Test` still run; why, and what reliably disables the whole class?
answer
- class annotation turns public methods into tests
- a method annotation replaces it
- only some attributes merge
- a separate annotation for disabling
basics
~20 sA method's own @Test replaces the class-level one, and enabled is not inherited, so those methods default to enabled. Class-level enabled = false only disables unannotated public methods; @Ignore on the class disables every test method in it.
solid answer
~40 sA class-level `@Test` turns every public, non-synthetic method of the class that returns `void` into a test and supplies their attributes. A method that has its own `@Test` gets a fresh annotation instead: TestNG joins only `groups`, `dependsOnGroups` and `dependsOnMethods` from the class and inherits `description` and `dataProviderClass` when the method leaves them unset, while `enabled`, `priority`, `timeOut` and the rest come from the method, so `enabled` is back to its default `true`. To switch off the whole class use `org.testng.annotations.Ignore` on it, which disables methods with their own `@Test` too and, per its Javadoc, child classes; on a package in `package-info.java` it covers sub-packages. Disabled methods are not run and are not reported as SKIPPED.
code
java · 10 linesimport org.testng.annotations.Test;
@Test(enabled = false)
public class LegacyCheckoutTest {
public void cartTotalsMatch() {} // disabled: uses the class-level @Test
@Test(groups = "smoke")
public void checkoutCompletes() {} // runs: its own @Test sets enabled = true
}go deeper
Know that @Test can go on a class to make its public methods tests, and that enabled = false switches a test off.
Explain which methods a class-level @Test collects and which attributes a method-level @Test joins or inherits from it.
Diagnose why class-level enabled = false leaves annotated methods running, fix it with @Ignore, and spot helpers in annotated base classes being run as tests.
Set a disabling convention: @Ignore with a reason and a tracked ticket, never a silent enabled flag, and report ignored counts beside skips.
## What a class-level `@Test` does TestNG's `@Test` targets both methods and types. Put on a class, its documented effect is to make *"all the public methods of this class"* test methods even if they are not annotated. In the 7.12 source the rule is a little narrower than that sentence: - the method must be **public** and not compiler-generated (synthetic); - it must return `void`, unless the suite allows return values (`allow-return-values`, default `false`); a method with a return value is skipped with a log line; - a method that carries a configuration annotation such as a `@Before`/`@After` hook stays a configuration method only; - TestNG looks the class annotation up through superclasses, so a base class annotated with `@Test` makes the public methods of its subclasses tests as well. That last point is where the trap with shared base classes comes from: a public helper such as `login()` in a class covered by a class-level `@Test` is collected and run as a test. ## How class and method annotations combine When a method also has its own `@Test`, TestNG does **not** merge the two annotations attribute by attribute. It builds the method's annotation from the method's own values and pulls in only a few things from the class: | Attribute | Taken from | |---|---| | `groups`, `dependsOnGroups`, `dependsOnMethods` | Method values joined with the class values | | `description`, `dataProviderClass` | The method, falling back to the class when unset | | `enabled`, `priority`, `timeOut`, `invocationCount`, `expectedExceptions` and the rest | The method only, with the annotation defaults | So `@Test(enabled = false)` on the class reaches only the public methods that have **no** annotation of their own. A method written as `@Test(groups = "smoke")` has `enabled` at its default `true` and runs. TestNG's own `EnableTest` encodes exactly this: in its sample class `B`, annotated `@Test(enabled = false)` at class level, the plain method `testB` is not invoked while `testB2` and `testB3`, which have their own `@Test`, are. ## `@Ignore`: the switch for a whole class or package TestNG ships `org.testng.annotations.Ignore` (added in 6.13), described in its Javadoc as an *"Alternative of @Test(enabled=false)"*: 1. On a **method**, it is functionally equivalent to `@Test(enabled = false)`. 2. On a **class**, it disables all test methods of the class, including ones with their own `@Test`; the documentation says it has higher priority than individual method annotations. Its Javadoc adds that ignoring a class also ignores tests from child classes. 3. On a **package**, written in `package-info.java`, it ignores all tests in the package and its sub-packages. `@Ignore` takes an optional `value` string, which TestNG prefixes to the method's description, so the reason is visible in reports. ## How disabled tests show up A method that is disabled, by `enabled = false` or by `@Ignore`, is never invoked. It is **not** a skip: it does not enter the skipped results that a failed dependency or configuration method produces, and TestNG's JUnit-style XML report counts excluded methods, disabled ones among them, in a separate `ignored` attribute. This matters when a team reads "zero skipped" as "everything ran". ## Example ```java import org.testng.annotations.Test; @Test(enabled = false) public class LegacyCheckoutTest { public void cartTotalsMatch() {} // disabled: uses the class-level @Test @Test(groups = "smoke") public void checkoutCompletes() {} // runs: its own @Test sets enabled = true } ``` Replacing the class annotation with `@Ignore("Checkout v1 retired")` and keeping `@Test` on each method disables both. ## Practical guidance - Use a class-level `@Test` for shared attributes like a group or a description, not for switching tests off; remember that only groups, dependencies, the description and the data-provider class reach methods that carry their own annotation. - When you inherit a suite, search for class-level `enabled = false` and check which methods it actually reaches before trusting it. - Prefer `@Ignore` with a reason when disabling a class, because it cannot be undone by a method annotation. - Be wary of a class-level `@Test` on a shared base class; every public helper in it and in its subclasses becomes a test. - JUnit Jupiter's equivalent of the disabling switch is `@Disabled`; it is a different annotation in a different package and TestNG ignores it.
- What happens to a public helper method in a class covered by a TestNG class-level @Test?If it is public, non-synthetic and returns `void`, TestNG collects it as a test and runs it. A helper with a return value is skipped unless the suite sets `allow-return-values`, and a method with a configuration annotation stays a configuration method. The lookup walks superclasses, so a base class with a class-level `@Test` spreads this to subclasses.
- Is a TestNG method disabled with enabled = false reported as skipped?No. It is never invoked and never enters the skipped results, which come from causes such as failed dependencies, failed configuration or `SkipException`. TestNG's JUnit-style XML report counts excluded methods, disabled ones among them, in a separate `ignored` attribute, so read that number too.
- A method's own @Test merges every attribute from the class-level @Test.
- @Test(enabled = false) on a class always disables every method in it.
- A class-level @Test only collects methods whose names start with test.
- Disabled TestNG methods are counted among the skipped tests.
- TestNG has no @Ignore of its own and only honours JUnit's.