Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Problems with @(Before|After)Suite and @(Before|After)Test #184

Closed
ash2k opened this issue Mar 3, 2012 · 1 comment
Closed

Problems with @(Before|After)Suite and @(Before|After)Test #184

ash2k opened this issue Mar 3, 2012 · 1 comment

Comments

@ash2k
Copy link

ash2k commented Mar 3, 2012

Code, demonstrating issues:

public abstract class AbstractTest {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @BeforeSuite
    public final void beforeAbstractSuite() {
        log.debug("beforeAbstractSuite()");
    }

    @BeforeTest
    public final void beforeAbstractTest() {
        log.debug("beforeAbstractTest()");
    }

    @BeforeClass
    public final void beforeAbstractClass() {
        log.debug("beforeAbstractClass()");
    }

    @BeforeMethod
    public final void beforeAbstractMethod() {
        log.debug("beforeAbstractMethod()");
    }

    @Test
    public final void abstractTest() {
        log.debug("abstractTest()");
    }

    @AfterMethod
    public final void afterAbstractMethod() {
        log.debug("afterAbstractMethod()");
    }

    @AfterClass
    public final void afterAbstractClass() {
        log.debug("afterAbstractClass()");
    }

    @AfterTest
    public final void afterAbstractTest() {
        log.debug("afterAbstractTest()");
    }

    @AfterSuite
    public final void afterAbstractSuite() {
        log.debug("afterAbstractSuite()");
    }
}
public class RealTest1 extends AbstractTest {
    @Test
    public void test1() {
        log.debug("test1");
    }
}
public class RealTest2 extends AbstractTest {
    @Test
    public void test2() {
        log.debug("test2");
    }
}

testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" preserve-order="false" parallel="tests">
    <test name="Test" preserve-order="false" parallel="classes">
        <packages>
            <package name="com.ash2k.test1" />
        </packages>
    </test>
</suite>

After running tests the logger output looks like this:

13:43:05 [main] DEBUG RealTest2:20 - beforeAbstractSuite()
13:43:05 [TestNG] DEBUG RealTest2:25 - beforeAbstractTest()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:30 - beforeAbstractClass()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:30 - beforeAbstractClass()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:40 - abstractTest()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:40 - abstractTest()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:35 - beforeAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:8 - test2
13:43:05 [pool-3-thread-1] DEBUG RealTest1:8 - test1
13:43:05 [pool-3-thread-2] DEBUG RealTest2:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:45 - afterAbstractMethod()
13:43:05 [pool-3-thread-2] DEBUG RealTest2:50 - afterAbstractClass()
13:43:05 [pool-3-thread-1] DEBUG RealTest1:50 - afterAbstractClass()
13:43:05 [TestNG] DEBUG RealTest2:55 - afterAbstractTest()
13:43:05 [main] DEBUG RealTest2:60 - afterAbstractSuite()

I see two problems here:

  • Methods, annotated with @(Before|After)Suite and @(Before|After)Test are not executed before/after each child class. I mean i expected each child class to contribute invocations of the corresponding methods.
  • If the above expectations are correct then there is another issue: those methods are executed with wrong threads. This can be the cause of visibility/concurrency problems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants