(九)TestNG学习之路—失败测试重跑

目录

(一)TestNG学习之路—HelloWorld入门
(二)TestNG学习之路—注解及属性概览
(三)TestNG学习之路—TestNG.xml/YAML
(四)TestNG学习之路—注解详述之@Test
(五)TestNG学习之路—注解详述之参数化
(六)TestNG学习之路—注解详述之@Factory
(七)TestNG学习之路—注解详述之忽略测试
(八)TestNG学习之路—注解详述之并发
(九)TestNG学习之路—失败测试重跑
(十)TestNG学习之路—编码执行TestNG
(十一)TestNG学习之路—BeanShell高级用法
(十二)TestNG学习之路—注解转换器
(十三)TestNG学习之路—方法拦截器
(十四)TestNG学习之路—TestNG监听器
(十五)TestNG学习之路—依赖注入
(十六)TestNG学习之路—测试报告
(十七)基于TestNG+Rest Assured+Allure的接口自动化测试框架

前言

在案例执行过程中,往往需要对失败的案例进行重跑,TestNG亦提供相应的实现方案。

示例

当套件中的测试执行失败时,TestNG都会创建一个名为testng-failed.xml的文件,该XML文件包含运行失败的方法的信息,允许您快速重现失败,而不必运行整个测试,如下所示:
编写测试类:

import org.testng.Assert;
import org.testng.annotations.*;

public class TestNGHelloWorld1 {
    @BeforeTest
    public void bfTest() {
        System.out.println("TestNGHelloWorld1 beforTest!");
    }

    @Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = ".*zero")
    public void helloWorldTest1() {
        System.out.println("TestNGHelloWorld1 Test1!");
        int c = 1 / 0;
        Assert.assertEquals("1", "1");
    }

    @Test()
    @Parameters(value = "para")
    public void helloWorldTest2(@Optional("Tom")String str) {
        Assert.assertEquals("1", "2");
        System.out.println("TestNGHelloWorld1 Test2! "+ str);

    }

    @AfterTest
    public void AfTest() {
        System.out.println("TestNGHelloWorld1 AfterTest!");
    }
}

执行:

D:\IntelliJ_IDEA_workspace\TestNG>java -classpath "%classpath%;D:\IntelliJ_IDEA_workspace\TestNG\lib" org.testng.TestNG -d tom testng14.xml

执行后,可发现tom目录下,生成了一个testng-failed.xml文件。


testng-failed.xml

testng-failed.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="Failed suite [All Test Suite]">
  <test name="Test(failed)">
    <parameter name="para" value="Tomandy"/>
    <classes>
      <class name="TestNGHelloWorld1">
        <methods>
          <include name="helloWorldTest2" invocation-numbers="0"/>
          <include name="AfTest"/>
          <include name="bfTest"/>
        </methods>
      </class> <!-- TestNGHelloWorld1 -->
    </classes>
  </test> <!-- Test(failed) -->
</suite> <!-- Failed suite [All Test Suite] -->

后续需要重跑失败案例,只需执行testng-failed.xml即可。但是在持续集成实施过程中,我们更希望的是用例执行失败后自动重跑,可通过TestNG提供的retryAnalyzer实现,示例如下:
实现IRetryAnalyzer。

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class MyRetry implements IRetryAnalyzer {

    private int retryCount = 0;
    private static final int maxRetryCount = 3;

    public boolean retry(ITestResult iTestResult) {
        if (retryCount < maxRetryCount) {
            retryCount++;
            return true;
        }
        return false;
    }
}

helloWorldTest2方法添加retryAnalyzer:

import org.testng.Assert;
import org.testng.annotations.*;

public class TestNGHelloWorld1 {
    @BeforeTest
    public void bfTest() {
        System.out.println("TestNGHelloWorld1 beforTest!");
    }

    @Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = ".*zero")
    public void helloWorldTest1() {
        System.out.println("TestNGHelloWorld1 Test1!");
        int c = 1 / 0;
        Assert.assertEquals("1", "1");
    }

    @Test(retryAnalyzer = MyRetry.class)  //失败重跑
    @Parameters(value = "para")
    public void helloWorldTest2(@Optional("Tom")String str) {
        Assert.assertEquals("1", "2");
        System.out.println("TestNGHelloWorld1 Test2! "+ str);

    }

    @AfterTest
    public void AfTest() {
        System.out.println("TestNGHelloWorld1 AfterTest!");
    }
}

执行后,可发现helloWorldTest2方法重跑了3遍。


retry
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 感谢原作者的奉献,原作者博客地址:http://blog.csdn.net/zhu_ai_xin_520/arti...
    狼孩阅读 14,125评论 1 35
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 小的时候遥想28岁的自己,是一个遥远模糊的影像,可转眼就到来小的时候所遥想的未来,来到了自己28岁生日到来前的现在...
    海燕随手记阅读 321评论 0 0
  • 游烈士陵园有感 2017年7月8日肖静 今天天气炎热,上午我们小红旗实践队前往郑州烈士陵园进行参观,烈士——一个让...
    我想静静_24d6阅读 176评论 0 0