forked from gotestyourself/gotestsum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
144 lines (126 loc) · 3.68 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*Package junitxml creates a JUnit XML report from a testjson.Execution.
*/
package junitxml
import (
"encoding/xml"
"io"
"path/filepath"
"runtime"
"github.com/pkg/errors"
"gotest.tools/gotestsum/testjson"
)
// JUnitTestSuites is a collection of JUnit test suites.
type JUnitTestSuites struct {
XMLName xml.Name `xml:"testsuites"`
Suites []JUnitTestSuite
}
// JUnitTestSuite is a single JUnit test suite which may contain many
// testcases.
type JUnitTestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Time string `xml:"time,attr"`
Name string `xml:"name,attr"`
Properties []JUnitProperty `xml:"properties>property,omitempty"`
TestCases []JUnitTestCase
}
// JUnitTestCase is a single test case with its result.
type JUnitTestCase struct {
XMLName xml.Name `xml:"testcase"`
Classname string `xml:"classname,attr"`
Name string `xml:"name,attr"`
Time string `xml:"time,attr"`
SkipMessage *JUnitSkipMessage `xml:"skipped,omitempty"`
Failure *JUnitFailure `xml:"failure,omitempty"`
}
// JUnitSkipMessage contains the reason why a testcase was skipped.
type JUnitSkipMessage struct {
Message string `xml:"message,attr"`
}
// JUnitProperty represents a key/value pair used to define properties.
type JUnitProperty struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
// JUnitFailure contains data related to a failed test.
type JUnitFailure struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Contents string `xml:",chardata"`
}
// Write creates an XML document and writes it to out.
func Write(out io.Writer, exec *testjson.Execution) error {
return errors.Wrap(write(out, generate(exec)), "failed to write JUnit XML")
}
func generate(exec *testjson.Execution) JUnitTestSuites {
suites := JUnitTestSuites{}
for _, pkgname := range exec.Packages() {
pkg := exec.Package(pkgname)
junitpkg := JUnitTestSuite{
Name: pkgname,
Tests: pkg.Total,
Time: testjson.FormatDurationAsSeconds(pkg.Elapsed(), 3),
Properties: packageProperties(),
TestCases: packageTestCases(pkg),
Failures: len(pkg.Failed),
}
suites.Suites = append(suites.Suites, junitpkg)
}
return suites
}
func packageProperties() []JUnitProperty {
return []JUnitProperty{
{Name: "go.version", Value: runtime.Version()},
}
}
func packageTestCases(pkg *testjson.Package) []JUnitTestCase {
cases := []JUnitTestCase{}
if pkg.TestMainFailed() {
jtc := newJUnitTestCase(testjson.TestCase{
Test: "TestMain",
})
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: pkg.Output(""),
}
cases = append(cases, jtc)
}
for _, tc := range pkg.Failed {
jtc := newJUnitTestCase(tc)
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: pkg.Output(tc.Test),
}
cases = append(cases, jtc)
}
for _, tc := range pkg.Skipped {
jtc := newJUnitTestCase(tc)
jtc.SkipMessage = &JUnitSkipMessage{Message: pkg.Output(tc.Test)}
cases = append(cases, jtc)
}
for _, tc := range pkg.Passed {
jtc := newJUnitTestCase(tc)
cases = append(cases, jtc)
}
return cases
}
func newJUnitTestCase(tc testjson.TestCase) JUnitTestCase {
return JUnitTestCase{
Classname: filepath.Base(tc.Package),
Name: tc.Test,
Time: testjson.FormatDurationAsSeconds(tc.Elapsed, 3),
}
}
func write(out io.Writer, suites JUnitTestSuites) error {
doc, err := xml.MarshalIndent(suites, "", "\t")
if err != nil {
return err
}
_, err = out.Write([]byte(xml.Header))
if err != nil {
return err
}
_, err = out.Write(doc)
return err
}