本文主要是介绍TestNG的ITestListener,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
testng listener 类图
- 主要是继承
ItestListener
里面的方法。当自定义listener 时候,主要是使用这些方法与运行时的testng交互获取结果。invoke
方法的定义见下面所示。 - 后续开发者可编写自己的Listener进行对结果的再处理。 for example,testng类中有一个ExitCodeListener,其代码如下。
定义于 SuiteRunner这个上下文管理类中。(此方法用于invoke suiteListener, 并不是用于处理ItestListener)
private void invokeListeners(boolean start) {for (ISuiteListener sl : m_listeners) {if (start) {sl.onStart(this);}else {sl.onFinish(this);}}
ItestListener 的invoke, 是testng中的一个Invoker
类,
This class is responsible for invoking methods:
test methods ,configuration methods possibly in a separate thread and then for notifying the result listeners.
代码如下:
public static void runTestListeners(ITestResult tr, List<ITestListener> listeners) {for (ITestListener itl : listeners) {switch(tr.getStatus()) {case ITestResult.SKIP: {itl.onTestSkipped(tr);break;}case ITestResult.SUCCESS_PERCENTAGE_FAILURE: {itl.onTestFailedButWithinSuccessPercentage(tr);break;}case ITestResult.FAILURE: {itl.onTestFailure(tr);break;}case ITestResult.SUCCESS: {itl.onTestSuccess(tr);break;}case ITestResult.STARTED: {itl.onTestStart(tr);break;}default: {assert false : "UNKNOWN STATUS:" + tr;}}
ExitcodeListener
用于监测这个testng 整体有没有run m_hasTests
,以及run的结果m_status
是怎么样的。
private int m_status;
private boolean m_hasTests= false;
m_status有几种state:
- protected static final int HAS_FAILURE = 1;
- protected static final int HAS_SKIPPED = 2;
- protected static final int HAS_FSP = 4;
- protected static final int HAS_NO_TEST = 8;
exitcodeListener代码如下:
public static class ExitCodeListener implements IResultListener2 {private TestNG m_mainRunner;public ExitCodeListener() {m_mainRunner = TestNG.m_instance;}public ExitCodeListener(TestNG runner) {m_mainRunner = runner;}@Overridepublic void beforeConfiguration(ITestResult tr) {}@Overridepublic void onTestFailure(ITestResult result) {setHasRunTests();m_mainRunner.setStatus(HAS_FAILURE);}@Overridepublic void onTestSkipped(ITestResult result) {setHasRunTests();m_mainRunner.setStatus(HAS_SKIPPED);}@Overridepublic void onTestFailedButWithinSuccessPercentage(ITestResult result) {setHasRunTests();m_mainRunner.setStatus(HAS_FSP);}@Overridepublic void onTestSuccess(ITestResult result) {setHasRunTests();}@Overridepublic void onStart(ITestContext context) {setHasRunTests();}@Overridepublic void onFinish(ITestContext context) {}@Overridepublic void onTestStart(ITestResult result) {setHasRunTests();}private void setHasRunTests() {m_mainRunner.m_hasTests= true;}/*** @see org.testng.IConfigurationListener#onConfigurationFailure(org.testng.ITestResult)*/@Overridepublic void onConfigurationFailure(ITestResult itr) {m_mainRunner.setStatus(HAS_FAILURE);}/*** @see org.testng.IConfigurationListener#onConfigurationSkip(org.testng.ITestResult)*/@Overridepublic void onConfigurationSkip(ITestResult itr) {m_mainRunner.setStatus(HAS_SKIPPED);}/*** @see org.testng.IConfigurationListener#onConfigurationSuccess(org.testng.ITestResult)*/@Overridepublic void onConfigurationSuccess(ITestResult itr) {}
}
这篇关于TestNG的ITestListener的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!