Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

@AfterClass config method is executed for EACH @Test method when parallel == methods #2726

Closed
1 of 7 tasks
dr29bart opened this issue Feb 8, 2022 · 1 comment · Fixed by #2757
Closed
1 of 7 tasks

Comments

@dr29bart
Copy link
Contributor

dr29bart commented Feb 8, 2022

TestNG Version

7.5 (also reproducible in previous releases: 7.3.0)

Expected behavior

a configuration method annotated with @AfterClass is executed only ONCE after all @Test methods in the class are finished

Actual behavior

a configuration method annotated with @AfterClass is executed multiple times after all @Test methods in the class are finished

Is the issue reproducible on runner?

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

Test case sample

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ATest {

    @BeforeClass
    public void setup() {
        System.out.println("BEFORE_" + Thread.currentThread().getName());
    }

    @AfterClass
    public void tearDown() {
        System.out.println("AFTER_" + Thread.currentThread().getName());
    }

    @Test
    public void testa() {
        System.out.println("testa_" + Thread.currentThread().getName());
    }

    @Test
    public void testb() {
        System.out.println("testb_" + Thread.currentThread().getName());
    }
}

Output:

BEFORE_TestNG-test=tqa-1
testb_TestNG-test=tqa-2
testa_TestNG-test=tqa-1
AFTER_TestNG-test=tqa-1
AFTER_TestNG-test=tqa-2

image

@dr29bart
Copy link
Contributor Author

dr29bart commented Feb 8, 2022

It looks like inst is never actually added to the m_classMethodMap.getInvokedAfterClassMethods()
https://github.com/cbeust/testng/blob/0ace1d4bdfc4f8c5ccd0849ad4768f4dd08146fd/testng-core/src/main/java/org/testng/internal/invokers/TestMethodWorker.java#L201-L206

 Set<Object> instances = 
     invokedAfterClassMethods.computeIfAbsent(testClass, key -> Sets.newHashSet()); 
 Object inst = mi.getInstance(); 
 if (!instances.contains(inst)) { 
   invokeInstances.add(inst); 
   instances.add(inst);
 } 

@juherr juherr added this to the 7.6.0 milestone Feb 9, 2022
krmahadevan added a commit to krmahadevan/testng that referenced this issue May 9, 2022
krmahadevan added a commit that referenced this issue May 9, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants