Unit testing 如何在retryAnalyzer之后将TestNG DefaultSuite结果发送给Maven Surefire插件
我搜索了很多,这是我得到的最接近的答案,但并没有解决我的问题 但我需要将上述问题从TestNG扩展到Maven。请帮忙 我的项目堆栈:TestNG,Maven surefire插件,Maven。我正在从命令行“mvn干净编译测试”运行测试。我正在使用retryanalyzer重新运行第二次通过的失败测试。我已经能够使用CustomListener更新存储在TestNG的testcontext中的结果(基于stackoverflow中的解决方案)。当我作为TestNG套件运行测试时,这一点得到了证实 但当我运行“mvn clean compile test”时,所有重试都被视为单独的测试,构建总是失败。如何让TestNG只向Maven Surefire插件发送最终套件结果 TESTNG执行:Unit testing 如何在retryAnalyzer之后将TestNG DefaultSuite结果发送给Maven Surefire插件,unit-testing,maven,testing,testng,maven-surefire-plugin,Unit Testing,Maven,Testing,Testng,Maven Surefire Plugin,我搜索了很多,这是我得到的最接近的答案,但并没有解决我的问题 但我需要将上述问题从TestNG扩展到Maven。请帮忙 我的项目堆栈:TestNG,Maven surefire插件,Maven。我正在从命令行“mvn干净编译测试”运行测试。我正在使用retryanalyzer重新运行第二次通过的失败测试。我已经能够使用CustomListener更新存储在TestNG的testcontext中的结果(基于stackoverflow中的解决方案)。当我作为TestNG套件运行测试时,这一点得到了证
============================================
DefaultTest
Total tests run: 4, Failures: 2, Skips: 0
============================================
============================================
DefaultSuite
Total tests run: 2, Failures: 0, Skips: 0
============================================
Tests run: 4, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 0.292 sec <<< FAILURE!
Results :
Failed tests:
test1(foo.TestClass1)
test1(foo.TestClass1)
Tests run: 4, Failures: 2, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
MAVEN执行:
============================================
DefaultTest
Total tests run: 4, Failures: 2, Skips: 0
============================================
============================================
DefaultSuite
Total tests run: 2, Failures: 0, Skips: 0
============================================
Tests run: 4, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 0.292 sec <<< FAILURE!
Results :
Failed tests:
test1(foo.TestClass1)
test1(foo.TestClass1)
Tests run: 4, Failures: 2, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
测试运行:4次,失败:2次,错误:0次,跳过:0次,经过的时间:0.292秒终于得到了它。我使用以下代码:
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
某些西装信息不正确,但maven surefire工作正常
maven surefire插件2.16
testng 6.8.8
//====InvokedMethodListener=====
导入org.testng.IInvokedMethod;
导入org.testng.IInvokedMethodListener;
导入org.testng.ITestResult;
公共类InvokedMethodListener实现IInvokedMethodListener{
@凌驾
调用前公共无效(IInvokedMethod方法,ITestResult testResult){
}
@凌驾
调用后公共void(IInvokedMethod方法,ITestResult testResult){
RetryAnalyzer RetryAnalyzer=(RetryAnalyzer)testResult.getMethod().getRetryAnalyzer();
if(retryAnalyzer==null | | retryAnalyzer.isFailed()){
返回;
}
if(testResult.getStatus()==ITestResult.FAILURE){
testResult.setStatus(ITestResult.SUCCESS);
}
}
}
//====RetryAnalyzer=====
导入org.testng.IRetryAnalyzer;
导入org.testng.ITestResult;
导入org.testng.Reporter;
公共类RetryAnalyzer实现IRetryAnalyzer{
私有整数计数=0;
私有整数最大计数=2;
@凌驾
公共布尔重试(ITestResult结果){
如果(!result.issucess()){
如果(计数<最大计数){
计数++;
返回true;
}
}
返回false;
}
公共布尔值失败(){
返回计数>=最大计数;
}
}
//=======基本测试=====
@侦听器({..InvokedMethodListener.class})
公共抽象类基类测试{
...
@BeforeSuite(alwaysRun=true)
公共void beforeSuite(ITestContext上下文){
for(ITestNGMethod方法:context.getAllTestMethods()){
方法setRetryAnalyzer(新的RetryAnalyzer());
}
}
}
嗨!你的问题解决了吗?我跟踪到maven testng runner,它将自己的侦听器安装到testng中。它忽略了最终的反向调整,因为它在这里保留了自己的问题。有什么想法或更新吗?。谢谢在我的测试框架中有一个类似的设置,但是我得到了以下错误:Listener包。RerunTestAnalyzer@6fda4e65必须是ITestListener、ISuiteListener、IReporter、iNotationTransformer、IMethodInterceptor或IInvokedMethodListener之一。您知道如何修复此错误吗?经过一些研究,我发现我们不需要在pom.xml中声明我们的IRetryAnalyzer自定义实现。由于它不是典型的TestNG侦听器,因此上面的错误是有效的。此外,修正已执行测试总数的逻辑在TestListenerAdapter实现中。只要我们在pom文件中声明了这个监听器,Maven就会打印正确执行的测试数量。