TestNG重新运行测试失败,没有运行BeforeClass AfterClass方法

时间:2015-04-20 23:30:40

标签: java selenium-webdriver annotations testng

我实现了从以下链接重新运行失败的TestNG测试类的逻辑:

https://martinholladay.wordpress.com/2013/11/17/webdriver-2-0-automatically-retry-failed-tests/

不幸的是,它使用"测试"运行方法。注释,而不是运行BeforeClass(@BeforeClass)和AfterClass(@AfterClass)方法。我试着查看ITestAnnotations的setDependsOnMethods和getDependsOnMethods方法无济于事。

有没有人知道如何让侦听器类运行BeforeClass和AfterClass方法?

public class RetryListener implements IAnnotationTransformer {
    public void transform(ITestAnnotation annotation, Class testClass,
            Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if (retry == null) {
            annotation.setRetryAnalyzer(Retry.class);
        }
    }
}

//The test begins here....

@BeforeClass(alwaysRun = true)
@Parameters("Environment")
public void BeforeClass(String sEnv) throws Exception {
    WebDriver driver = new FirefoxDriver();
    driver.get("www.google.com");

}

@Test
public void TestMethod() {
    //Some test...
}

@AfterClass(alwaysRun = true)
public void AfterClass() {
    driver.quit();
}

1 个答案:

答案 0 :(得分:0)

TestNG仅调用失败的@Test方法。您可以在此处查看实施https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/internal/Invoker.java#L1408

所以不,你不能再次在RetryListener中调用@BeforeClass@AfterClass