我正在使用TestNG和selenium webdriver java。我想用json格式制作输出报告,可以用json格式生成报告吗?请给我一个这个问题的例子。
答案 0 :(得分:4)
您可以编写自己的记者,以任何您需要的格式生成测试执行报告。这是一个简单的json记者的例子
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;
}
}
由它制作的报告的例子
[
{
"name": "Suite1",
"passed": [
{
"name": "test1"
}
],
"failed": [
{
"name": "test2"
},
{
"name": "test3"
}
],
"skipped": [
{
"name": "test4"
}
]
}
]
答案 1 :(得分:0)
如果您使用POJO类存储输出响应,则可以使用以下实现重写同一个POJO类中的toString()方法。
@Override
public String toString() {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
try {
return mapper.writeValueAsString(this).replaceAll("\\n", "<br>").replaceAll("\\s", " ");
} catch (JsonProcessingException e) {
return e.getMessage();
}
}
然后,如果您在测试方法中提供以下代码
Reporter.log(pojoClass.toString());
您将在Index.html文件的Reporter输出部分中获得JSON格式的结果,如下所示
{
"status" : "success",
"data" : [ {
"id" : 1,
"employee_name" : "Tiger Nixon",
"employee_salary" : 320800,
"employee_age" : 61,
"profile_image" : ""
}, {
"id" : 2,
"employee_name" : "Garrett Winters",
"employee_salary" : 170750,
"employee_age" : 63,
"profile_image" : ""
}, {
"id" : 3,
"employee_name" : "Ashton Cox",
"employee_salary" : 86000,
"employee_age" : 66,
"profile_image" : ""
}, {
"id" : 4,
"employee_name" : "Cedric Kelly",
"employee_salary" : 433060,
"employee_age" : 22,
"profile_image" : ""
}, {
"id" : 5,
"employee_name" : "Airi Satou",
"employee_salary" : 162700,
"employee_age" : 33,
"profile_image" : ""
}, {
"id" : 6,
"employee_name" : "Brielle Williamson",
"employee_salary" : 372000,
"employee_age" : 61,
"profile_image" : ""
}, {
"id" : 7,
"employee_name" : "Herrod Chandler",
"employee_salary" : 137500,
"employee_age" : 59,
"profile_image" : ""
}, {
"id" : 8,
"employee_name" : "Rhona Davidson",
"employee_salary" : 327900,
"employee_age" : 55,
"profile_image" : ""
}, {
"id" : 9,
"employee_name" : "Colleen Hurst",
"employee_salary" : 205500,
"employee_age" : 39,
"profile_image" : ""
}, {
"id" : 10,
"employee_name" : "Sonya Frost",
"employee_salary" : 103600,
"employee_age" : 23,
"profile_image" : ""
}, {
"id" : 11,
"employee_name" : "Jena Gaines",
"employee_salary" : 90560,
"employee_age" : 30,
"profile_image" : ""
}, {
"id" : 12,
"employee_name" : "Quinn Flynn",
"employee_salary" : 342000,
"employee_age" : 22,
"profile_image" : ""
}, {
"id" : 13,
"employee_name" : "Charde Marshall",
"employee_salary" : 470600,
"employee_age" : 36,
"profile_image" : ""
}, {
"id" : 14,
"employee_name" : "Haley Kennedy",
"employee_salary" : 313500,
"employee_age" : 43,
"profile_image" : ""
}, {
"id" : 15,
"employee_name" : "Tatyana Fitzpatrick",
"employee_salary" : 385750,
"employee_age" : 19,
"profile_image" : ""
}, {
"id" : 16,
"employee_name" : "Michael Silva",
"employee_salary" : 198500,
"employee_age" : 66,
"profile_image" : ""
}, {
"id" : 17,
"employee_name" : "Paul Byrd",
"employee_salary" : 725000,
"employee_age" : 64,
"profile_image" : ""
}, {
"id" : 18,
"employee_name" : "Gloria Little",
"employee_salary" : 237500,
"employee_age" : 59,
"profile_image" : ""
}, {
"id" : 19,
"employee_name" : "Bradley Greer",
"employee_salary" : 132000,
"employee_age" : 41,
"profile_image" : ""
}, {
"id" : 20,
"employee_name" : "Dai Rios",
"employee_salary" : 217500,
"employee_age" : 35,
"profile_image" : ""
}, {
"id" : 21,
"employee_name" : "Jenette Caldwell",
"employee_salary" : 345000,
"employee_age" : 30,
"profile_image" : ""
}, {
"id" : 22,
"employee_name" : "Yuri Berry",
"employee_salary" : 675000,
"employee_age" : 40,
"profile_image" : ""
}, {
"id" : 23,
"employee_name" : "Caesar Vance",
"employee_salary" : 106450,
"employee_age" : 21,
"profile_image" : ""
}, {
"id" : 24,
"employee_name" : "Doris Wilder",
"employee_salary" : 85600,
"employee_age" : 23,
"profile_image" : ""
} ]
}