skip to content

Configuration Hook Order

TestNG runs setup and teardown at five scopes, from @BeforeSuite down to @BeforeMethod, and by default a failed hook skips the tests it guards. Interviewers ask for the order and the skip.

on this pageshow

explore

questions

5

In TestNG, in what order do the @BeforeSuite, @BeforeTest, @BeforeClass and @BeforeMethod hooks and their @After pairs run?

level: juniorimportance: must knowfreq 65%

answer

  1. widest scope first
  2. suite, test tag, class, method
  3. @BeforeTest is not per test method
  4. after hooks unwind in reverse

basics

~10 s

TestNG nests its hooks from widest to narrowest: @BeforeSuite, @BeforeTest (once per <test> element of testng.xml), @BeforeClass, @BeforeMethod, then the test method; the @After hooks unwind in reverse, @AfterMethod first and @AfterSuite last.

solid answer

~40 s

TestNG runs setup from the widest scope inward: `@BeforeSuite` once for the whole suite, `@BeforeTest` once per `<test>` element of `testng.xml`, `@BeforeClass` before the first test method of a class, and `@BeforeMethod` before every test method invocation. Teardown mirrors it: `@AfterMethod` after each invocation, then `@AfterClass`, `@AfterTest` and finally `@AfterSuite`. The trap is `@BeforeTest`: it is tied to the `<test>` tag of the suite file, not to a `@Test` method. `@BeforeGroups` sits inside this nesting, firing just before the first test method of its group. Because TestNG by default runs all of a class's test methods on one instance, `@BeforeClass` need not be static, unlike JUnit Jupiter's `@BeforeAll`.

code

java · 24 lines
java
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class HookOrderTest {
  @BeforeSuite  public void beforeSuite()  { System.out.println("beforeSuite"); }
  @BeforeTest   public void beforeTest()   { System.out.println("beforeTest"); }
  @BeforeClass  public void beforeClass()  { System.out.println("beforeClass"); }
  @BeforeMethod public void beforeMethod() { System.out.println("  beforeMethod"); }

  @Test public void first()  { System.out.println("    first"); }
  @Test public void second() { System.out.println("    second"); }

  @AfterMethod  public void afterMethod()  { System.out.println("  afterMethod"); }
  @AfterClass   public void afterClass()   { System.out.println("afterClass"); }
  @AfterTest    public void afterTest()    { System.out.println("afterTest"); }
  @AfterSuite   public void afterSuite()   { System.out.println("afterSuite"); }
}

go deeper

for a junior

Recite the ladder without hesitating: suite, test tag, class, method on the way in, and the mirror image on the way out. Say clearly that @BeforeTest belongs to the <test> element of testng.xml.

for a middle

Explain frequency, not just order: once per suite, once per <test> element, once per class instance, once per invocation including each data-provider row, and where a lazily fired @BeforeGroups lands.

for a senior

Tie each scope to the state it should own, and point out the bugs that come from putting per-invocation state in @BeforeClass when one instance serves every test method of the class.

for a principal

Expect to discuss which scope a team allows for expensive shared setup, since suite and test-tag hooks trade speed for coupling across every class in that slice of the run.

