重新运行Selenium测试会导致Maven构建失败
我们每晚都会运行一组UI测试,有时一些测试会因为网络故障而失败。为了避免测试结果出现假阴性,我使用了重新运行Selenium测试会导致Maven构建失败,selenium,testng,maven-surefire-plugin,Selenium,Testng,Maven Surefire Plugin,我们每晚都会运行一组UI测试,有时一些测试会因为网络故障而失败。为了避免测试结果出现假阴性,我使用了IRtetryAnalyzer,它可以重新运行失败的测试多达3次 当我右键单击suite.xml从Eclispe运行测试时,它工作得非常好-如果测试最初失败,但重试时通过,则第一个结果不算作失败 但是,当作为Mavenbuild的一部分运行相同的测试时,如果任何测试失败,即使在重试时成功通过,构建也会失败 例如,当以TestNGsuite运行时,运行一组5个测试,其中一个测试第一次失败,但第二次尝
IRtetryAnalyzer
,它可以重新运行失败的测试多达3次
当我右键单击suite.xml从Eclispe运行测试时,它工作得非常好-如果测试最初失败,但重试时通过,则第一个结果不算作失败
但是,当作为Maven
build的一部分运行相同的测试时,如果任何测试失败,即使在重试时成功通过,构建也会失败
例如,当以TestNG
suite运行时,运行一组5个测试,其中一个测试第一次失败,但第二次尝试失败,则会给出此输出:
运行的测试总数:5,失败:0,跳过:0
但对于Maven,结果是不同的:
测试运行:6,失败:1,错误:0,跳过:0
有没有办法使
Maven
不将重新运行测试视为失败?我在中找到了部分解决方案
我仍然无法从所有测试计数中删除失败和重试的测试结果,但如果测试通过第二次或第三次执行,至少我的构建不会失败。最终得到了它。我使用以下代码: ListenerPadter:
public class MyTestListenerAdapter extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult result) {
if (result.getMethod().getRetryAnalyzer() != null) {
MyRetryAnalyzer retryAnalyzer = (MyRetryAnalyzer)result.getMethod().getRetryAnalyzer();
if(retryAnalyzer.isRetryAvailable()) {
result.setStatus(ITestResult.SKIP);
} else {
result.setStatus(ITestResult.FAILURE);
}
Reporter.setCurrentTestResult(result);
}
}
@Overrride
public void onFinish(ITestContext context) {
Iterator<ITestResult> failedTestCases =context.getFailedTests().getAllResults().iterator();
while (failedTestCases.hasNext()) {
System.out.println("failedTestCases");
ITestResult failedTestCase = failedTestCases.next();
ITestNGMethod method = failedTestCase.getMethod();
if (context.getFailedTests().getResults(method).size() > 1) {
System.out.println("failed test case remove as dup:" + failedTestCase.getTestClass().toString());
failedTestCases.remove();
} else {
if (context.getPassedTests().getResults(method).size() > 0) {
System.out.println("failed test case remove as pass retry:" + failedTestCase.getTestClass().toString());
failedTestCases.remove();
}
}
}
}
}
public class MyRetryAnalyzer implements IRetryAnalyzer {
private static int MAX_RETRY_COUNT = 3;
AtomicInteger count = new AtomicInteger(MAX_RETRY_COUNT);
public boolean isRetryAvailable() {
return (count.intValue() > 0);
}
@Override
public boolean retry(ITestResult result) {
boolean retry = false;
if (isRetryAvailable()) {
System.out.println("Going to retry test case: " + result.getMethod() + ", " + (MAX_RETRY_COUNT - count.intValue() + 1) + " out of " + MAX_RETRY_COUNT);
retry = true;
count.decrementAndGet();
}
return retry;
}
}
POM.xml->Surefire配置:
这是您应该配置“覆盖”surefire侦听器的地方,它有自己的计数器
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles><suiteXmlFile>${basedir}/testng.xml</suiteXmlFile></suiteXmlFiles>
<properties>
<property>
<name>listener</name>
<value>Utils.MyTestListenerAdapter,Utils.MyRetryAnalizer</value>
</property>
</properties>
org.apache.maven.plugins
maven surefire插件
2.18.1
${basedir}/testng.xml
听众
Utils.MyTestListenerAdapter、Utils.myretryanalyzer
我复制了文件,但构建仍然失败(mvn测试)。有什么想法吗?。