Why @AfterClass called after tests from another classes?

半世苍凉 提交于 2019-12-11 04:45:20

问题


I tried to run test cases in determined order but without luck. As I see methods annotated with @AfterClass runs after methods from another test:

Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNGMapConfigurator@558ee9d6
RUN class com.example.testng.ITCaseOne.beforeClass()
RUN class com.example.testng.ITCaseOne.someCase()
RUN class com.example.testng.ITCaseTwo.beforeClass()
RUN class com.example.testng.ITCaseTwo.someCase()
RUN class com.example.testng.ITCaseOne.anotherCase()
RUN class com.example.testng.ITCaseOne.afterClass()
RUN class com.example.testng.ITCaseTwo.anotherCase()
RUN class com.example.testng.ITCaseTwo.afterClass()
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.25 sec

And it fail because all @Test from ITCaseTwo must be called only after ITCaseOne.afterClass() (because I use Selenium and tests from one case should check appropriate page).

My simple classes:

public class ITCaseOne {

    @BeforeClass
    public void beforeClass() {
        System.out.printf("RUN %s.beforeClass()\n", getClass());
    }

    @AfterClass(alwaysRun = true)
    public void afterClass() {
        System.out.printf("RUN %s.afterClass()\n", getClass());
    }

    @Test(groups = "std-one")
    public void someCase() {
        System.out.printf("RUN %s.someCase()\n", getClass());
    }

    @Test(groups = "logic-one", dependsOnGroups = "std-one")
    public void anotherCase() {
        System.out.printf("RUN %s.anotherCase()\n", getClass());
    }

}

and

public class ITCaseTwo {

    @BeforeClass
    public void beforeClass() {
        System.out.printf("RUN %s.beforeClass()\n", getClass());
    }

    @AfterClass(alwaysRun = true)
    public void afterClass() {
        System.out.printf("RUN %s.afterClass()\n", getClass());
    }

    @Test(groups = "std-two")
    public void someCase() {
        System.out.printf("RUN %s.someCase()\n", getClass());
    }

    @Test(groups = "logic-two", dependsOnGroups = "std-two")
    public void anotherCase() {
        System.out.printf("RUN %s.anotherCase()\n", getClass());
    }

}

If it's important I use maven-failsafe-plugin 2.12 and testng 6.4

(BTW, I also try to use suite file with preserve-order="true" but it doesn't work for me.)

Thanks in advance!


回答1:


I was able to reproduce this behavior, it's a bug. I'll look into it. In the meantime, commenting out one of the two dependsOnGroups should fix the incorrect behavior.




回答2:


Essentially you have dependent method requirements. If you are keeping them in separate classes, then you can make @Test of the second class, which you want executed second, to depend on one of the groups in the first class. So if you keep @Test(groups = "std-two", dependsOnGroups = "logic-one"), things should work, the way u want it..




回答3:


I found yet another way how to fix it with test suite and preserve-order="true" (inspired by When needing to run tests from 2 classes from testng.xml, why does TestNG pick mehods randomly from classes?).

Create src/test/config/testng.xml with following content

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Selenium tests" preserve-order="true">
    <test name="Test #1">
        <classes>
            <class name="com.example.testng.ITCaseOne" />
        </classes>
    </test>
    <test name="Test #2">
        <classes>
            <class name="com.example.testng.ITCaseTwo" />
        </classes>
    </test>
</suite>

Handle it by maven-failsafe-plugin

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.12</version>
        <configuration>
                <suiteXmlFiles>
                            <suiteXmlFile>${basedir}/src/test/config/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
        </configuration>
</plugin>

And now it works as expected:

RUN class com.example.testng.ITCaseOne.beforeClass()
RUN class com.example.testng.ITCaseOne.someCase()
RUN class com.example.testng.ITCaseOne.anotherCase()
RUN class com.example.testng.ITCaseOne.afterClass()
RUN class com.example.testng.ITCaseTwo.beforeClass()
RUN class com.example.testng.ITCaseTwo.someCase()
RUN class com.example.testng.ITCaseTwo.anotherCase()
RUN class com.example.testng.ITCaseTwo.afterClass()


来源:https://stackoverflow.com/questions/9808703/why-afterclass-called-after-tests-from-another-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!