Skip to content

Commit 91a6d92

Browse files
authored
Another take on bug #4953 (#6076)
* Added missing currentTest to hook and avoid test duplicates in the allure report due to hook starts * Flip order in allure report hook test to match description..
1 parent 8115cc9 commit 91a6d92

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

packages/wdio-allure-reporter/src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,18 @@ class AllureReporter extends WDIOReporter {
100100
}
101101

102102
onTestStart(test) {
103+
const testTitle = test.currentTest ? test.currentTest : test.title
104+
if (this.isAnyTestRunning() && this.allure.getCurrentTest().name == testTitle) {
105+
// Test already in progress, most likely started by a before each hook
106+
this.setCaseParameters(test.cid)
107+
return
108+
}
109+
103110
if (this.options.useCucumberStepReporter) {
104-
return this.allure.startStep(test.title)
111+
return this.allure.startStep(testTitle)
105112
}
106113

107-
this.allure.startCase(test.title)
114+
this.allure.startCase(testTitle)
108115
this.setCaseParameters(test.cid)
109116
}
110117

@@ -165,7 +172,7 @@ class AllureReporter extends WDIOReporter {
165172
}
166173

167174
if (!this.isAnyTestRunning()) { // is any CASE running
168-
this.allure.startCase(test.title)
175+
this.onTestStart(test)
169176
} else {
170177
this.allure.getCurrentTest().name = test.title
171178
}

packages/wdio-allure-reporter/tests/__fixtures__/testState.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export function hookStart() {
9999
return hookState()
100100
}
101101

102+
export function hookStartWithCurrentTest() {
103+
return Object.assign(hookState(), { currentTest: testState().title })
104+
}
105+
102106
export function hookPassed() {
103107
return Object.assign(hookState(), { state: 'passed', end: '2018-05-14T15:17:21.631Z', _duration: 2730 })
104108
}

packages/wdio-allure-reporter/tests/suite.test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { runnerEnd, runnerStart } from './__fixtures__/runner'
1212
import { suiteEnd, suiteStart } from './__fixtures__/suite'
1313
import {
1414
testFailed, testPassed, testPending, testStart, testFailedWithMultipleErrors,
15-
hookStart, hookFailed,
15+
hookStart, hookFailed, hookStartWithCurrentTest,
1616
testFailedWithAssertionErrorFromExpectWebdriverIO
1717
} from './__fixtures__/testState'
1818
import {
@@ -313,6 +313,54 @@ describe('Pending tests', () => {
313313
})
314314
})
315315

316+
describe('Hook start', () => {
317+
let outputDir
318+
let allureXml
319+
320+
beforeEach(() => {
321+
outputDir = directory()
322+
})
323+
324+
afterEach(() => {
325+
clean(outputDir)
326+
})
327+
328+
for (const hookFirst of [true, false]) {
329+
it(`should use currentTest if provided by hook and not report multiple tests when start hook comes ${hookFirst?'first':'second'}`, () => {
330+
331+
const reporter = new AllureReporter({ stdout: true, outputDir })
332+
333+
const runnerEvent = runnerStart()
334+
delete runnerEvent.capabilities.browserName
335+
delete runnerEvent.capabilities.version
336+
337+
reporter.onRunnerStart(runnerEvent)
338+
reporter.onSuiteStart(suiteStart())
339+
340+
if (hookFirst) {
341+
reporter.onHookStart(hookStartWithCurrentTest())
342+
reporter.onTestStart(testStart())
343+
} else {
344+
reporter.onTestStart(testStart())
345+
reporter.onHookStart(hookStartWithCurrentTest())
346+
}
347+
348+
reporter.onTestFail(testFailed())
349+
reporter.onSuiteEnd(suiteEnd())
350+
reporter.onRunnerEnd(runnerEnd())
351+
352+
const results = getResults(outputDir)
353+
354+
expect(results).toHaveLength(1)
355+
allureXml = results[0]
356+
357+
expect(allureXml('test-case').length).toEqual(1)
358+
expect(allureXml('test-case > name').text()).toEqual('should can do something')
359+
expect(allureXml('test-case').attr('status')).toEqual('failed')
360+
})
361+
}
362+
})
363+
316364
const assertionResults = {
317365
webdriver: {
318366
commandTitle: 'GET /session/:sessionId/element',

packages/wdio-reporter/src/stats/hook.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Hook {
1313
uid?: string
1414
errors?: Error[]
1515
error?: Error
16+
currentTest?: string
1617
}
1718

1819
export default class HookStats extends RunnableStats {
@@ -23,13 +24,15 @@ export default class HookStats extends RunnableStats {
2324
errors?: Error[]
2425
error?: Error
2526
state?: 'failed'
27+
currentTest?: string
2628

2729
constructor (runner: Hook) {
2830
super('hook')
2931
this.uid = RunnableStats.getIdentifier(runner)
3032
this.cid = runner.cid
3133
this.title = runner.title
3234
this.parent = runner.parent
35+
this.currentTest = runner.currentTest
3336
}
3437

3538
complete (errors?: Error[]) {

packages/wdio-reporter/tests/stats/hook.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ test('should get initialised', () => {
55
cid: '0-0',
66
title: 'foobar',
77
parent: 'barfoo',
8+
currentTest: 'sometest'
89
})
910
expect(hook.type).toBe('hook')
1011
expect(hook.cid).toBe('0-0')
1112
expect(hook.title).toBe('foobar')
1213
expect(hook.parent).toBe('barfoo')
14+
expect(hook.currentTest).toBe('sometest')
1315
})
1416

1517
test('should allow to be called complete', () => {

0 commit comments

Comments
 (0)