【学习笔记】系列十九:TestNG - IRetryAnalyzer

重做失败的case是常用的操作

实现方法1 - 使用RetryAnalyzerCount,此类继承自IRetryAnalyzer

import org.testng.ITestResult;
import org.testng.util.RetryAnalyzerCount;

/**
 * An implementation of IRetryAnalyzer that allows you to specify the maximum
 * number of times you want your test to be retried.
 */
public class TestngRetry extends RetryAnalyzerCount {
	
	public TestngRetry() {
                // set retry count
		setCount(1);
	}

	@Override
	public boolean retryMethod(ITestResult result) {
		System.out.println("Retrying...");
		return !result.isSuccess();
	}
}

需在@Test注解中标注使用

@Test(description = "xxx2", retryAnalyzer = TestngRetry.class)

实现方法2 - 自定义retry类继承IRetryAnalzer

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
 
public class TestngRetry implements IRetryAnalyzer{
    private int count = 1;
    private int max_retry_count = 3;
     
    /*
     *  An implementation of IRetryAnalyzer that retry failed test cases
     * @see org.testng.IRetryAnalyzer#retry(org.testng.ITestResult)
     */
    @Override
    public boolean retry(ITestResult iTestResult) {
        System.out.println("Retrying:"+iTestResult.getName()+","+count+"th failure");
        if(count

同样需要每个case的@Test注解中标注使用,框架级别编码比较麻烦

此时可以使用注解转换器IAnnotationTransformer,同样是前一篇介绍的监听器的一种

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
 
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;
import org.testng.internal.annotations.IAnnotationTransformer;
 
import com.Retry.OverrideRetry;
 
public class retryListener implements IAnnotationTransformer{
 
    @SuppressWarnings("rawtypes")
    @Override
    public void transform(ITestAnnotation iTestAnnotation,
            Class class1, Constructor constructor, Method method) {
        IRetryAnalyzer iRetryAnalyzer = iTestAnnotation.getRetryAnalyzer();
        System.out.println("transform(),iRetryAnalyzer:"+iRetryAnalyzer);
        if(iRetryAnalyzer==null){
            iTestAnnotation.setRetryAnalyzer(OverrideRetry.class);
        }
    }
}

这样就可以在xml中引入此监听器进行使用,不用在每个case的@Test注解中标注了

你可能感兴趣的:(软件测试,学习笔记)