面向百度编程之-测试报告

2018-04-19  本文已影响93人  abaeccdce206

官方网站在这里:http://extentreports.relevantcodes.com/
当年github上开源,如今pro版本居然收钱了
先说总结:

1. 生成的报告简洁美观,

2.生成的单html方便jenkins集成发邮件

3.自带集中展示历史报告的服务端

4.支持java,

5.可定制报告

6.数据入库

官网提供V2.x版本和V3.x版本,只支持java8。注意

客户端地址:https://github.com/anshooarora/extentreports-java/commits/master

服务端地址:https://github.com/anshooarora/extentx

安装过程,官方文档上比较消息。

官方说明在这里:http://extentreports.com/docs/versions/3/java/, 提供了3种和testng集成示例:

1.直接在@BeforeSuite@BeforeClass进行初始化

2.自己实现testng的ITestListener接口,监听的适合你已经有测试狂简了,不想动原来的case

3.自己实现testng的IReporter接口,更加直观

以上随便选择一种都可以,实现ITestListener接口。内容类似下面:

public class ExtentTestNGITestListener implements ITestListener {

    private static ExtentReports extent = ExtentManager.getInstance("test-output/extent.html");
    private static ThreadLocal test = new ThreadLocal();
    public static MacacaClient driver;

    @Override
    public synchronized void onStart(ITestContext context) {
    }

    @Override
    public synchronized void onFinish(ITestContext context) {
        extent.flush();
    }

    @Override
    public synchronized void onTestStart(ITestResult result) {
        test.set(extent.createTest(result.getMethod().getMethodName()));
    }

    @Override
    public synchronized void onTestSuccess(ITestResult result) {
        ((ExtentTest)test.get()).pass("Test passed");
    }

    @Override
    public synchronized void onTestFailure(ITestResult result) {
        ((ExtentTest)test.get()).fail(result.getThrowable());
        File directory = new File("test-output");
        try {
            String screenPath = directory.getCanonicalPath() + "/";
            File file = new File(screenPath);
            if (!file.exists()){
                file.mkdirs();
            }
            String fileName = result.getMethod().getMethodName() + ".png";
            driver.saveScreenshot(screenPath + fileName);
            ((ExtentTest)test.get()).addScreenCaptureFromPath(screenPath + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public synchronized void onTestSkipped(ITestResult result) {
        ((ExtentTest)test.get()).skip(result.getThrowable());
    }

    @Override
    public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {

    }
}

onTestFailure函数里面实现了出错自动截图,调用的是ExtentTest的addScreenCaptureFromPath方法。

ExtentManager用来做初始化:

public class ExtentManager {
    private static ExtentReports extent;

    public static ExtentReports getInstance(String filePath) {
        if (extent == null)
            createInstance(filePath);
        return extent;
    }

    public static void createInstance(String filePath) {
        extent = new ExtentReports();
        extent.setSystemInfo("os", "Linux");
        extent.attachReporter(createHtmlReporter(filePath), createExtentXReporter());
    }

    public static ExtentHtmlReporter createHtmlReporter(String filePath){
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(filePath);
        //报表位置
        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        //使报表上的图表可见
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setDocumentTitle(filePath);
        htmlReporter.config().setEncoding("utf-8");
        htmlReporter.config().setReportName("XXX项目测试报告");
        return htmlReporter;
    }

    public static ExtentXReporter createExtentXReporter() {
        ExtentXReporter extentx = new ExtentXReporter("127.0.0.1",27017);
        extentx.config().setProjectName("test1");
        extentx.config().setReportName("Build-1224");
        extentx.config().setServerUrl("http://localhost:1337");
        return extentx;
    }

ExtentXReporter构造函数里填的是mongodb的地址和端口。

在res/testng.xml里面注册这个监听器,测试类也写上:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener class-name="com.util.extent.ExtentTestNGITestListener"></listener>
    </listeners>
    <test name="Test">
        <classes>
            <class name="macaca.client.ExtentTestNGReportBuilder"></class>
        </classes>
    </test>
</suite>

测试类调用,随便写2个case演示:macaca是阿里的自动化框架,也不错的,可以直接删除也行,内容罢了

public class ExtentTestNGReportBuilder {
    MacacaClient driver = new MacacaClient();

    @BeforeClass
    public synchronized void beforeClass() throws Exception {
        JSONObject porps = new JSONObject();
        porps.put("platformName", "android");
        porps.put("reuse", 1);
        porps.put("app", "https://npmcdn.com/android-app-bootstrap@latest/android_app_bootstrap/build/outputs/apk/android_app_bootstrap-debug.apk");
        JSONObject desiredCapabilities = new JSONObject();
        desiredCapabilities.put("desiredCapabilities", porps);
        driver.initDriver(desiredCapabilities);
        ExtentTestNGITestListener.driver = driver;
    }

    @AfterClass
    public synchronized void afterClass() throws Exception{
        driver.quit();
    }

    @Test
    public void test_1() throws Exception{
        assert 1==0;
    }

    @Test
    public void test_2() {
        assert 1 == 1;
    }

}

运行命令

mvn clean test

开始测试

生成的本地报告在test-output下面,内容类似下面:

最下面可以看到失败的截图:

image
只查看失败的case:
image
Dashboard页面:
image

看看服务端的报告(运行了多次):

image

image

汇总页面:

这里的PROJECT和BUILD是ExtentManager类里面

extentx.config().setProjectName("test1");
extentx.config().setReportName("Build-1224");

这里设定的,这里实际使用时可以用jenkins集成时直接由jenkins传进来

image

具体某一次的报告:

image

如果觉得这种报告太笨重了,可以看下这种
https://www.jianshu.com/p/ce09d3bd1c37
轻量级别的

ps:所有酷炫的测试报告,都不适合发邮件,因为内容太多了
适合发邮件的模板去掉各种渲染

上一篇 下一篇

猜你喜欢

热点阅读