-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
junit_formatter.ts
291 lines (266 loc) · 8.21 KB
/
junit_formatter.ts
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import xmlbuilder from 'xmlbuilder'
import * as messages from '@cucumber/messages'
import {
Attachment,
Duration,
Feature,
getWorstTestStepResult,
Pickle,
Rule,
TestStepResult,
TestStepResultStatus,
} from '@cucumber/messages'
import { doesHaveValue } from '../value_checker'
import { valueOrDefault } from '../value_checker'
import { ITestCaseAttempt } from './helpers/event_data_collector'
import {
getGherkinExampleRuleMap,
getGherkinStepMap,
} from './helpers/gherkin_document_parser'
import { getPickleStepMap, getStepKeyword } from './helpers/pickle_parser'
import Formatter, { IFormatterOptions } from './'
interface IJUnitTestSuite {
name: string
failures: number
skipped: number
time: number
tests: IJUnitTestCase[]
}
interface IJUnitTestCase {
classname: string
name: string
time: number
result: IJUnitTestCaseResult
systemOutput: string
steps: IJUnitTestStep[]
}
interface IJUnitTestCaseFailure {
type: string
message?: string
detail: string
}
interface IJUnitTestCaseResult {
status: TestStepResultStatus
failure?: IJUnitTestCaseFailure
}
interface IJUnitTestStep {
attachments: Attachment[]
hidden: boolean
keyword: string
line: number
name?: string
result: TestStepResult
time: number
}
interface IBuildJUnitTestStepOptions {
isBeforeHook: boolean
gherkinStepMap: Record<string, messages.Step>
pickleStepMap: Record<string, messages.PickleStep>
testStep: messages.TestStep
testStepAttachments: messages.Attachment[]
testStepResult: messages.TestStepResult
}
export default class JunitFormatter extends Formatter {
private readonly names: Record<string, string[]> = {}
private readonly suiteName: string
public static readonly documentation: string = 'Outputs JUnit report'
constructor(options: IFormatterOptions) {
super(options)
this.suiteName = valueOrDefault(
options.parsedArgvOptions.junit?.suiteName,
'cucumber-js'
)
options.eventBroadcaster.on('envelope', (envelope: messages.Envelope) => {
if (doesHaveValue(envelope.testRunFinished)) {
this.onTestRunFinished()
}
})
}
private getTestCases() {
return this.eventDataCollector
.getTestCaseAttempts()
.filter((attempt) => !attempt.willBeRetried)
}
private getTestSteps(
testCaseAttempt: ITestCaseAttempt,
gherkinStepMap: Record<string, messages.Step>,
pickleStepMap: Record<string, messages.PickleStep>
) {
return testCaseAttempt.testCase.testSteps.map((testStep) => {
const isBeforeHook = !doesHaveValue(testStep.pickleStepId)
return this.getTestStep({
isBeforeHook,
gherkinStepMap,
pickleStepMap,
testStep,
testStepAttachments: testCaseAttempt.stepAttachments[testStep.id],
testStepResult: testCaseAttempt.stepResults[testStep.id],
})
})
}
private getTestStep({
isBeforeHook,
gherkinStepMap,
pickleStepMap,
testStep,
testStepAttachments,
testStepResult,
}: IBuildJUnitTestStepOptions): IJUnitTestStep {
const data: Partial<IJUnitTestStep> = {}
if (testStep.pickleStepId) {
const pickleStep = pickleStepMap[testStep.pickleStepId]
data.keyword = getStepKeyword({ pickleStep, gherkinStepMap })
data.line = gherkinStepMap[pickleStep.astNodeIds[0]].location.line
data.name = pickleStep.text
} else {
data.keyword = isBeforeHook ? 'Before' : 'After'
data.hidden = true
}
data.result = testStepResult
data.time = testStepResult.duration
? this.durationToSeconds(testStepResult.duration)
: 0
data.attachments = testStepAttachments
return data as IJUnitTestStep
}
private getTestCaseResult(steps: IJUnitTestStep[]): IJUnitTestCaseResult {
const { status, message, exception } = getWorstTestStepResult(
steps.map((step) => step.result)
)
return {
status,
failure:
message || exception
? {
type: exception?.type,
message: exception?.message,
detail: message,
}
: undefined,
}
}
private durationToSeconds(duration: Duration): number {
const NANOS_IN_SECOND = 1_000_000_000
return (
(duration.seconds * NANOS_IN_SECOND + duration.nanos) / NANOS_IN_SECOND
)
}
private nameOrDefault(name: string, fallbackSuffix: string): string {
if (!name) {
return `(unnamed ${fallbackSuffix})`
}
return name
}
private getTestCaseName(
feature: Feature,
rule: Rule | undefined,
pickle: Pickle
) {
const featureName = this.nameOrDefault(feature.name, 'feature')
const pickleName = this.nameOrDefault(pickle.name, 'scenario')
const testCaseName = rule
? this.nameOrDefault(rule.name, 'rule') + ': ' + pickleName
: pickleName
if (!this.names[featureName]) {
this.names[featureName] = []
}
let index = 0
while (
this.names[featureName].includes(
index > 0 ? `${testCaseName} [${index}]` : testCaseName
)
) {
index++
}
const name = index > 0 ? `${testCaseName} [${index}]` : testCaseName
this.names[featureName].push(name)
return name
}
private formatTestSteps(steps: IJUnitTestStep[]): string {
return steps
.filter((step) => !step.hidden)
.map((step) => {
const statusText = step.result.status.toLowerCase()
const maxLength = 80 - statusText.length - 3
const stepText = `${step.keyword}${step.name}`
.padEnd(maxLength, '.')
.substring(0, maxLength)
return `${stepText}...${statusText}`
})
.join('\n')
}
private onTestRunFinished(): void {
const testCases = this.getTestCases()
const tests = testCases.map<IJUnitTestCase>(
(testCaseAttempt: ITestCaseAttempt) => {
const { gherkinDocument, pickle } = testCaseAttempt
const { feature } = gherkinDocument
const gherkinExampleRuleMap = getGherkinExampleRuleMap(gherkinDocument)
const rule = gherkinExampleRuleMap[pickle.astNodeIds[0]]
const gherkinStepMap = getGherkinStepMap(gherkinDocument)
const pickleStepMap = getPickleStepMap(pickle)
const steps = this.getTestSteps(
testCaseAttempt,
gherkinStepMap,
pickleStepMap
)
const stepDuration = steps.reduce(
(total, step) => total + (step.time || 0),
0
)
return {
classname: this.nameOrDefault(feature.name, 'feature'),
name: this.getTestCaseName(feature, rule, pickle),
time: stepDuration,
result: this.getTestCaseResult(steps),
systemOutput: this.formatTestSteps(steps),
steps,
}
}
)
const passed = tests.filter(
(item) => item.result.status === TestStepResultStatus.PASSED
).length
const skipped = tests.filter(
(item) => item.result.status === TestStepResultStatus.SKIPPED
).length
const failures = tests.length - passed - skipped
const testSuite: IJUnitTestSuite = {
name: this.suiteName,
tests,
failures,
skipped,
time: tests.reduce((total, test) => total + test.time, 0),
}
this.log(this.buildXmlReport(testSuite))
}
private buildXmlReport(testSuite: IJUnitTestSuite): string {
const xmlReport = xmlbuilder
.create('testsuite', { invalidCharReplacement: '' })
.att('failures', testSuite.failures)
.att('skipped', testSuite.skipped)
.att('name', testSuite.name)
.att('time', testSuite.time)
.att('tests', testSuite.tests.length)
testSuite.tests.forEach((test) => {
const xmlTestCase = xmlReport.ele('testcase', {
classname: test.classname,
name: test.name,
time: test.time,
})
if (test.result.status === TestStepResultStatus.SKIPPED) {
xmlTestCase.ele('skipped')
} else if (test.result.status !== TestStepResultStatus.PASSED) {
const xmlFailure = xmlTestCase.ele('failure', {
type: test.result.failure?.type,
message: test.result.failure?.message,
})
if (test.result?.failure) {
xmlFailure.cdata(test.result.failure.detail)
}
}
xmlTestCase.ele('system-out', {}).cdata(test.systemOutput)
})
return xmlReport.end({ pretty: true })
}
}