Skip to content

Commit 0bb4a82

Browse files
teresaromeroefd6
andauthored
New test result format json (#3101)
* feat: add JSON format support for test reports and update file output extension * Update internal/testrunner/reporters/formats/json.go Co-authored-by: Dan Kortschak <[email protected]> --------- Co-authored-by: Dan Kortschak <[email protected]>
1 parent 9887ae1 commit 0bb4a82

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

internal/cobraext/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ const (
160160
ProfileFormatFlagDescription = "format of the profiles list (table | json)"
161161

162162
ReportFormatFlagName = "report-format"
163-
ReportFormatFlagDescription = "format of test report, eg: human, xUnit"
163+
ReportFormatFlagDescription = "format of test report, eg: human, xUnit, json"
164164

165165
ReportFullFlagName = "full"
166166
ReportFullFlagDescription = "whether to show the full report or a summary"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package formats
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
11+
"github.com/elastic/elastic-package/internal/testrunner"
12+
)
13+
14+
func init() {
15+
testrunner.RegisterReporterFormat(ReportFormatJSON, reportJSONFormat)
16+
}
17+
18+
const (
19+
// ReportFormatJSON reports test results in a JSON format
20+
ReportFormatJSON testrunner.TestReportFormat = "json"
21+
)
22+
23+
type jsonResult struct {
24+
Package string `json:"package"`
25+
DataStream string `json:"data_stream,omitempty"`
26+
TestType string `json:"test_type"`
27+
Name string `json:"name"`
28+
Result string `json:"result"`
29+
TimeElapsed string `json:"time_elapsed"`
30+
FailureDetails string `json:"failure_details,omitempty"`
31+
}
32+
33+
func reportJSONFormat(results []testrunner.TestResult) (string, error) {
34+
if len(results) == 0 {
35+
return "No test results", nil
36+
}
37+
38+
jsonReport := make([]jsonResult, 0, len(results))
39+
for _, r := range results {
40+
jsonResult := jsonResult{
41+
Package: r.Package,
42+
DataStream: r.DataStream,
43+
TestType: string(r.TestType),
44+
Name: r.Name,
45+
TimeElapsed: r.TimeElapsed.String(),
46+
}
47+
48+
if r.FailureMsg != "" {
49+
jsonResult.FailureDetails = fmt.Sprintf("%s/%s %s:\n%s\n", r.Package, r.DataStream, r.Name, r.FailureDetails)
50+
}
51+
52+
var result string
53+
if r.ErrorMsg != "" {
54+
result = fmt.Sprintf("ERROR: %s", r.ErrorMsg)
55+
} else if r.FailureMsg != "" {
56+
result = fmt.Sprintf("FAIL: %s", r.FailureMsg)
57+
} else if r.Skipped != nil {
58+
result = fmt.Sprintf("SKIPPED: %s", r.Skipped)
59+
} else {
60+
result = "PASS"
61+
}
62+
jsonResult.Result = result
63+
64+
jsonReport = append(jsonReport, jsonResult)
65+
}
66+
67+
b, err := json.Marshal(jsonReport)
68+
if err != nil {
69+
return "", fmt.Errorf("marshaling test results to JSON: %w", err)
70+
}
71+
return string(b), nil
72+
}

internal/testrunner/reporters/outputs/file.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ func reportToFile(pkg, report string, testType testrunner.TestType, format testr
3939
}
4040

4141
ext := "txt"
42-
if format == formats.ReportFormatXUnit {
42+
switch format {
43+
case formats.ReportFormatXUnit:
4344
ext = "xml"
45+
case formats.ReportFormatJSON:
46+
ext = "json"
4447
}
4548

4649
fileName := fmt.Sprintf("%s-%s-%d.%s", pkg, testType, time.Now().UnixNano(), ext)

0 commit comments

Comments
 (0)