In TestNG, what does an @BeforeGroups method do, when exactly does it run, and why might it never run in a group-filtered suite?
answer
- setup keyed to a label
- just before the group's first method
- value overrides groups
- must live in a class of the run
- no member selected, no setup
basics
~20 s@BeforeGroups runs setup once per suite-file <test>, shortly before the first test method of the named groups. It never fires if no member of those groups is selected, or if its class is not part of that <test>.
solid answer
~50 s`@BeforeGroups("smoke")` names the groups it prepares; its `value` overrides the `groups` attribute, which is otherwise used for the same purpose. TestNG runs it once per `<test>` element of the suite file, shortly before the first test method belonging to any of those groups, and `@AfterGroups` mirrors it after the group's last method. It is useful when a group spans classes, such as seeding data once for every `smoke` test. It is silent in three situations: no method of the group is selected in that `<test>`, the `@BeforeGroups` method sits in a class that is not part of the `<test>`, or the group filter selects other groups only. When a filter is active, TestNG counts a configuration method's before-groups as its own groups, so including `smoke` also selects `@BeforeGroups("smoke")`. If the method fails, the group's tests are skipped.
code
java · 14 linesimport org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
public abstract class ShopTestBase {
protected static final String SMOKE = "smoke";
@BeforeGroups(SMOKE)
public void seedCatalogue() {
// load the small catalogue every smoke test reads
}
@AfterGroups(SMOKE)
public void clearCatalogue() { }
}go deeper
Recall that @BeforeGroups prepares a named group and @AfterGroups cleans up after it, running around the group's tests.
Explain the timing: once per <test>, just before the group's first method, and only when the group has selected members and the method's class is in the run.
Diagnose a silent group setup from the suite file: missing class, empty group, mismatched name, and plan for it running once per <test>.
Decide where group setup lives, usually a shared base class with constant group names, so it cannot drift out of the run as suites are reorganised.
## What @BeforeGroups is for TestNG lets a configuration method be tied to a **group** rather than to a class or a single test. A method annotated `@BeforeGroups` prepares one or more groups; its partner `@AfterGroups` tears down after them. This is the right tool when the tests that need a piece of setup are spread across several classes but share a label, for example seeding a catalogue once for every test tagged `smoke`, wherever those tests live. Two attributes name the groups: - `value`, as in `@BeforeGroups("smoke")` or `@BeforeGroups({"smoke", "checkout"})`. When present it **overrides** the `groups` list. - `groups`, which otherwise does double duty: it is the list of groups the method belongs to and, when `value` is empty, the list it runs before. ## When exactly it runs The `BeforeGroups` annotation documents that the method is guaranteed to run **shortly before the first test method that belongs to any of these groups** is invoked. Three consequences follow: 1. **Once per `<test>`:** TestNG tracks group setup per `<test>` element of the suite file, so a suite with two `<test>` elements that both contain smoke tests runs the setup in each of them. 2. **Lazily:** nothing happens at the start of the run. If the first smoke test is the fortieth method scheduled, the setup runs just before that fortieth method. 3. **Not at all** when no method of the group runs in that `<test>`. `@AfterGroups` is the mirror image: it runs after the last test method of the group has run in that `<test>`. ## Why it sometimes never runs Most "my @BeforeGroups did not fire" reports come down to one of these: | Cause | What is going on | |---|---| | The group has no selected methods | The filter or the class list leaves no member of the group in that `<test>`, so there is nothing to run before. | | The method's class is not in the run | TestNG collects group configuration methods from the classes of the `<test>`. A `GroupSetup` class that is not listed, and is not a superclass of a listed class, is never seen. | | The filter selects other groups only | A run that includes only `regression` has no reason to prepare `smoke`. | | The group name does not match | A typo between `@BeforeGroups("smoke")` and `@Test(groups = "Smoke")` leaves the setup attached to a group nobody belongs to. | When a group filter is active, TestNG's selector treats the groups a configuration method runs before as groups it belongs to. So including `smoke` in the suite file selects `@BeforeGroups("smoke")` without adding `groups = "smoke"` to the configuration method as well. ## alwaysRun means something narrower here On other `@Before...` methods, `alwaysRun = true` makes the method run whatever groups are selected. The `BeforeGroups` annotation's own documentation excludes `@BeforeGroups` from that meaning: a group setup is inherently about its groups, and `alwaysRun` does not turn it into an unconditional hook. ## When the setup fails A failing `@BeforeGroups` method is a configuration failure, and TestNG reports the tests that needed it as **SKIP** rather than FAIL. The broader rules for configuration failures, and how `@BeforeGroups` slots between class-level and method-level hooks, are covered with TestNG's configuration hook order. ## A worked example Suppose one `<test>` lists `CartTests` and `SearchTests`, both extending a base class that holds `@BeforeGroups("smoke")` and `@AfterGroups("smoke")`: 1. TestNG schedules the methods of both classes. 2. Just before the first smoke method, from whichever class it comes, the group setup runs. 3. The remaining smoke methods run without repeating it. 4. After the last smoke method in that `<test>`, the group teardown runs. Non-smoke methods scheduled before the first smoke method run without the seed, so they must not rely on it. ## Practical guidance - Keep group setup in a base class every test class extends, or list the setup class in each `<test>`, so it is always part of the run. - Use constants for group names shared between `@Test` and `@BeforeGroups`, so a typo becomes a compile error. - Make the setup idempotent: it runs once per `<test>`, so a suite with several `<test>` elements runs it several times. - Prefer it to a fake first test that only seeds data; a configuration method is not counted as a test in the report. JUnit Jupiter has no group-scoped hook; its tags filter tests but do not trigger setup.
- A TestNG suite has two <test> elements, each containing smoke tests. How many times does @BeforeGroups("smoke") run?Twice: once in each <test>. TestNG tracks group setup per <test> element, running it shortly before that <test>'s first smoke method. If the setup is expensive or not idempotent, merge the classes into one <test> or move the work to suite-level setup.
- In TestNG, how does @BeforeGroups differ from putting the same setup in a test method that the smoke tests depend on?@BeforeGroups is a configuration method: it is not counted as a test, and a failure skips the group's tests as a configuration failure. A setup test wired with dependsOnMethods is a real test that appears in the report, and its failure skips dependents through the dependency rule. The first keeps the report clean; the second makes the setup visible as a result.
- @BeforeGroups runs at the start of the suite, before any test.
- @BeforeGroups runs once for the whole suite, however many <test> elements contain the group.
- @BeforeGroups runs even if no method of the group is selected.
- alwaysRun = true makes a group setup run even when its group is filtered out.
- A group setup anywhere on the classpath is picked up automatically.
Assumes TestNG 7.12, whose group selector counts a configuration method's before-groups as its own groups when a filter is active.