## What a configuration method is TestNG calls its setup and teardown hooks **configuration methods**. Each one is an ordinary public method carrying one of the `@Before*` or `@After*` annotations from `org.testng.annotations`. The annotation decides two things: **how often** the method runs and **which piece of the run** it wraps. TestNG has five nested scopes, plus a group scope that cuts across them. | Annotation pair | Runs | Wraps | |---|---|---| | `@BeforeSuite` / `@AfterSuite` | once per suite | everything in the `<suite>` | | `@BeforeTest` / `@AfterTest` | once per `<test>` element | all classes listed inside that `<test>` tag | | `@BeforeClass` / `@AfterClass` | once per class instance | all test methods of the class | | `@BeforeMethod` / `@AfterMethod` | once per test method invocation | a single invocation of a `@Test` method | | `@BeforeGroups` / `@AfterGroups` | once per listed group | the first to the last method of that group | ## The full run order For a suite with one `<test>` element holding one class with two test methods, TestNG runs: 1. `@BeforeSuite` 2. `@BeforeTest` 3. `@BeforeClass` 4. `@BeforeMethod`, the first test method, `@AfterMethod` 5. `@BeforeMethod`, the second test method, `@AfterMethod` 6. `@AfterClass` 7. `@AfterTest` 8. `@AfterSuite` The rule to remember: **setup goes from the widest scope inward, teardown comes back out in reverse**. Each `@After` hook closes the scope its matching `@Before` hook opened. ## @BeforeTest means a <test> tag, not a test method This is the most common misreading in interviews. In TestNG vocabulary: - a **test method** is a method annotated `@Test`; - a **test** is a `<test>` element inside the `testng.xml` suite file, which groups classes, packages or methods; - `@BeforeTest` runs before any test method belonging to the classes inside that `<test>` tag, and `@AfterTest` after all of them have run. So if one suite file lists a class in two different `<test>` elements, that class's `@BeforeTest` method runs once for each element. If a `<test>` element holds three classes that each declare a `@BeforeTest` method, all three run before the first test method of that `<test>`. Teams use `@BeforeTest` for setup shared by one slice of a suite, for example one target environment or one browser configuration. ## Where @BeforeGroups slots in `@BeforeGroups` is not a fixed rung on the ladder. TestNG documents it as running shortly before the first test method that belongs to one of its groups is invoked, and `@AfterGroups` shortly after the last one. In the 7.12 source it is triggered from inside the invocation of that first test method: after the `@BeforeClass` of the class holding it and before that method's `@BeforeMethod`. Group membership and filtering are a separate subject; for ordering, treat group hooks as lazy, fired by the first and last member of the group. ## Per-invocation, not per-method `@BeforeMethod` and `@AfterMethod` wrap each **invocation**, not each method declaration. A test method fed six rows by a data provider gets six `@BeforeMethod` calls. That is usually what you want: every invocation starts from fresh per-method state. ## Which scope suits which setup Choosing the scope is choosing how much work is shared and how much state leaks between tests: - **Suite** for expensive, read-only preparation every class needs, such as loading configuration or checking that the environment answers. - **`<test>` element** for setup that differs per slice of the suite, such as a target environment or browser named by that slice's parameters. - **Class** for state shared by the methods of one class, accepting that one test can disturb the next. - **Method** for isolation: every invocation starts clean, at the cost of repeating the work. ## How it compares with JUnit Jupiter - JUnit Jupiter's class-level and method-level hooks are `@BeforeAll` and `@BeforeEach`; TestNG's are `@BeforeClass` and `@BeforeMethod`. - Jupiter has no counterpart of `@BeforeTest`, because the `<test>` element is a TestNG suite-file concept. - TestNG by default creates **one instance per test class** and runs every test method on it, so a non-static `@BeforeClass` method is normal; Jupiter normally requires `@BeforeAll` to be static. ## Mistakes interviewers listen for - Saying `@BeforeTest` runs before every test method; that is `@BeforeMethod`. - Saying the `@After` hooks run in the same order as the `@Before` hooks; they unwind in reverse. - Treating `@BeforeSuite` as per class; it runs once for the whole suite, whichever class declares it. - Carrying JUnit Jupiter's static rule over to TestNG's `@BeforeClass`.

  • If one TestNG suite file lists the same class inside two <test> elements, how many times do its @BeforeSuite and @BeforeTest methods run?
    `@BeforeTest` runs once per `<test>` element, so twice here, with `@AfterTest` closing each. `@BeforeSuite` runs once for the whole suite and `@AfterSuite` once at the end, however many `<test>` elements mention the class.
  • Why can a TestNG @BeforeClass method be an instance method when JUnit Jupiter normally wants @BeforeAll to be static?
    TestNG by default creates one instance of a test class and runs all its test methods on it, so it has an instance to call `@BeforeClass` on before the first test method. Jupiter by default creates a fresh instance per test method, so a once-per-class hook normally has no instance and must be static.
  • In TestNG, how many times does @BeforeMethod run for a test method that a data provider feeds five rows?
    Five times. `@BeforeMethod` and `@AfterMethod` wrap each invocation, and every data-provider row is a separate invocation of the test method, so each row starts from freshly prepared per-method state.

  • @BeforeTest runs before every @Test method in the class.
  • The @After hooks run in the same order as the @Before hooks.
  • @BeforeSuite runs again before each test class in the suite.
  • A TestNG @BeforeClass method must be static, like JUnit Jupiter's @BeforeAll.
  • @BeforeMethod runs once per class rather than once per test invocation.
open as a page

In a TestNG Selenium-Java suite, the @BeforeClass login hook throws and all 40 tests in that class report SKIPPED instead of FAILED; why, and how do you diagnose it?

level: seniorimportance: must knowfreq 55%

basics

~20 s

TestNG treats a failed configuration method as a broken precondition: the hook is reported as a configuration failure and every test depending on it is SKIPPED. Diagnosis starts at the hook's exception, which each skipped result also carries.

open as a page

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%

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.

open as a page

In a TestNG @AfterMethod hook, how do you find out which test method just ran and whether it passed, failed or was skipped?

level: middleimportance: should knowfreq 42%

basics

~10 s

Declare an ITestResult parameter on the @AfterMethod method: TestNG injects the result of the test method that just ran, so getStatus(), getMethod() and getThrowable() give the outcome, the method and any exception.

open as a page

In TestNG, when a test class extends a base class and both declare @BeforeClass and @BeforeMethod hooks, in what order do they run?

level: middleimportance: should knowfreq 48%

basics

~10 s

Scope nests first, inheritance second: every class-level hook runs before any method-level one, and within one scope the superclass's @Before hook runs before the subclass's, while @After hooks run subclass first, then superclass.

open as a page