首页 > 解决方案 > 如何从 Jenkins 重新执行失败的自动化场景

问题描述

我正在使用 maven 命令在 TestNG 框架中运行黄瓜测试。每天我都在执行 Jenkins 的测试用例并在 Jenkins 中生成黄瓜报告。(使用黄瓜报告插件)

我正在寻找一种解决方案来在 Jenkins 中重新运行失败的测试用例,它应该给出最终报告。

请为我提供实现这一目标的方法。

标签: seleniumjenkinscucumbertestng

解决方案


一种简单的方法是,在 TestNG 中使用 IRetryAnalyzer。它将重新运行失败的测试用例。

在最终报告中,如果重新运行通过,那么我将显示为通过(最初失败一个显示为跳过)

如果重新运行也失败,则标记为失败。

例子:

 public class Retry implements IRetryAnalyzer {

private int count = 0;
private static int maxTry = 3;

@Override
public boolean retry(ITestResult iTestResult) {
    if (!iTestResult.isSuccess()) {                      //Check if test not succeed
        if (count < maxTry) {                            //Check if maxtry count is reached
            count++;                                     //Increase the maxTry count by 1
            iTestResult.setStatus(ITestResult.FAILURE);  //Mark test as failed
            return true;                                 //Tells TestNG to re-run the test
        } else {
            iTestResult.setStatus(ITestResult.FAILURE);  //If maxCount reached,test marked as failed
        }
    } else {
        iTestResult.setStatus(ITestResult.SUCCESS);      //If test passes, TestNG marks it as passed
    }
    return false;
}

}

添加Testng.xml文件

您也可以添加测试

 @Test(retryAnalyzer = Retry.class)

推荐阅读