【TestNG快板说五】TestNG失败用例重新运行

失败用例重新运行

做自动化测试的过程中,经常会碰到一些因为环境因素导致当次测试用例运行失败的场景,重新运行就正常了,为了避免减少事后对这些测试结果的分析,我们可以对failed的测试用例设置重新运行。

那么在TestNG中如何实现失败测试用例的重新云心呢?

需要实现IRetryAnalyzer接口中的retry方法,拦截测试用例的运行结果,根据运行结果是否是false,判定是否要重新云心测试用以及设置运行次数。

设置测试用例运行失败后,最多重新运行三次:

public class Retry implements IRetryAnalyzer {
    private int count = 0;
    private static int maxTry = 3;

    @Override
    public boolean retry(ITestResult iTestResult) {
        if (!iTestResult.isSuccess()) {
            if (count < maxTry) {
                count++;
                iTestResult.setStatus(ITestResult.FAILURE); // mark test as failed
                return true;
            } else {
                iTestResult.setStatus(ITestResult.FAILURE);
            }
        } else {
            iTestResult.setStatus(ITestResult.FAILURE);
        }
        return false;
    }
}

在testDemo2中增加assert false,使之运行失败:

public class Demo2 {
    @BeforeClass
    public void init() {
        System.out.print("BeforeClass\n");
    }

    @Test
    public void testDemo1() throws InterruptedException{
        System.out.print("testDemo1\n");
    }

    @Test
    public void testDemo2() throws InterruptedException{
        System.out.print("testDemo2\n");
        assert false;
    }

    @Test
    public void testDemo3() throws InterruptedException{
        System.out.print("testDemo3\n");
    }

    @AfterClass
    public void quit() {
        System.out.print("AfterClass\n");
    }
}

测试结果:

BeforeClass
testDemo2

java.lang.AssertionError
	at com.nitb.demo.Demo2.testDemo2(Demo2.java:21)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	
testDemo1
testDemo3
AfterClass

从上面的结果看出,这里testDemo2只执行了一次,下面我们修改下testDemo2:

    @Test(retryAnalyzer = Retry.class)
    public void testDemo2() throws InterruptedException{
        System.out.print("testDemo2\n");
        assert false;
    }

运行结果:

BeforeClass
testDemo2

Test ignored.
testDemo2

Test ignored.
testDemo2

Test ignored.
testDemo2

java.lang.AssertionError
	at com.nitb.demo.Demo2.testDemo2(Demo2.java:21)
    ......
testDemo3
testDemo1
AfterClass

===============================================
Suite1
Total tests run: 6, Failures: 1, Skips: 3
===============================================

分析结果,testDemo2一共运行了三次,前两次由于都是失败,都被忽视了,第三次的时候还是失败,才抛出异常。

设置默认所有测试用例失败后都重新运行max

需要实现IAnnotationTransformer,同时在testng.xml的listeners中添加自定义实现的注释变换器。

public class AnnotationTransformer implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) {
        iTestAnnotation.setRetryAnalyzer(Retry.class);
    }
}

修改testng.xml:

<suite name="Suite1" verbose="1" >
    <listeners>
        <listener class-name="com.nitb.demo.TestOrderRandomizer" />
        <listener class-name="com.nitb.demo.AnnotationTransformer" />
    </listeners>
    <test name="Regression1">
        <classes>
            <class name="com.nitb.demo.Demo2">
            </class>
        </classes>
    </test>
</suite>

在testDemo1也添加assert false:

    @Test
    public void testDemo1() throws InterruptedException{
        System.out.print("testDemo1\n");
        assert false;
    }

运行结果:

BeforeClass
testDemo1

Test ignored.
testDemo1

Test ignored.
testDemo1

Test ignored.
testDemo1

java.lang.AssertionError
	at com.nitb.demo.Demo2.testDemo1(Demo2.java:16)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	

分析结果:看到这里即使没有设置@Test(retryAnalyzer = Retry.class),testDemo1也运行了三次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值