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.

@BeforeClass and @AfterClass in superclass ignore groups when running parallel in classes

See original GitHub issue

TestNG Version

Edited: 6.14.3

Expected behavior

@BeforeClass and @AfterClass in superclass should respect group parameters and be ran before and after classes with those groups.

Actual behavior

On 6.14.3 @BeforeClass and @AfterClass run for each class no matter groups they belong to. See example tests and output below. If @BeforeClass and @AfterClass are put in each test class instead of superclass then it works fine but it defeats the purpose of having superclass to handle setup and teardown for all test classes. Plus, it adds tons of boilerplate.

On 7.0.0 @BeforeClassand@AfterClass` run for each class before test methods multiple times. Same happens after test methods.

Is the issue reproductible on runner?

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

Test case sample

Class A with group “a”

@Test(groups = ["a"])
class ClassA : BaseTest() {
    @Test
    fun someTest() {
        println("Test in class A with group A ran on ${Thread.currentThread().id} thread")
    }
}

Class B with group “b”

@Test(groups = ["b"])
class ClassB : BaseTest() {
    @Test
    fun someTest() {
        println("Test in class B with group B ran on ${Thread.currentThread().id} thread")
    }
}

Superclass with @BeforeClass and @AfterClass methods for each group.

open class BaseTest {
    @BeforeClass(groups = ["a"])
    fun beforeClassA() {
        println("Ran before class A on ${Thread.currentThread().id} thread")
    }

    @BeforeClass(groups = ["b"])
    fun beforeClassB() {
        println("Ran before class B on ${Thread.currentThread().id} thread")
    }

    @AfterClass(groups = ["a"])
    fun afterClassA() {
        println("Ran after class A on ${Thread.currentThread().id} thread")
    }

    @AfterClass(groups = ["b"])
    fun afterClassB() {
        println("Ran after class B on ${Thread.currentThread().id} thread")
    }
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="All tests" verbose="1" parallel="classes" thread-count="2" >
    <test name="Tests">
        <classes>
            <class name="test.ClassA" />
            <class name="test.ClassB" />
        </classes>
    </test>
</suite>

Actual Output:

Ran before class A on 14 thread
Ran before class A on 15 thread
Ran before class B on 14 thread
Ran before class B on 15 thread
Test in class A with group A ran on 14 thread
Test in class B with group B ran on 15 thread
Ran after class A on 14 thread
Ran after class A on 15 thread
Ran after class B on 14 thread
Ran after class B on 15 thread

Expected output:

Ran before class A on 14 thread
Ran before class B on 15 thread
Test in class A with group A ran on 14 thread
Test in class B with group B ran on 15 thread
Ran after class A on 14 thread
Ran after class B on 15 thread

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19

github_iconTop GitHub Comments

1reaction
Proryanatorcommented, Dec 31, 2020

@Proryanator Could you open a new issue? It will be better than this old closed issue 😃

You can find the newly opened issue here: #2448

1reaction
juherrcommented, Dec 31, 2020

@Proryanator Could you open a new issue? It will be better than this old closed issue 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

TestNG @BeforeClass doesn't run from super class if it not an ...
I have @BeforeClass testng annotation in class A. And I only annotate class C with @Test. Now, I expect that while running class...
Read more >
Annotations - TestNG Docs
@AfterClass, The annotated method will be run after all the test methods in the current class ... Behaviour of annotations in superclass of...
Read more >
Documentation - TestNG
If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests...
Read more >
TestNG - Test LifeCycle Annotations - HowToDoInJava
@AfterClass, The annotated method will be run after all the test methods ... to groups specified in the @Test annotation at the class...
Read more >
TestNG Annotations And Benefits – AUTOMATION TESTING
@BeforeClass. @AfterClass. @BeforeMethod. @AfterMethod. Configuration information for a TestNG class: @BeforeSuite: The annotated method will be run before ...
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