如何使用json格式自定义TestNG报告结果 [英] how to customize TestNG report result with json format

查看:19
本文介绍了如何使用json格式自定义TestNG报告结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TestNG 和 selenium webdriver java.我想做一个json格式的输出报告,testng可以做一个json格式的报告吗?请给我举个例子.

解决方案

您可以编写自己的报告程序,以您需要的任何格式生成测试执行报告.这是一个简单的 json 报告器示例

包测试;导入 java.io.FileWriter;导入 java.io.IOException;导入 java.util.List;导入 java.util.Set;导入 org.json.simple.JSONArray;导入 org.json.simple.JSONObject;导入 org.testng.IReporter;导入 org.testng.ISuite;导入 org.testng.ITestContext;导入 org.testng.ITestResult;导入 org.testng.xml.XmlSuite;公共类 Reporter 实现 IReporter {@SuppressWarnings("未选中")public void generateReport(List xmlSuites, Listsuites, String outputDirectory) {JSONArray 结果 = new JSONArray();suites.forEach(元素->{results.add(createSuiteJsonObject(element));});试试 (FileWriter file = new FileWriter(outputDirectory + "/report.json")) {file.write(results.toJSONString());} catch (IOException e) {//处理}}@SuppressWarnings("未选中")公共 JSONObject createSuiteJsonObject(ISuite 套件){JSONObject 结果 = 新 JSONObject();JSONArraypassedMethods = new JSONArray();JSONArray failedMethods = new JSONArray();JSONArray skippedMethods = new JSONArray();Suite.getResults().entrySet().forEach(element -> {ITestContext context = element.getValue().getTestContext();passedMethods.addAll(createResultJsonArray(context.getPassedTests().getAllResults()));failedMethods.addAll(createResultJsonArray(context.getFailedTests().getAllResults()));skippedMethods.addAll(createResultJsonArray(context.getSkippedTests().getAllResults()));});result.put("name",suite.getName());result.put("passed",passedMethods);result.put("failed", failedMethods);result.put("跳过", skippedMethods);返回结果;}@SuppressWarnings("未选中")公共 JSONArray createResultJsonArray(Set results) {JSONArray 结果 = new JSONArray();结果.forEach(元素->{JSONObject currentJsonResult = new JSONObject();currentJsonResult.put("name", element.getName());result.add(currentJsonResult);});返回结果;}}

及其生成的报告示例

<预><代码>[{"name": "Suite1",通过":[{名称":测试1"}],失败的": [{名称":测试2"},{名称":测试3"}],跳过":[{名称":测试4"}]}]

I am working with TestNG and selenium webdriver java. I want to make a output report in json format, can testng make a report with json format?Please suggest me a example for this issue.

解决方案

You can write your own reporter that will produce test execution reports in whatever format you need. Here is an example of simple json reporter

package test;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.xml.XmlSuite;

public class Reporter implements IReporter {

    @SuppressWarnings("unchecked")
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        JSONArray results = new JSONArray();
        suites.forEach(element->{
            results.add(createSuiteJsonObject(element));
        });
        try (FileWriter file = new FileWriter(outputDirectory + "/report.json")) {
            file.write(results.toJSONString());
        } catch (IOException e) {
            //handle
        }

    }

    @SuppressWarnings("unchecked")
    public JSONObject createSuiteJsonObject(ISuite suite) {
        JSONObject result = new JSONObject();
        JSONArray passedMethods = new JSONArray();
        JSONArray failedMethods = new JSONArray();
        JSONArray skippedMethods = new JSONArray();
        suite.getResults().entrySet().forEach(element -> {
            ITestContext context = element.getValue().getTestContext();
            passedMethods.addAll(createResultJsonArray(context.getPassedTests().getAllResults()));
            failedMethods.addAll(createResultJsonArray(context.getFailedTests().getAllResults()));
            skippedMethods.addAll(createResultJsonArray(context.getSkippedTests().getAllResults()));
        });
        result.put("name", suite.getName());
        result.put("passed", passedMethods);
        result.put("failed", failedMethods);
        result.put("skipped", skippedMethods);
        return result;
    }

    @SuppressWarnings("unchecked")
    public JSONArray createResultJsonArray(Set<ITestResult> results) {
        JSONArray result = new JSONArray();
        results.forEach(element ->{
            JSONObject currentJsonResult = new JSONObject();
            currentJsonResult.put("name", element.getName());
            result.add(currentJsonResult);
        });
        return result;
    }


}

And example of the report produced by it

[
    {
        "name": "Suite1",
        "passed": [
            {
                "name": "test1"
            }
        ],
        "failed": [
            {
                "name": "test2"
            },
            {
                "name": "test3"
            }
        ],
        "skipped": [
            {
                "name": "test4"
            }
        ]
    }
]

这篇关于如何使用json格式自定义TestNG报告结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