forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt_junit.go
195 lines (164 loc) · 4.49 KB
/
fmt_junit.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package godog
import (
"encoding/xml"
"fmt"
"io"
"os"
"time"
"github.com/DATA-DOG/godog/gherkin"
)
func init() {
Format("junit", "Prints junit compatible xml to stdout", junitFunc)
}
func junitFunc(suite string, out io.Writer) Formatter {
return &junitFormatter{
suite: &junitPackageSuite{
Name: suite,
TestSuites: make([]*junitTestSuite, 0),
},
out: out,
started: time.Now(),
}
}
type junitFormatter struct {
suite *junitPackageSuite
out io.Writer
// timing
started time.Time
caseStarted time.Time
featStarted time.Time
outline *gherkin.ScenarioOutline
outlineExample int
}
func (j *junitFormatter) Feature(feature *gherkin.Feature, path string, c []byte) {
testSuite := &junitTestSuite{
TestCases: make([]*junitTestCase, 0),
Name: feature.Name,
}
if len(j.suite.TestSuites) > 0 {
j.current().Time = time.Since(j.featStarted).String()
}
j.featStarted = time.Now()
j.suite.TestSuites = append(j.suite.TestSuites, testSuite)
}
func (j *junitFormatter) Defined(*gherkin.Step, *StepDef) {
}
func (j *junitFormatter) Node(node interface{}) {
suite := j.current()
tcase := &junitTestCase{}
switch t := node.(type) {
case *gherkin.ScenarioOutline:
j.outline = t
return
case *gherkin.Scenario:
tcase.Name = t.Name
suite.Tests++
j.suite.Tests++
case *gherkin.Examples:
j.outlineExample = 0
return
case *gherkin.TableRow:
j.outlineExample++
tcase.Name = fmt.Sprintf("%s #%d", j.outline.Name, j.outlineExample)
suite.Tests++
j.suite.Tests++
default:
return
}
if len(suite.TestCases) > 0 {
suite.current().Time = time.Since(j.caseStarted).String()
}
j.caseStarted = time.Now()
suite.TestCases = append(suite.TestCases, tcase)
}
func (j *junitFormatter) Failed(step *gherkin.Step, match *StepDef, err error) {
suite := j.current()
suite.Failures++
j.suite.Failures++
tcase := suite.current()
tcase.Status = "failed"
tcase.Failure = &junitFailure{
Message: fmt.Sprintf("%s %s: %s", step.Type, step.Text, err.Error()),
}
}
func (j *junitFormatter) Passed(step *gherkin.Step, match *StepDef) {
suite := j.current()
tcase := suite.current()
tcase.Status = "passed"
}
func (j *junitFormatter) Skipped(step *gherkin.Step) {
}
func (j *junitFormatter) Undefined(step *gherkin.Step) {
suite := j.current()
suite.Errors++
j.suite.Errors++
tcase := suite.current()
tcase.Status = "undefined"
tcase.Error = append(tcase.Error, &junitError{
Type: "undefined",
Message: fmt.Sprintf("%s %s", step.Type, step.Text),
})
}
func (j *junitFormatter) Pending(step *gherkin.Step, match *StepDef) {
suite := j.current()
suite.Errors++
j.suite.Errors++
tcase := suite.current()
tcase.Status = "pending"
tcase.Error = append(tcase.Error, &junitError{
Type: "pending",
Message: fmt.Sprintf("%s %s: TODO: write pending definition", step.Type, step.Text),
})
}
func (j *junitFormatter) Summary() {
j.suite.Time = time.Since(j.started).String()
io.WriteString(j.out, xml.Header)
enc := xml.NewEncoder(j.out)
enc.Indent("", s(2))
if err := enc.Encode(j.suite); err != nil {
fmt.Fprintln(os.Stderr, "failed to write junit xml:", err)
}
}
type junitFailure struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr,omitempty"`
}
type junitError struct {
XMLName xml.Name `xml:"error,omitempty"`
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
}
type junitTestCase struct {
XMLName xml.Name `xml:"testcase"`
Name string `xml:"name,attr"`
Status string `xml:"status,attr"`
Time string `xml:"time,attr"`
Failure *junitFailure `xml:"failure,omitempty"`
Error []*junitError
}
type junitTestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Skipped int `xml:"skipped,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Time string `xml:"time,attr"`
TestCases []*junitTestCase
}
func (ts *junitTestSuite) current() *junitTestCase {
return ts.TestCases[len(ts.TestCases)-1]
}
type junitPackageSuite struct {
XMLName xml.Name `xml:"testsuites"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Skipped int `xml:"skipped,attr"`
Failures int `xml:"failures,attr"`
Errors int `xml:"errors,attr"`
Time string `xml:"time,attr"`
TestSuites []*junitTestSuite
}
func (j *junitFormatter) current() *junitTestSuite {
return j.suite.TestSuites[len(j.suite.TestSuites)-1]
}