Skip to content

Commit

Permalink
Another take on bug #4953 (#6076)
Browse files Browse the repository at this point in the history
* 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..
  • Loading branch information
oversizedhat authored Nov 16, 2020
1 parent 8115cc9 commit 91a6d92
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/wdio-allure-reporter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,18 @@ class AllureReporter extends WDIOReporter {
}

onTestStart(test) {
const testTitle = test.currentTest ? test.currentTest : test.title
if (this.isAnyTestRunning() && this.allure.getCurrentTest().name == testTitle) {
// Test already in progress, most likely started by a before each hook
this.setCaseParameters(test.cid)
return
}

if (this.options.useCucumberStepReporter) {
return this.allure.startStep(test.title)
return this.allure.startStep(testTitle)
}

this.allure.startCase(test.title)
this.allure.startCase(testTitle)
this.setCaseParameters(test.cid)
}

Expand Down Expand Up @@ -165,7 +172,7 @@ class AllureReporter extends WDIOReporter {
}

if (!this.isAnyTestRunning()) { // is any CASE running
this.allure.startCase(test.title)
this.onTestStart(test)
} else {
this.allure.getCurrentTest().name = test.title
}
Expand Down
4 changes: 4 additions & 0 deletions packages/wdio-allure-reporter/tests/__fixtures__/testState.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export function hookStart() {
return hookState()
}

export function hookStartWithCurrentTest() {
return Object.assign(hookState(), { currentTest: testState().title })
}

export function hookPassed() {
return Object.assign(hookState(), { state: 'passed', end: '2018-05-14T15:17:21.631Z', _duration: 2730 })
}
Expand Down
50 changes: 49 additions & 1 deletion packages/wdio-allure-reporter/tests/suite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { runnerEnd, runnerStart } from './__fixtures__/runner'
import { suiteEnd, suiteStart } from './__fixtures__/suite'
import {
testFailed, testPassed, testPending, testStart, testFailedWithMultipleErrors,
hookStart, hookFailed,
hookStart, hookFailed, hookStartWithCurrentTest,
testFailedWithAssertionErrorFromExpectWebdriverIO
} from './__fixtures__/testState'
import {
Expand Down Expand Up @@ -313,6 +313,54 @@ describe('Pending tests', () => {
})
})

describe('Hook start', () => {
let outputDir
let allureXml

beforeEach(() => {
outputDir = directory()
})

afterEach(() => {
clean(outputDir)
})

for (const hookFirst of [true, false]) {
it(`should use currentTest if provided by hook and not report multiple tests when start hook comes ${hookFirst?'first':'second'}`, () => {

const reporter = new AllureReporter({ stdout: true, outputDir })

const runnerEvent = runnerStart()
delete runnerEvent.capabilities.browserName
delete runnerEvent.capabilities.version

reporter.onRunnerStart(runnerEvent)
reporter.onSuiteStart(suiteStart())

if (hookFirst) {
reporter.onHookStart(hookStartWithCurrentTest())
reporter.onTestStart(testStart())
} else {
reporter.onTestStart(testStart())
reporter.onHookStart(hookStartWithCurrentTest())
}

reporter.onTestFail(testFailed())
reporter.onSuiteEnd(suiteEnd())
reporter.onRunnerEnd(runnerEnd())

const results = getResults(outputDir)

expect(results).toHaveLength(1)
allureXml = results[0]

expect(allureXml('test-case').length).toEqual(1)
expect(allureXml('test-case > name').text()).toEqual('should can do something')
expect(allureXml('test-case').attr('status')).toEqual('failed')
})
}
})

const assertionResults = {
webdriver: {
commandTitle: 'GET /session/:sessionId/element',
Expand Down
3 changes: 3 additions & 0 deletions packages/wdio-reporter/src/stats/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Hook {
uid?: string
errors?: Error[]
error?: Error
currentTest?: string
}

export default class HookStats extends RunnableStats {
Expand All @@ -23,13 +24,15 @@ export default class HookStats extends RunnableStats {
errors?: Error[]
error?: Error
state?: 'failed'
currentTest?: string

constructor (runner: Hook) {
super('hook')
this.uid = RunnableStats.getIdentifier(runner)
this.cid = runner.cid
this.title = runner.title
this.parent = runner.parent
this.currentTest = runner.currentTest
}

complete (errors?: Error[]) {
Expand Down
2 changes: 2 additions & 0 deletions packages/wdio-reporter/tests/stats/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ test('should get initialised', () => {
cid: '0-0',
title: 'foobar',
parent: 'barfoo',
currentTest: 'sometest'
})
expect(hook.type).toBe('hook')
expect(hook.cid).toBe('0-0')
expect(hook.title).toBe('foobar')
expect(hook.parent).toBe('barfoo')
expect(hook.currentTest).toBe('sometest')
})

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

0 comments on commit 91a6d92

Please sign in to comment.