Skip to content

Issue549: Groups in @BeforeMethod and @AfterMethod don't work as expected

Bruce Wen edited this page Jul 22, 2019 · 2 revisions

This wiki page is to explain the Issue 549.

Demo Soure Code and Suite

https://github.com/wengm/testng-training/blob/master/basic/src/test/java/org/testng/forum/i549/Test549.java https://github.com/wengm/testng-training/blob/master/basic/src/suites/forum/i549.xml

Which versions of TestNg were impacted

6.10 - 7.0.0-beta7

Expected Behavior

beforeTc1(), afterTc1() are not executed at all because it belongs to the group S1 and the test method testTc1 belong to that group is not executed at all.

Why it's expected

The Javadoc of the groups attribute says: 6.10:

  /**
   * The list of groups this class/method belongs to.
   */

7.0.0-beta7:

  /**
   * The list of groups this class/method belongs to. Note that even if the test method being
   * invoked belongs to a different group, all @BeforeMethod methods will be invoked before it as
   * long as they belong to groups that were selected to run at all. See {@link #onlyForGroups()} to
   * select test method groups which this method will be invoked before.
   */

The Javadoc in 7.0.0-beta7 is still a bit confused. However, if let me guess, it means as long as the groups to which the @BeforeMethod belongs is selected to run, the @BeforeMethod will run for all test methods.

Actual Behavior

Even the test methods of the group in the attribute groups is NOT selectd to run, @BeforeMethod belong that group still run.

Solution or Workaround

From 7.0.0-beta7, Use onlyForGroups attribute if you want Before and After methods only run for the specific test method.