上一章介紹了ant+testng+xlst,其實到了這里,我們就可以直接搭建在Jenkins服務器上,讓它跑起來!但是,我之前部署在Jenkins上的UI自動化用例偶爾會報錯,我去查看代碼,又沒有什么問題,再運行一次,它又正常了,其實這種問題,我相信不止我一個人遇到!
說到這里,不得不引進TestNg框架的一個接口:IRetryAnalyzer,它有一個方法retry(ITestResult iTestResult)實現對失敗的測試用例多次執行,可以有效的排除上面我說的那種情況。那么怎么來把這個方法給用起來呢?TestNg框架里的Annotion(注解)里可以直接指向該類。
一、下面舉個簡單的列子
1.IRetryAnalyzer接口的實現,
package com.Retry; import org.testng.IRetryAnalyzer; import org.testng.ITestResult; public class OverrideRetry implements IRetryAnalyzer{ private int count = 1; private int max_retry_count = 3; /* * OverrideRetry實現接口IRetryAnalyzer的方法,重復執行失敗用例 * (non-Javadoc) * @see org.testng.IRetryAnalyzer#retry(org.testng.ITestResult) */ @Override public boolean retry(ITestResult iTestResult) { System.out.println("執行用例:"+iTestResult.getName()+",第"+count+"次失敗"); if(count<max_retry_count){ count++; return true; } return false; } }
2.TestNg測試類
@Test(retryAnalyzer= OverrideRetry.class) public void b(){ System.out.println(2/0); }
3.運行結果
二、通過上面的demo,我們實現了對失敗的測試用例進行二次執行。但是愛思考的同學就會發現,上面的demo不太適合用到真正的自動化測試里,因為每個@test里都要進行retryAnalyzer配置,重復勞動,太累了。
下面給大家介紹TestNg另外一個接口IAnnotationTransformer,判斷用例是否達到重試的要求。
1.IRetryAnalyzer接口的實現,
package com.Retry; import org.testng.IRetryAnalyzer; import org.testng.ITestResult; import org.testng.Reporter; public class OverrideRetry implements IRetryAnalyzer{ private int count = 1; //統計測試用例失敗的次數 private int Max_Fail_Count = 3; //測試用例最大失敗次數 @Override public boolean retry(ITestResult iTestResult) { String msg = "執行用例:"+iTestResult.getName()+"第"+count+"次運行失敗"; System.out.println(iTestResult); Reporter.log(msg); if(count<Max_Fail_Count){ count++; return true; } return false; } }
2.IAnnotationTransformer接口的實現。
package com.Transformer; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.testng.IRetryAnalyzer; import org.testng.annotations.ITestAnnotation; import org.testng.internal.annotations.IAnnotationTransformer; import com.Retry.OverrideRetry; public class OverrideTransformer implements IAnnotationTransformer{ @SuppressWarnings("rawtypes") @Override public void transform(ITestAnnotation iTestAnnotation, Class class1, Constructor constructor, Method method) { IRetryAnalyzer iRetryAnalyzer = iTestAnnotation.getRetryAnalyzer(); System.out.println("transform(),iRetryAnalyzer:"+iRetryAnalyzer); if(iRetryAnalyzer==null){ iTestAnnotation.setRetryAnalyzer(OverrideRetry.class); } } }
3.testng.xml,里面添加了監聽器listener,監聽類com.Transformer.OverrideTransformer里的重試函數transform(ITestAnnotation iTestAnnotation, Class class1, Constructor constructor, Method method) ,判斷下面的測試用例是否失敗,達到重試要求.
<?xml version="1.0" encoding="UTF8"?>
<suite name = "suite">
<listeners> <listener class-name="com.Transformer.OverrideTransformer"></listener> </listeners>
<test name="CloudPoint" > <parameter name="resp_code" value="200"/> <classes name = "Normal"> <class name="com.TestNg.CloudPoint.CP_Normal_1"/> <class name="com.TestNg.CloudPoint.CP_Normal_2"/> <class name="com.TestNg.CloudPoint.CP_Exception_androidlost"/> <class name="com.TestNg.CloudPoint.CP_Exception_kvlost"/> </classes> </test>
<test name="DDL" > <parameter name="resp_code" value="200"/> <classes name = "Normal"> <class name="com.TestNg.DDL.DDL_Normal_1"/> </classes> </test>
</suite>
4.運行build.xml,標紅部分是重試函數對失敗用例的運行結果的輸出
Buildfile: D:\java\workspaces\GroupApi\build.xml
clean:
[delete] Deleting directory D:\java\workspaces\GroupApi\bin
compile:
[echo] mkdir
[mkdir] Created dir: D:\java\workspaces\GroupApi\bin
[javac] Compiling 11 source files to D:\java\workspaces\GroupApi\bin
run:
[testng] [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] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
[testng] log4j:WARN Please initialize the log4j system properly.
[testng] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] true
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] true
[testng] excelread:{} is String
[testng] data:{} is String
[testng] D:\java\workspaces\GroupApi
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] [TestResult name=StatusCode status=FAILURE method=DDL_Normal_1.StatusCode()[pri:0, instance:com.TestNg.DDL.DDL_Normal_1@50f8360d] output={null}] [testng] [TestResult name=StatusCode status=FAILURE method=DDL_Normal_1.StatusCode()[pri:0, instance:com.TestNg.DDL.DDL_Normal_1@50f8360d] output={null}] [testng] [TestResult name=StatusCode status=FAILURE method=DDL_Normal_1.StatusCode()[pri:0, instance:com.TestNg.DDL.DDL_Normal_1@50f8360d] output={null}]
[testng] ===============================================
[testng] suite
[testng] Total tests run: 11, Failures: 1, Skips: 2
[testng] ===============================================
[testng] The tests failed.
[xslt] Processing D:\java\workspaces\GroupApi\test-output\testng-results.xml to D:\java\workspaces\GroupApi\test-output\index1.html
[xslt] Loading stylesheet D:\java\workspaces\GroupApi\test-output\testng-results.xsl
BUILD SUCCESSFUL
Total time: 8 seconds
5.測試報告輸出