目錄
(一)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í)之路—測試報(bào)告
(十七)基于TestNG+Rest Assured+Allure的接口自動(dòng)化測試框架
前言
在案例執(zhí)行過程中,往往需要對失敗的案例進(jìn)行重跑融虽,TestNG亦提供相應(yīng)的實(shí)現(xiàn)方案麦牺。
示例
當(dāng)套件中的測試執(zhí)行失敗時(shí)捣鲸,TestNG都會創(chuàng)建一個(gè)名為testng-failed.xml的文件吁断,該XML文件包含運(yùn)行失敗的方法的信息擦俐,允許您快速重現(xiàn)失敗佣蓉,而不必運(yùn)行整個(gè)測試酸舍,如下所示:
編寫測試類:
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目錄下褐捻,生成了一個(gè)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ù)集成實(shí)施過程中,我們更希望的是用例執(zhí)行失敗后自動(dòng)重跑景馁,可通過TestNG提供的retryAnalyzer實(shí)現(xiàn)板壮,示例如下:
實(shí)現(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遍绰精。