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.

Expected behavior of annotated methods on an @Test(groups="mygroup") annotated class

See original GitHub issue

I have experienced a strange issue in TestNG 7+ that used to work in TestNG 6. Basically I have a suite defined that runs a group of mygroup and I have a few test classes annotated with @Test(groups="mygroup"). These classes also have a defined @BeforeClass and @BeforeMethod and @AfterMethod which are public void.

When I run my suite, all of the public functions inside the class are picked up and run as if they had an @Test annotation. That includes the public functions that have the above three listed annotations, which all fail with NPEs.

So, my question is, what is the expected behavior in this scenario and what should my annotations look like? Here’s a very rudimentary code sample of what I have for one of the tests:

package com.company.app.integration.suite.app;

@Test(groups = "mygroup")
public class AppRestTest {
    @BeforeClass
    public void setupRestTest() {
        // A bunch of setup that is run as a Test
    }

    @BeforeMethod(alwaysRun = true)
    public void beforeMethod(Method method) {
        System.out.println("Begin " + method.getName() + "..."); // NPE
    }

    @AfterMethod(alwaysRun = true)
    public void afterMethod(ITestResult result) {
        System.out.println("End " + result.getMethod().getMethodName()); // NPE
    }

    @Test
    public void testRestPostGetAll() {
        // works fine and passes as expected
    }

    @Test(groups="othergroup")
    public void testRestPostGetAllNoFilter() {
        // works fine but runs with both group names
    }
    
}

TestNG Version

7.0.0, 7.1.0, 7.3.0

Expected behavior

The previous behavior was that the annotated methods would be skipped and any annotated method with a group that shouldn’t be run was excluded. (It’s worth noting that I am working on an upgrade from TestNG 6.8.8 to 7.x.x and so if the expected behavior has changed that’s fine, I’m looking for clarification as this is not expressed anywhere in the docs I could find).

Actual behavior

Any public function, even TestNG annotated, in the Test class is run as a test when the Class itself has an included groups tag.

Is the issue reproductible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

Test case provided above.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
krmahadevancommented, Sep 27, 2020

@cardonator - I am going ahead and closing off this issue since it turns out to be resolved.

0reactions
cardonatorcommented, Sep 27, 2020

Ahh, thanks for continuing to look into this. So I tried again on 7.3.0 after basically clearing out my maven cache on the demo project and that does indeed appear to fix the issue! When I was testing this before I was having issues with 7.3.0 testing due to conflicts with the IDE. It’s possible that the times I was “testing” with 7.3.0 I was actually somehow still using compiled classes with an earlier version of TestNG included.

I really appreciate the help researching and testing. Sorry it turned out to be already resolved!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Newly-Introduced JUnit 5 Annotations and Classes - Manning
The @Disabled annotation can be used over classes and test methods. It's used to signal that the annotated test class or test method...
Read more >
2. Annotations — PHPUnit 9.5 Manual - Read the Docs
It is then “inherited” to all test methods of that test class. Tests can be selected for execution based on groups using the...
Read more >
How to group test cases in TestNG [with Examples]
In this tutorial, you will learn how to group test cases in TestNG along with the demonstration with relevant TestNG group examples.
Read more >
Annotated test methods and return types - Google Groups
re-use a test method as a helper method for use in some other test within the same class. I would expect that TestNG...
Read more >
TestNG Tutorials 30: Groups Attribute at Class Level In TestNG
Suppose if you need to write multiple @Test annotated methods in a class, and each belongs to same group, you need to mention...
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