skip to content

What does alwaysRun = true do on a TestNG configuration method, and why do teams put it on @AfterMethod and @AfterClass hooks?

level: middleimportance: should knowfreq 50%

answer

  1. default is false
  2. teardown after a failure
  3. group filtering drops setup
  4. guard against half-built setup

basics

~20 s

TestNG documents alwaysRun = true two ways: an @After hook still runs after earlier methods failed or were skipped, and a @Before hook other than @BeforeGroups runs whatever its groups. Teams use it so teardown survives a skip cascade.

solid answer

~40 s

`alwaysRun` defaults to `false` on every configuration annotation. On `@After*` hooks, TestNG documents it as running the hook even if one or more methods invoked earlier failed or were skipped. Without it, a failed `@BeforeMethod` or `@BeforeClass` skips the matching `@AfterMethod` and `@AfterClass` too, so files, sessions or test data the setup half-created are never cleaned up. On `@Before*` hooks, except `@BeforeGroups`, it is documented as running the hook regardless of its groups: when a run includes only some groups, a setup hook outside them is otherwise filtered out and the included tests run without their setup. The catch is that an `alwaysRun` teardown also runs after its setup threw, so it must null-check what it releases. The same-named attribute on `@Test` is a different feature, a soft dependency.

code

java · 32 lines
java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ExportTest {
  private Path workDir;

  @BeforeMethod(alwaysRun = true)
  public void createWorkDir() throws IOException {
    workDir = Files.createTempDirectory("export");
  }

  @Test(groups = "smoke")
  public void writesCsv() { }

  @AfterMethod(alwaysRun = true)
  public void deleteWorkDir() throws IOException {
    if (workDir == null) {
      return; // setup failed before assigning it
    }
    try (Stream<Path> paths = Files.walk(workDir)) {
      paths.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }
    workDir = null;
  }
}

go deeper

for a junior

Know that alwaysRun defaults to false and that teams put alwaysRun = true on @AfterMethod and @AfterClass so cleanup happens after a failure.

for a middle

Explain both documented meanings, teardown after failed or skipped methods and setup regardless of groups, and how group filtering drops a hook outside the included groups.

for a senior

Show the failure modes: leaked resources from skipped teardown, NullPointerExceptions from teardown that assumed setup finished, and smoke runs that silently lose their setup.

for a principal

Expect to argue for a suite-wide convention, such as every teardown marked alwaysRun and null-safe, and for code review that catches setup hooks left outside group filters.

## The attribute and its default Every TestNG configuration annotation, from `@BeforeSuite` down to `@AfterMethod`, has a boolean attribute `alwaysRun`, and its default is `false`. The javadoc on the annotations in TestNG 7.12 describes it in two halves: - **For before methods** (`@BeforeSuite`, `@BeforeTest`, `@BeforeClass` and `@BeforeMethod`, but not `@BeforeGroups`): if set to true, the method runs regardless of what groups it belongs to. - **For after methods** (`@AfterSuite`, `@AfterClass` and the rest): if set to true, the method runs even if one or more methods invoked previously failed or were skipped. The two halves solve two different everyday problems. ## Teardown that survives a skip cascade When a TestNG configuration method fails, the default suite setting `configfailurepolicy="skip"` skips the methods that depended on it. That includes the **teardown hooks**. If `@BeforeMethod` throws after it has already created a temporary directory, started a server or inserted test rows, a plain `@AfterMethod` is skipped and nothing is released. Over a long run, those leftovers pile up and later tests fail for reasons unrelated to their own code. Marking teardown `alwaysRun = true` lets it run anyway: 1. setup throws part-way through; 2. TestNG records a configuration failure and skips the dependent test; 3. the `alwaysRun` teardown still runs and releases whatever exists. This is why `@AfterMethod(alwaysRun = true)` and `@AfterClass(alwaysRun = true)` are near-standard in UI and API suites written on TestNG. ## The teardown must expect half-built setup Running after a failure means running when the setup **did not finish**. The teardown cannot assume its fields are populated: - check each resource for `null` before releasing it; - release resources independently, so one failing release does not stop the next; - reset fields after releasing them, because by default one class instance serves every test method of the class. A teardown that dereferences a field its setup never assigned throws a `NullPointerException`, which TestNG reports as a second configuration failure sitting beside the real setup error, easy to mistake for it. ## Setup that survives group filtering TestNG can run a subset of a suite by **groups**, through the suite file or the `-groups` option of its command-line runner. Configuration methods take part in that filtering: a hook that does not belong to an included group is excluded. A class whose `@Test` methods carry `groups = "smoke"` but whose `@BeforeMethod` carries no groups will, in a smoke-only run, execute its tests **without the setup**, typically failing with a null field. Two fixes exist: | Fix | Effect | |---|---| | `@BeforeMethod(alwaysRun = true)` | the hook is included whatever groups the run selects | | put the hook in the group, e.g. `groups = "smoke"` | the hook is included only for runs that select that group | A related default helps here: `inheritGroups` is `true`, so a configuration method belongs to the groups named in a **class-level** `@Test(groups = ...)` annotation. Groups declared only on individual test methods are not inherited by the hooks. ## What the 7.12 source does beyond the javadoc Reading the TestNG 7.12 source shows the flag applied more broadly than the two-half description. The group filter includes any configuration method marked `alwaysRun`, teardown included, and the configuration-failure check lets any `alwaysRun` hook bypass the skip, setup hooks included. Treat that as an explanation for behaviour you may observe, and write code against the documented meaning. ## Not the same as @Test(alwaysRun) `@Test` has an attribute with the same name, but it means something else: a test method that depends on other methods still runs when they fail, which TestNG calls a **soft dependency**. That belongs to dependencies between tests, not to configuration hooks. In an interview, say which annotation you mean. ## Common mistakes - Assuming TestNG always runs teardown, as a `finally` block would; by default a skip cascade skips it. - Believing `alwaysRun` on teardown changes the test's verdict; the test stays skipped and the setup failure stays reported. - Writing an `alwaysRun` teardown that assumes setup completed. - Filtering a run by groups and forgetting that hooks outside the included groups are dropped.

  • A TestNG run selects only the smoke group and every test in one class fails with a NullPointerException on a field set in @BeforeMethod. What happened?
    The `@BeforeMethod` belongs to no included group, so TestNG's group filtering excluded it and the tests ran without setup. Mark the hook `alwaysRun = true`, add it to the group, or move the groups to a class-level `@Test(groups = ...)` so the hook inherits them.
  • Why can an @AfterMethod(alwaysRun = true) hook in TestNG produce a second configuration failure that obscures the first?
    It runs even when `@BeforeMethod` threw before assigning its fields. If it dereferences one of them, it throws too, and the report now shows a teardown error beside the real setup error, which readers often chase first. Null-check each resource and release them independently.

  • TestNG always runs @AfterMethod, so alwaysRun on it is redundant.
  • alwaysRun = true on teardown turns the skipped test into a pass.
  • An alwaysRun teardown can assume its setup completed.
  • Configuration hooks without groups always run when a run filters by groups.
  • alwaysRun on a configuration method and on @Test mean the same thing.

Assumes TestNG 7.12; the javadoc meaning of alwaysRun and the broader source behaviour were checked against the 7.12 annotations and invoker code.