持续集成 使用 testng 失败用例重跑后成功,ant 的 bulid.xml 构建项目时判断本次构建依旧是失败的,该如何处理呢

小人物 · 2021年09月13日 · 1545 次阅读

添加了 testng 失败用例重跑

public class TestCaseRetry implements IRetryAnalyzer{
     private static Logger logger = Logger.getLogger(TestCaseRetry.class);
        //当前正在进行的重试次数
     private int currentCount = 1;
        //允许重试的最大次数
     private int maxCount = 2;


    @Override
    public boolean retry(ITestResult result) {
          logger.info("当前重试次数是【" + currentCount + "】");
            if (currentCount < maxCount) {
                logger.info("当前测试方法【" + result.getName() + "】执行失败,进入失败用例重试模式,正在进行第【" + currentCount + "】次重试");
                currentCount = currentCount + 1;
                return true;
            }else {
                reSetCount();
                return false;
            }           
    }
    public boolean isRetryAvailable() {
            return currentCount < maxCount;
        }
    public void reSetCount(){
        currentCount=1;
    }
@Override
public void transform(ITestAnnotation annotation,Class testClass, Constructor testConstructor, Method testMethod) {
    // TODO Auto-generated method stub
      //1.初始化IRetryAnalyzer对象
    IRetryAnalyzer iRetryAnalyzer = annotation.getRetryAnalyzer();
    //判断测试方法是否设置重跑属性
    if (iRetryAnalyzer == null) {
        //为空说明用例方法里面未设置重跑,则按照全局设置进行用例重跑
        //如果用例方法设置了注解属性retryAnalyzer,则按照注解的重跑
        annotation.setRetryAnalyzer(PC.base.TestCaseRetry.class);
    }
}

public class CustomListener extends TestListenerAdapter{
    private static Logger logger = Logger.getLogger(CustomListener.class);

    @Override
    public void onFinish(ITestContext testContext) {
        super.onFinish(testContext);

        // List of test results which we will delete later
        ArrayList<ITestResult> testsToBeRemoved = new ArrayList<ITestResult>();
        // collect all id's from passed test
        Set<Integer> passedTestIds = new HashSet<Integer>();
        for (ITestResult passedTest : testContext.getPassedTests()
                .getAllResults()) {
            logger.info("PassedTests = " + passedTest.getName());
            passedTestIds.add(getId(passedTest));
        }

        Set<Integer> failedTestIds = new HashSet<Integer>();
        for (ITestResult failedTest : testContext.getFailedTests()
                .getAllResults()) {
            logger.info("failedTest = " + failedTest.getName());
            int failedTestId = getId(failedTest);

            // if we saw this test as a failed test before we mark as to be
            // deleted
            // or delete this failed test if there is at least one passed
            // version
            if (failedTestIds.contains(failedTestId)
                    || passedTestIds.contains(failedTestId)) {
                testsToBeRemoved.add(failedTest);
            } else {
                failedTestIds.add(failedTestId);
            }
        }

        // finally delete all tests that are marked
        for (Iterator<ITestResult> iterator = testContext.getFailedTests()
                .getAllResults().iterator(); iterator.hasNext();) {
            ITestResult testResult = iterator.next();
            if (testsToBeRemoved.contains(testResult)) {
                logger.info("Remove repeat Fail Test: " + testResult.getName());
                iterator.remove();
            }
        }

    }

    private int getId(ITestResult result) {
        int id = result.getTestClass().getName().hashCode();
        id = id + result.getMethod().getMethodName().hashCode();
        id = id
                + (result.getParameters() != null ? Arrays.hashCode(result
                        .getParameters()) : 0);
        return id;
    }

在使用 ant 构建时,发现失败用例重跑成功后,ant 上面判断的构建状态依旧是失败。能否改成成功呢?


haltOnfailure="true" useDefaultListeners="false" listeners="PC.base.RetryListener,PC.base.CustomListener,org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter" failureproperty="test.failed">





暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册