前言
一,為什么要用監(jiān)聽器衡招??州刽?
Testng雖然提供了不少強(qiáng)大的功能和靈活的選項(xiàng)浪箭,但不能解決所有的問題,而監(jiān)聽器就是用來定制額外的功能以滿足我們的需求的匹表;
二宣鄙,監(jiān)聽器的實(shí)際是什么?流椒?明也?
監(jiān)聽器實(shí)際上是一些預(yù)定義的java接口,用戶創(chuàng)建這些接口的實(shí)現(xiàn)類(即implements某監(jiān)聽接口绣硝,并實(shí)現(xiàn)里面的方法)撑刺,并加入到testng中,testng便會在運(yùn)行的不同時(shí)刻調(diào)用這些類中你自定義實(shí)現(xiàn)的接口方法甫菠,從而實(shí)現(xiàn)定制額外的功能;
監(jiān)聽器種類:
? IAnnotationTransformer(修改@Test注釋屬性)
只能用來修改 @Test注解的注釋屬性
void transform(ITest annotation, Class testClass, Constructor testConstructor, Method testMethod);
annotation 代表就是為 testMethod 定義的 @Test 注釋拂苹。調(diào)用其方法可以更改 @Test 注釋屬性痰洒。例如:annotation.setEnabled(false);
便會在運(yùn)行時(shí)將屬性 enabled 改為 false 從而禁用了當(dāng)前的測試方法。
? IAnnotationTransformer2(修改其他注解的注釋屬性)
*用來修改除 @Test 以外的 TestNG 的注釋脯宿,例如:@DataProvider 以及 @Factory 等注解的注釋屬性能夠通過該監(jiān)聽器修改泉粉;
void transform(IDataProviderAnnotation annotation, java.lang.reflect.Method method)
void transform(IFactoryAnnotation annotation, java.lang.reflect.Method method)
? IHookable(類似與面向方面編程(AOP)中的 Around Advice 的功能)
*用來在測試方法執(zhí)行前后提供了切入點(diǎn)嗡靡,從而使用戶能夠在測試方法運(yùn)行前后注入特定的功能
void run(IHookCallBack callBack, ITestResult testResult)
? IInvokedMethodListener(類似與面向方面編程(AOP)中的 Before Advice 和 After Advice 的功能)
*在當(dāng)前測試方法被執(zhí)行前和執(zhí)行后注入特定的邏輯
void afterInvocation(IInvokedMethod method, ITestResult testResult)
void beforeInvocation(IInvokedMethod method, ITestResult testResult)
常用來加入日志
? ImethodInterceptor(控制列表執(zhí)行循序)
TestNG 啟動之后,所有的測試方法分成兩類:
一類是順序運(yùn)行的測試方法财边;(dependsOnGroups 和 dependsOnMethods)
一類是沒有特定運(yùn)行順序的測試方法点骑。(運(yùn)行順序是隨機(jī),每次運(yùn)行的順序都可能不同)
IMethodInterceptor 監(jiān)聽器用來對第二類測試有更大的控制權(quán)憨募,對列表重新排序袁辈,甚至增加或者減少測試方法;
java.util.List<IMethodInstance> intercept(java.util.List<IMethodInstance> methods, ITestContext context)
實(shí)現(xiàn)的intercept 方法會在所有測試方法被分類后以及所有測試方法被執(zhí)行前被調(diào)用尾膊。所有的測試方法將按照 intercept 返回值列表中的順序被執(zhí)行荞彼。
? IReporter(自定義測試報(bào)表)
IReporter 監(jiān)聽器用來自定義測試報(bào)表;
void generateReport(java.util.List<XmlSuite> xmlSuites, java.util.List<ISuite> suites, java.lang.String outputDirectory)
generateReport方法會在所有測試方法執(zhí)行結(jié)束后被調(diào)用抓谴,通過遍歷 xmlSuites 和 suites 能夠獲取所有測試方法的信息以及測試結(jié)果。outputDirectory 是默認(rèn)的測試報(bào)表生成路徑仰泻,當(dāng)然你可以指定其他路徑生成報(bào)表
? ISuiteListener(suite的AOP)
ISuiteListener 使用戶有機(jī)會在測試套件開始執(zhí)行以及執(zhí)行結(jié)束之后嵌入自己的邏輯滩届;
類似,但區(qū)別IInvokedMethodListener 針對的是測試方法丐吓,而 ISuiteListener 針對的是測試套件
void onFinish(ISuite suite)
void onStart(ISuite suite)
? ITestListener(簡便自定義測試方法執(zhí)行后的后續(xù)行為)
用來在測試方法執(zhí)行成功券犁、失敗或者跳過時(shí)指定不同后續(xù)行為;
IInvokedMethodListener 也可以實(shí)現(xiàn)粘衬,但I(xiàn)TestListener更簡便咳促;
void onTestFailure(ITestResult result)
void onTestSkipped(ITestResult result)
void onTestSuccess(ITestResult result)
另外:TestListenerAdapter 已經(jīng)實(shí)現(xiàn) ITestListener,并且提供了一些有用的方法褂删,比如分別獲取所有成功失敗跳過三種測試結(jié)果的測試方法的方法冲茸,并且 ITestListner 中有很多方法而 TestListenerAdapter 已給出了默認(rèn)實(shí)現(xiàn)。因此难衰,繼承 TestListenerAdapter 后逗栽,便只需關(guān)注需要修改的方法。
監(jiān)聽器使用方法:
一鳄虱,在 testng.xml 中使用 TestNG 監(jiān)聽器
<listeners>
<listener class-name="api.listeners.AutoTestListener"></listener>
<listener class-name="api.listeners.RetryListener"></listener>
<listener class-name="api.listeners.ExtentTestNGIReporterListener"/>
</listeners>
二凭峡,在源代碼中使用 TestNG 監(jiān)聽器
通過 @Listeners 注釋,可以直接在 Java 源代碼中添加 TestNG 監(jiān)聽器
@Listeners(RetryListener.class)
public class SampleTest {
@Test()
public void test1() {
sleep(6000);
System.out.println("test1");
}
*注意:
1悠栓,在 @Listeners 中添加監(jiān)聽器跟在 testng.xml 添加監(jiān)聽器一樣,將被應(yīng)用到整個測試套件中的測試方法惭适。如果需要控制監(jiān)聽器的應(yīng)用范圍(比如添加的監(jiān)聽器僅使用于某些測試測試類或者某些測試方法),則必須在監(jiān)聽器類中編寫適當(dāng)?shù)呐袛噙壿嫛?br>
2往枷,在 @Listeners 中添加監(jiān)聽器跟在 testng.xml 添加監(jiān)聽器的不同之處在于凄杯,它不能添加 IAnnotationTransformer 和 IAnnotationTransformer2 監(jiān)聽器。原因是因?yàn)檫@兩種監(jiān)聽器必須在更早的階段添加到 TestNG 中才能實(shí)施修改注釋的操作屯碴,所以它們只能在 testng.xml 添加膊存。
3,TestNG 對添加的監(jiān)聽器不做去重判斷今艺。因此爵卒,如果 testng.xml 和源代碼中添加了相同的監(jiān)聽器,該監(jiān)聽器的方法會被調(diào)用兩次钓株。有關(guān)這一點(diǎn),大家可以通過運(yùn)行本文附帶的示例代碼包中 testng.xml 驗(yàn)證铲掐。因此值桩,切記,不要通過多種方式重復(fù)添加監(jiān)聽器携栋。
三咳秉,通過 ServiceLoader 使用 TestNG 監(jiān)聽器
創(chuàng)建一個 jar 文件,里面包含 TestNG 監(jiān)聽器的實(shí)現(xiàn)類以及 ServiceLoader 需要的配置信息向挖,并在運(yùn)行 TestNG 時(shí)把該 jar 文件加載到類路徑中。具體步驟請查閱 TestNG 官方文檔
四何之,通過命令行使用 TestNG 監(jiān)聽器
java org.testng.TestNG -listener MyListener testng1.xml [testng2.xml testng3.xml ...]
摘自:TestNG 官方文檔,實(shí)戰(zhàn) TestNG 監(jiān)聽器
以上~對你有幫助的話,點(diǎn)個喜歡??吧~~