本文介绍了为什么@AfterClass 在其他类的测试后调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按确定的顺序运行测试用例,但没有运气.正如我看到用 @AfterClass 注释的方法在另一个测试的方法之后运行:

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

它失败了,因为 ITCaseTwo 中的所有 @Test 必须在 ITCaseOne.afterClass() 之后调用(因为我使用 Selenium 和测试从一个案例应该检查适当的页面).

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).

我的简单课程:

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());
    }

}

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());
    }

}

如果这很重要,我使用 maven-failsafe-plugin 2.12 和 testng 6.4

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

(顺便说一句,我也尝试将套件文件与 preserve-order="true" 一起使用,但它对我不起作用.)

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

提前致谢!

推荐答案

我能够重现此行为,这是一个错误.我会调查一下.同时,注释掉两个dependsOnGroups 之一应该可以修复不正确的行为.

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.

这篇关于为什么@AfterClass 在其他类的测试后调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 05:34