question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Either I’m misinterpreting or misusing TestNG-6.8.8, but it seems like the groups attribute in the @BeforeMethod and @AfterMethod aren’t working as expected.

Consider following class:

public class MyClass {
    public void method1() {
        System.out.println("Method 1 invocation");
    }
    public void method2() {
        System.out.println("Method 2 invocation");
    }
}

and following test

public class MyClassTest {
    MyClass o = new MyClass();
    @BeforeMethod(groups = "method1") 
    public void before1() {
        System.out.println("Before1");
    }
    @BeforeMethod(groups = "method2") 
    public void before2() {
        System.out.println("Before2");
    }

    @AfterMethod(groups = "method1") 
    public void after1() {
        System.out.println("After1");
    }
    @AfterMethod(groups = "method2") 
    public void after2() {
        System.out.println("After2");
    }

    @Test(groups = "method1")
    public void testMethod1() throws Exception {
        o.method1();
    }
    @Test(groups = "method2")
    public void testMethod2() throws Exception {
        o.method2();
    }
}

I would expect the output to be

Before1
Method 1 invocation
After1
Before2
Method 2 invocation
After2

However, the output is

Before1
Before2
Method 1 invocation
After1
After2
Before1
Before2
Method 2 invocation
After1
After2

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:30 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
wenijinewcommented, Jul 22, 2019

I tried both 6.10 and 7.0.0-beta, the test result is not expected. Here is the wiki page with details.

If groups cannot group Before and After methods, why don’t remove it to avoid confusions. I am quite confused why we need to add one more attribute onlyForGroups rather than fix groups attribute to make it workable in the expected way. (@juherr, if groups is used for test selection, why did we add it to Before/After Method annotation also?)

But anyway, onlyForGroups can work in 7.0.0-beta7.

1reaction
stachenovcommented, Mar 30, 2018

No, @BeforeGroup doens’t do exactly what is needed here. It is only invoked once per group, whereas it would be nice to have a way to invoke a configuration method before every test from a specific group. Ditto for @AfterMethod. Right now, pretty much the only way to achieve it is the Testcase Class per Test Fixture strategy as xUnit Patterns book puts it. Sometimes it’s fine, but sometimes one ends up with too many classes this way, especially if inheritance is involved.

I propose to add a kind of group filter to @BeforeMethod. Perhaps, @BeforeMethod(onlyForGroups = ...). Then this method will only be invoked before a test method belonging to one of the listed groups. I have just tried some minimal implementation, works fine for me. Should I submit a PR to discuss it further?

And as far as I can see, issue #780 is very similar, so this should fix it too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

BeforeMethod/@AfterMethod(onlyForGroups) methods did not ...
Its an interesting observation if you change onlyforgroups to groups then everything works: but when there are multiple groups included in ...
Read more >
TestNG: @BeforeMethod/@AfterMethod methods having no ...
This is onlyForGroups description from the javadocs: Causes this method to be invoked only if the test method belongs to a listed group....
Read more >
Failure in @AfterMethod and @BeforeMethod - Google Groups
This attribute only applies to failures happening in *test* methods. If a failure happens in a configuration method, TestNG doesn't recover because the...
Read more >
Test methods, classes and groups - TestNG Docs
Any @BeforeMethod (and @AfterMethod) can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called ...
Read more >
TestNG Annotations in Selenium Webdriver | BrowserStack
Let's explore how these methods work. @BeforeMethod: This will be executed before every @test annotated method. @AfterMethod: This will be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found