• 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:HCTGetTimesrccomhct	estdataTestSuite.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
    ===============================================
  • 相关阅读:
    luogu1196 银河英雄传说 (并查集)
    [BZOJ2243][SDOI2011]染色
    [BZOJ1879] [Sdoi2009]Bill的挑战
    [Noip2003] 侦探推理
    [Noip2005] 篝火晚会
    [JZOJ100047] 【NOIP2017提高A组模拟7.14】基因变异
    [九省联考2018]一双木棋chess
    [Noip2009] 靶形数独
    [Luogu2737] [USACO4.1]麦香牛块Beef McNuggets
    [BZOJ3109] [cqoi2013]新数独
  • 原文地址:https://www.cnblogs.com/HCT118/p/7430459.html
Copyright © 2020-2023  润新知