简体   繁体   中英

TestNG Re run test on failure, not running BeforeClass AfterClass methods

I implemented logic that re runs a failed TestNG test class from the following link:

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

Unfortunately, it runs the method with the "Test" annotation, and not running the BeforeClass (@BeforeClass) and AfterClass (@AfterClass) methods. I tried looking into setDependsOnMethods and getDependsOnMethods methods of ITestAnnotations to no avail.

Does anybody know how to get the listener class to run BeforeClass and AfterClass methods as well?

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

TestNG invokes only failed @Test methods. You can see implementation here https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/internal/Invoker.java#L1408

So no, you can't invoke @BeforeClass and @AfterClass in RetryListener again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM