在我的实习中,我必须使用 TestNG 和 selenium 来测试 Web 应用程序。但我有一个问题,有时硒或浏览器由于某种随机原因无法工作,因此工作测试被标记为“失败”。为了避免这种情况,我可以使用注释@Test(invocationCount = 4, successPercentage = 25)
,那么如果测试成功一次,测试就被标记为“成功”,这很好,但问题是这个解决方案将测试时间乘以 4,这不是很有效。
我可以做的来减少测试时间,就是编写一些规则“如果测试失败,则重新运行此测试(并且仅当测试失败时),如果第二次、第三次或第四次有效,则将此测试标记为“成功”“这样我就可以避免这些随机错误。但我还没有找到如何编写这个规则,我看到我们可以添加一个监听器,所以我们有一个名为“onTestFailure
“这样当测试失败时我可以做一些事情,但我不知道如何重新运行测试。
我还发现 testng-failed.xml 保存了所有失败的测试,因此我们可以运行此 xml 文件来重新运行这些测试,但这会删除之前第一次运行的报告,但我只想标记失败的测试如果第二次运行成功,则为“成功”。 (我已经将 testNG/selenium 集成到 Jenkins 中,所以我有一个包含所有测试的图表,所以这个方法不是很适应,但是这个方法不会将测试时间乘以 4,这就是我想要的)
因此,如果您知道如何做到这一点,那就太好了。
您可以使用 IRetryAnalyzer、侦听器和自定义报告器的组合来完成您想要的操作。
IRetry分析器:
public class RetryAnalyzer implements IRetryAnalyzer {
private int count = 0;
// this number is actually twice the number
// of retry attempts will allow due to the retry
// method being called twice for each retry
private int maxCount = 6;
protected Logger log;
private static Logger testbaseLog;
static {
PropertyConfigurator.configure("test-config/log4j.properties");
testbaseLog = Logger.getLogger("testbase.testng");
}
public RetryAnalyzer()
{
testbaseLog.trace( " ModeledRetryAnalyzer constructor " + this.getClass().getName() );
log = Logger.getLogger("transcript.test");
}
@Override
public boolean retry(ITestResult result) {
testbaseLog.trace("running retry logic for '"
+ result.getName()
+ "' on class " + this.getClass().getName() );
if(count < maxCount) {
count++;
return true;
}
return false;
}
}
重试监听器:
public class RetryTestListener extends TestListenerAdapter {
private int count = 0;
private int maxCount = 3;
@Override
public void onTestFailure(ITestResult result) {
Logger log = Logger.getLogger("transcript.test");
Reporter.setCurrentTestResult(result);
if(result.getMethod().getRetryAnalyzer().retry(result)) {
count++;
result.setStatus(ITestResult.SKIP);
log.warn("Error in " + result.getName() + " with status "
+ result.getStatus()+ " Retrying " + count + " of 3 times");
log.info("Setting test run attempt status to Skipped");
}
else
{
count = 0;
log.error("Retry limit exceeded for " + result.getName());
}
Reporter.setCurrentTestResult(null);
}
@Override
public void onTestSuccess(ITestResult result)
{
count = 0;
}
然而,TestNG 中似乎存在一个错误,实际上导致一些测试结果被报告为跳过和失败。为了防止这种情况,我建议您重写您想要使用的任何 Reporter 并包含如下方法:
private IResultMap removeIncorrectlyFailedTests(ITestContext test)
{
List<ITestNGMethod> failsToRemove = new ArrayList<ITestNGMethod>();
IResultMap returnValue = test.getFailedTests();
for(ITestResult result : test.getFailedTests().getAllResults())
{
long failedResultTime = result.getEndMillis();
for(ITestResult resultToCheck : test.getSkippedTests().getAllResults())
{
if(failedResultTime == resultToCheck.getEndMillis())
{
failsToRemove.add(resultToCheck.getMethod());
break;
}
}
for(ITestResult resultToCheck : test.getPassedTests().getAllResults())
{
if(failedResultTime == resultToCheck.getEndMillis())
{
failsToRemove.add(resultToCheck.getMethod());
break;
}
}
}
for(ITestNGMethod method : failsToRemove)
{
returnValue.removeResult(method);
}
return returnValue;
}
完成所有这些后,您可以使用 .addListener 添加报告器,并在 @Test 注释中指定 retryAnalyzer。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)