testng自定义html报告

目录

实现原理

源码展示

定义测试结果类TestResult

定义 TestResultCollection 集合类

自定义测试报告模板report.vm

扩展实现IReporter接口

工具类


testng自带的报告,有如下几个问题:

1. 不是很美观

2.html报告中js是外置的,在集成jenkins和浏览器兼容性上均存在一些小问题。因此决定自定义一个测试报告。

      自定义的报告截图如下

实现原理

testng对外提供了很多扩展接口,其中测试报告的扩展接口为IReporter接口

package org.testng;

import java.util.List;
import org.testng.xml.XmlSuite;

/*实现这个接口,并且配置testng监听器就可以了*/
public interface IReporter extends ITestNGListener {
    default void generateReport(List<XmlSuite> var1, List<ISuite> var2, String var3) {
    }
}

因此实现该接口,并且在testng框架里面配置listener即可,关于监听器的配置,请参照 https://testng.org/doc/documentation-main.html#testng-listeners

本扩展程序就是实现该接口,并且自定义html模板,最终通过Velocity渲染出html报告

源码展示

定义测试结果类TestResult

用于存储测试结果

package org.clearfuny.funnytest.interner.reporter;

import org.clearfuny.funnytest.util.ExceptionUtil;

import java.util.List;

public class TestResult {

    private String testName; //测试方法名
    private String className; //测试类名
    private String caseName;
    private String params; //测试用参数
    private String description; //测试描述
    private List<String> output; //Reporter Output
    private Throwable throwable; //测试异常原因
    private String throwableTrace;
    private int status; //状态
    private String duration;

    private boolean success;


    public String getTestName() {
        return testName;
    }

    public void setTestName(String testName) {
        this.testName = testName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getCaseName() {
        return caseName;
    }

    public void setCaseName(String caseName) {
        this.caseName = caseName;
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<String> getOutput() {
        return output;
    }

    public void setOutput(List<String> output) {
        this.output = output;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
        this.throwableTrace = ExceptionUtil.getStackTrace(throwable);
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getThrowableTrace() {
        return throwableTrace;
    }

    public void setThrowableTrace(String throwableTrace) {
        this.throwableTrace = throwableTrace;
    }
}

定义 TestResultCollection 集合类

testng采用数据驱动,一个测试类可以有多个测试用例集合,每个测试类,应该有个测试结果集

package org.clearfuny.funnytest.interner.reporter;


import org.testng.ITestResult;

import java.util.LinkedList;
import java.util.List;

public class TestResultCollection {

    private int totalSize = 0;

    private int successSize = 0;

    private int failedSize = 0;

    private int errorSize = 0;

    private int skippedSize = 0;

    private List<TestResult> resultList;


    public void addTestResult(TestResult result) {
        if (resultList == null) {
            resultList = new LinkedList<>();
        }
        resultList.add(result);

        switch (result.getStatus()) {
            case ITestResult.FAILURE:
                failedSize+=1;
                break;
            case ITestResult.SUCCESS:
                successSize+=1;
                break;
            case ITestResult.SKIP:
                skippedSize+=1;
                break;
        }

        totalSize+=1;
    }

    /*===============================[getter && setter]=================================*/

    public int getTotalSize() {
        return totalSize;
    }

    public void set
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值