testng中添加案例失败重试次数

1、编写自己的IRetryAnalyzer,需要继承IRetryAnalyzer

package com.hct.testdata;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.Reporter;
 
 
/**
 * TestNG retry Analyzer.*
 */
public class TestngRetry implements IRetryAnalyzer {
    public int initReTryNum=1;
    public int maxReTryNum=3;//默认的重试次数
    public int getMaxReTryNum() {
        return maxReTryNum;
    }

    @Override
    public boolean retry(ITestResult iTestResult) {
        String maxRetriesStr = iTestResult.getTestContext().getSuite().getParameter("maxReTryNum");//获取xml中配置的重试次数
        if (maxRetriesStr != null) {
            try {
                maxReTryNum = Integer.parseInt(maxRetriesStr);
                System.out.println("Get  Retry count from xml:" + maxReTryNum);
            } catch (final NumberFormatException e) {
                System.out.println("NumberFormatException while parsing maxRetries from suite file."+ e);
            }
        }
        if(initReTryNum<=maxReTryNum){
            String message="方法<"+iTestResult.getName()+">执行失败,重试第"+initReTryNum+"次";
            System.out.println(message);
            Reporter.setCurrentTestResult(iTestResult);
            Reporter.log(message);
            initReTryNum++;
            return true;
        }
        return false;
    }
 
}

 

2、编写xml中需要监听的listener

package com.hct.testdata;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;

public class RetryListener implements IAnnotationTransformer {
    public void transform(ITestAnnotation annotation, Class testClass,
            Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if (retry == null) {
            annotation.setRetryAnalyzer(TestngRetry.class);
        }
    }
}

 

3、测试代码,共4条测试案例

package com.hct.testdata;

import org.testng.Assert;
import org.testng.annotations.Test;

public class myTest {
    @Test(retryAnalyzer=TestngRetry.class)
    public void myTestCase1() {
        Assert.assertEquals(0,1,"出错了");
    }
    @Test
    public void myTestCase2() {
        Assert.assertEquals(0,0,"成功了");
    }
    @Test
    public void myTestCase3() {
        Assert.assertEquals(0,0,"成功了");
    }
    @Test
    public void myTestCase4() {
        Assert.assertEquals(0,1,"出错了");
    }
}

 

4、xml配置,在这个配置文件中配置重试2次

<?xml version="1.0" encoding="UTF-8"?>
<suite name="suite_login">
    <listeners>
        <listener class-name="com.hct.testdata.RetryListener" />
        <listener class-name="com.hct.testdata.TestListener" />
    </listeners>
    <parameter name="maxReTryNum" value="2"></parameter>
    <test name="LoginTest">
        <classes>
            <class name="com.hct.testdata.myTest" />
        </classes>
    </test>
</suite>

 

5、测试结果

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNG] Running:
  D:\HCT\GetTime\src\com\hct\testdata\TestSuite.xml

Get  Retry count from xml:2
方法<myTestCase1>执行失败,重试第1次
Get  Retry count from xml:2
方法<myTestCase1>执行失败,重试第2次
Get  Retry count from xml:2
Get  Retry count from xml:2
方法<myTestCase4>执行失败,重试第1次
Get  Retry count from xml:2
方法<myTestCase4>执行失败,重试第2次
Get  Retry count from xml:2
PassedTests = myTestCase2
PassedTests = myTestCase3
failedTest = myTestCase1
failedTest = myTestCase4
skippedTest = myTestCase4
skippedTest = myTestCase1
skippedTest = myTestCase1
skippedTest = myTestCase4
Remove repeat skip Test: myTestCase4
Remove repeat skip Test: myTestCase1
Remove repeat skip Test: myTestCase1
Remove repeat skip Test: myTestCase4

===============================================
suite_login
Total tests run: 4, Failures: 2, Skips: 0
===============================================

 

转载于:https://www.cnblogs.com/HCT118/p/7430459.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值