(九)TestNG學(xué)習(xí)之路—失敗測試重跑

目錄

(一)TestNG學(xué)習(xí)之路—HelloWorld入門
(二)TestNG學(xué)習(xí)之路—注解及屬性概覽
(三)TestNG學(xué)習(xí)之路—TestNG.xml/YAML
(四)TestNG學(xué)習(xí)之路—注解詳述之@Test
(五)TestNG學(xué)習(xí)之路—注解詳述之參數(shù)化
(六)TestNG學(xué)習(xí)之路—注解詳述之@Factory
(七)TestNG學(xué)習(xí)之路—注解詳述之忽略測試
(八)TestNG學(xué)習(xí)之路—注解詳述之并發(fā)
(九)TestNG學(xué)習(xí)之路—失敗測試重跑
(十)TestNG學(xué)習(xí)之路—編碼執(zhí)行TestNG
(十一)TestNG學(xué)習(xí)之路—BeanShell高級用法
(十二)TestNG學(xué)習(xí)之路—注解轉(zhuǎn)換器
(十三)TestNG學(xué)習(xí)之路—方法攔截器
(十四)TestNG學(xué)習(xí)之路—TestNG監(jiān)聽器
(十五)TestNG學(xué)習(xí)之路—依賴注入
(十六)TestNG學(xué)習(xí)之路—測試報告
(十七)基于TestNG+Rest Assured+Allure的接口自動化測試框架

前言

在案例執(zhí)行過程中,往往需要對失敗的案例進行重跑,TestNG亦提供相應(yīng)的實現(xiàn)方案。

示例

當(dāng)套件中的測試執(zhí)行失敗時,TestNG都會創(chuàng)建一個名為testng-failed.xml的文件,該XML文件包含運行失敗的方法的信息,允許您快速重現(xiàn)失敗,而不必運行整個測試,如下所示:
編寫測試類:

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!");
    }
}

執(zhí)行:

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

執(zhí)行后,可發(fā)現(xiàn)tom目錄下,生成了一個testng-failed.xml文件。


testng-failed.xml

testng-failed.xml內(nèi)容如下:

<?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] -->

后續(xù)需要重跑失敗案例,只需執(zhí)行testng-failed.xml即可。但是在持續(xù)集成實施過程中,我們更希望的是用例執(zhí)行失敗后自動重跑,可通過TestNG提供的retryAnalyzer實現(xiàn),示例如下:
實現(xiàn)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!");
    }
}

執(zhí)行后,可發(fā)現(xiàn)helloWorldTest2方法重跑了3遍。


retry
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 感謝原作者的奉獻,原作者博客地址:http://blog.csdn.net/zhu_ai_xin_520/arti...
    狼孩閱讀 14,301評論 1 35
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,663評論 19 139
  • 小的時候遙想28歲的自己,是一個遙遠(yuǎn)模糊的影像,可轉(zhuǎn)眼就到來小的時候所遙想的未來,來到了自己28歲生日到來前的現(xiàn)在...
    海燕隨手記閱讀 381評論 0 0
  • 游烈士陵園有感 2017年7月8日肖靜 今天天氣炎熱,上午我們小紅旗實踐隊前往鄭州烈士陵園進行參觀,烈士——一個讓...
    我想靜靜_24d6閱讀 200評論 0 0

友情鏈接更多精彩內(nèi)容