-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathfmt_junit.go
246 lines (210 loc) · 6.35 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package formatters
import (
"encoding/xml"
"fmt"
"io"
"os"
"sort"
"strconv"
"time"
"github.com/cucumber/godog/formatters"
"github.com/cucumber/godog/internal/models"
"github.com/cucumber/godog/internal/utils"
)
func init() {
formatters.Format("junit", "Prints junit compatible xml to stdout", JUnitFormatterFunc)
}
// JUnitFormatterFunc implements the FormatterFunc for the junit formatter
func JUnitFormatterFunc(suite string, out io.Writer) formatters.Formatter {
return &JUnit{Base: NewBase(suite, out)}
}
// JUnit renders test results in JUnit format.
type JUnit struct {
*Base
}
// Summary renders summary information.
func (f *JUnit) Summary() {
suite := f.buildJUNITPackageSuite()
_, err := io.WriteString(f.out, xml.Header)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to write junit string:", err)
}
enc := xml.NewEncoder(f.out)
enc.Indent("", s(2))
if err = enc.Encode(suite); err != nil {
fmt.Fprintln(os.Stderr, "failed to write junit xml:", err)
}
}
func junitTimeDuration(from, to time.Time) string {
return strconv.FormatFloat(to.Sub(from).Seconds(), 'f', -1, 64)
}
// getPickleResult deals with the fact that if there's no result due to 'StopOnFirstFailure' being
// set, MustGetPickleResult panics.
func (f *JUnit) getPickleResult(pickleID string) (res *models.PickleResult) {
defer func() {
if r := recover(); r != nil {
res = nil
}
}()
pr := f.Storage.MustGetPickleResult(pickleID)
res = &pr
return
}
func (f *JUnit) getPickleStepResult(stepID string) (res *models.PickleStepResult) {
defer func() {
if r := recover(); r != nil {
res = nil
}
}()
psr := f.Storage.MustGetPickleStepResult(stepID)
res = &psr
return
}
func (f *JUnit) getPickleStepResultsByPickleID(pickleID string) (res []models.PickleStepResult) {
defer func() {
if r := recover(); r != nil {
res = nil
}
}()
res = f.Storage.MustGetPickleStepResultsByPickleID(pickleID)
return
}
func (f *JUnit) buildJUNITPackageSuite() JunitPackageSuite {
features := f.Storage.MustGetFeatures()
sort.Sort(sortFeaturesByName(features))
testRunStartedAt := f.Storage.MustGetTestRunStarted().StartedAt
suite := JunitPackageSuite{
Name: f.suiteName,
TestSuites: make([]*junitTestSuite, len(features)),
Time: junitTimeDuration(testRunStartedAt, utils.TimeNowFunc()),
}
for idx, feature := range features {
pickles := f.Storage.MustGetPickles(feature.Uri)
sort.Sort(sortPicklesByID(pickles))
ts := junitTestSuite{
Name: feature.Feature.Name,
TestCases: make([]*junitTestCase, len(pickles)),
}
var testcaseNames = make(map[string]int)
for _, pickle := range pickles {
testcaseNames[pickle.Name] = testcaseNames[pickle.Name] + 1
}
firstPickleStartedAt := testRunStartedAt
lastPickleFinishedAt := testRunStartedAt
var outlineNo = make(map[string]int)
for idx, pickle := range pickles {
tc := junitTestCase{}
tc.Name = pickle.Name
if testcaseNames[tc.Name] > 1 {
outlineNo[tc.Name] = outlineNo[tc.Name] + 1
tc.Name += fmt.Sprintf(" #%d", outlineNo[tc.Name])
}
pickleResult := f.getPickleResult(pickle.Id)
if pickleResult == nil {
tc.Status = skipped.String()
} else {
if idx == 0 {
firstPickleStartedAt = pickleResult.StartedAt
}
lastPickleFinishedAt = pickleResult.StartedAt
}
if len(pickle.Steps) > 0 {
lastStep := pickle.Steps[len(pickle.Steps)-1]
if lastPickleStepResult := f.getPickleStepResult(lastStep.Id); lastPickleStepResult != nil {
lastPickleFinishedAt = lastPickleStepResult.FinishedAt
}
}
if pickleResult != nil {
tc.Time = junitTimeDuration(pickleResult.StartedAt, lastPickleFinishedAt)
}
ts.Tests++
suite.Tests++
pickleStepResults := f.getPickleStepResultsByPickleID(pickle.Id)
for _, stepResult := range pickleStepResults {
pickleStep := f.Storage.MustGetPickleStep(stepResult.PickleStepID)
switch stepResult.Status {
case passed:
tc.Status = passed.String()
case failed:
tc.Status = failed.String()
tc.Failure = &junitFailure{
Message: fmt.Sprintf("Step %s: %s", pickleStep.Text, stepResult.Err),
}
case ambiguous:
tc.Status = ambiguous.String()
tc.Error = append(tc.Error, &junitError{
Type: "ambiguous",
Message: fmt.Sprintf("Step %s", pickleStep.Text),
})
case skipped:
tc.Error = append(tc.Error, &junitError{
Type: "skipped",
Message: fmt.Sprintf("Step %s", pickleStep.Text),
})
case undefined:
tc.Status = undefined.String()
tc.Error = append(tc.Error, &junitError{
Type: "undefined",
Message: fmt.Sprintf("Step %s", pickleStep.Text),
})
case pending:
tc.Status = pending.String()
tc.Error = append(tc.Error, &junitError{
Type: "pending",
Message: fmt.Sprintf("Step %s: TODO: write pending definition", pickleStep.Text),
})
}
}
switch tc.Status {
case failed.String():
ts.Failures++
suite.Failures++
case undefined.String(), pending.String():
ts.Errors++
suite.Errors++
}
ts.TestCases[idx] = &tc
}
ts.Time = junitTimeDuration(firstPickleStartedAt, lastPickleFinishedAt)
suite.TestSuites[idx] = &ts
}
return suite
}
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
}
// JunitPackageSuite ...
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
}