我在selenium代码中使用RetryAnalyzer,如果一个测试用例第一次执行失败,retryAnalyzer会再次执行它。在我的例子中,如果第一次执行失败,第二次执行通过,我想在Extent Reports中只显示第二次执行的结果。但是我在报告中得到了两个测试案例的结果。
下面是我的代码。
ExtentTest test;
int maxRetryCount = 1;
@AfterMethod
protected void afterMethod(ITestResult result) {
boolean firstTry = true;
if(maxRetryCount>0){
if (result.getStatus() == ITestResult.FAILURE && firstTry == true) {
firstTry = false;
} else if (result.getStatus() == ITestResult.SKIP) {
test.log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
} else if (result.getStatus() == ITestResult.FAILURE && firstTry == false) {
test.log(LogStatus.ERROR, test.addScreenCapture(imgPath));
test.log(LogStatus.FAIL, result.getThrowable());
} else {
test.log(LogStatus.PASS, "Test passed");
}
}else{
if (result.getStatus() == ITestResult.FAILURE) {
test.log(LogStatus.ERROR, test.addScreenCapture(imgPath));
test.log(LogStatus.FAIL, result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
test.log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
} else {
test.log(LogStatus.PASS, "Test passed");
}
}
}
在这种情况下,如果测试用例在第一次执行时失败,它显示该测试用例的状态为 “未知”,而假设在重试时执行通过(第二次执行),通过的百分比显示为50%,而不是100%,因为它把未知的百分比也计算在内。
在下面的循环条件中,我应该做什么改变,使这个测试用例的结果不显示在报告中。
if (result.getStatus() == ITestResult.FAILURE && firstTry == true) { }
请给我建议。
解决方案:
如果测试状态是Skipped,你可以删除当前测试节点。类似这样的工作…
@BeforeMethod(alwaysRun=true)
public void beforeMethod(){
//be sure to create node in the BeforeMethod
HtmlReporter.createNode("CurrentTest");
}
@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult result) {
if (result.getStatus() == ITestResult.SKIP) {
HtmlReporter.removeCurrentNode();
}
移除当前节点的代码是这样的。
public static synchronized void removeCurrentNode() {
_report.removeTest(getNode());
}
public static synchronized ExtentTest getNode() {
return extentTestMap.get("node_" + Thread.currentThread().getId());
}