Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formatters): junit formatter template methods for creating elements #2349

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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