Skip to content

Commit

Permalink
feat(formatters): junit formatter extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlacotaco committed Nov 8, 2023
1 parent 6eede88 commit 44af7a5
Showing 1 changed file with 56 additions and 23 deletions.
79 changes: 56 additions & 23 deletions src/formatter/junit_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import {
import { getPickleStepMap, getStepKeyword } from './helpers/pickle_parser'
import Formatter, { IFormatterOptions } from './'

interface IJUnitTestSuite {
export interface IJUnitTestSuite {
name: string
failures: number
skipped: number
time: number
tests: IJUnitTestCase[]
}

interface IJUnitTestCase {
export interface IJUnitTestCase {
classname: string
name: string
time: number
Expand Down Expand Up @@ -258,32 +258,65 @@ export default class JunitFormatter extends Formatter {
this.log(this.buildXmlReport(testSuite))
}

buildTestSuite(testSuite: IJUnitTestSuite): Record<string, any> {
return {
testsuite: {
'@failures': testSuite.failures,
'@skipped': testSuite.skipped,
'@name': testSuite.name,
'@time': testSuite.time,
'@tests': testSuite.tests.length,
},
}
}

buildTestCase(test: IJUnitTestCase): Record<string, any> {
return {
testcase: {
'@classname': test.classname,
'@name': test.name,
'@time': test.time,
},
}
}

buildSkippedCase(_test: IJUnitTestCase): Record<string, any> {
return {
skipped: {},
}
}

buildFailedCase(test: IJUnitTestCase): Record<string, any> {
return {
failure: {
'@type': test.result.failure?.type,
'@message': test.result.failure?.message,
'#cdata': test.result.failure?.detail,
},
}
}

buildSystemOutput(test: IJUnitTestCase): Record<string, any> {
return {
'system-out': {
'#cdata': test.systemOutput,
},
}
}

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)
const xmlReport = xmlbuilder.create(this.buildTestSuite(testSuite), {
invalidCharReplacement: '',
})

testSuite.tests.forEach((test) => {
const xmlTestCase = xmlReport.ele('testcase', {
classname: test.classname,
name: test.name,
time: test.time,
})
const xmlTestCase = xmlReport.ele(this.buildTestCase(test))
if (test.result.status === TestStepResultStatus.SKIPPED) {
xmlTestCase.ele('skipped')
xmlTestCase.ele(this.buildSkippedCase(test))
} 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(this.buildFailedCase(test))
}
xmlTestCase.ele('system-out', {}).cdata(test.systemOutput)
xmlTestCase.ele(this.buildSystemOutput(test))
})

return xmlReport.end({ pretty: true })
Expand Down

0 comments on commit 44af7a5

Please sign in to comment.