Jtester之TestNG:@Listener注解

实现一个简单的监听器。dubbo接口测试中,若zookeeper注册中心连接超时导致接口调用失败,将用例执行状态改成SKIPPED。需要做的就是,重写org.testng.ITestListener接口下的onTestFailure方法,用例执行失败时,接受抛出的异常链,判断最初始的异常是否为ZkTimeoutException类型,如果是,将状态置成3-SKIPPED。

一、监听器实现

package org.smk.util.testnglistener;

import org.I0Itec.zkclient.exception.ZkTimeoutException;
import org.testng.ITestContext;
import org.testng.ITestResult;

public class DubboTestListener implements org.testng.ITestListener {

    @Override
    public void onTestStart(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestSuccess(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestFailure(ITestResult result) {
        // TODO Auto-generated method stub
        Throwable exp = result.getThrowable();
        if (exp != null) {
            while (exp.getCause() != null) {
                exp = exp.getCause();
            }
            if (exp.getClass().getName() == "org.I0Itec.zkclient.exception.ZkTimeoutException") {
                result.setStatus(3);
            }
        }
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStart(ITestContext context) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish(ITestContext context) {
        // TODO Auto-generated method stub

    }

}

二、执行情况

Jtester之TestNG:@Listener注解_第1张图片

你可能感兴趣的:(Jtester之TestNG:@Listener注解)