Skip to content

Add timestamp to JUnit reporter #2443

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber

## [Unreleased]

### Added
- Add the timestamp of the start time of the testsuite to the JUnit reporter ([#2443](https://github.com/cucumber/cucumber-js/pull/2443))

## [11.0.1] - 2024-09-14
### Fixed
- Add missing setParallelCanAssign export ([#2427](https://github.com/cucumber/cucumber-js/pull/2427))
Expand Down
20 changes: 20 additions & 0 deletions src/formatter/junit_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
Rule,
TestStepResult,
TestStepResultStatus,
TimeConversion,
} from '@cucumber/messages'

import { doesHaveValue } from '../value_checker'
import { valueOrDefault } from '../value_checker'
import { ITestCaseAttempt } from './helpers/event_data_collector'
Expand All @@ -26,6 +28,7 @@ interface IJUnitTestSuite {
skipped: number
time: number
tests: IJUnitTestCase[]
timestamp?: string
}

interface IJUnitTestCase {
Expand Down Expand Up @@ -70,6 +73,7 @@ interface IBuildJUnitTestStepOptions {
export default class JunitFormatter extends Formatter {
private readonly names: Record<string, string[]> = {}
private readonly suiteName: string
private testRunStarted?: messages.Timestamp
public static readonly documentation: string = 'Outputs JUnit report'

constructor(options: IFormatterOptions) {
Expand All @@ -79,6 +83,9 @@ export default class JunitFormatter extends Formatter {
'cucumber-js'
)
options.eventBroadcaster.on('envelope', (envelope: messages.Envelope) => {
if (doesHaveValue(envelope.testRunStarted)) {
this.testRunStarted = envelope.testRunStarted.timestamp
}
if (doesHaveValue(envelope.testRunFinished)) {
this.onTestRunFinished()
}
Expand All @@ -90,6 +97,15 @@ export default class JunitFormatter extends Formatter {
.getTestCaseAttempts()
.filter((attempt) => !attempt.willBeRetried)
}
private getTimestamp(): string | undefined {
if (!this.testRunStarted) {
return undefined
}
const ms = TimeConversion.timestampToMillisecondsSinceEpoch(
this.testRunStarted
)
return new Date(ms).toISOString().slice(0, -5)
}

private getTestSteps(
testCaseAttempt: ITestCaseAttempt,
Expand Down Expand Up @@ -253,6 +269,7 @@ export default class JunitFormatter extends Formatter {
failures,
skipped,
time: tests.reduce((total, test) => total + test.time, 0),
timestamp: this.getTimestamp(),
}

this.log(this.buildXmlReport(testSuite))
Expand All @@ -266,6 +283,9 @@ export default class JunitFormatter extends Formatter {
.att('name', testSuite.name)
.att('time', testSuite.time)
.att('tests', testSuite.tests.length)
if (testSuite.timestamp) {
xmlReport.att('timestamp', testSuite.timestamp)
}
testSuite.tests.forEach((test) => {
const xmlTestCase = xmlReport.ele('testcase', {
classname: test.classname,
Expand Down
55 changes: 40 additions & 15 deletions src/formatter/junit_formatter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0" tests="0"/>'
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0" tests="0" timestamp="1970-01-01T00:00:00"/>'
)
})
})
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <system-out><![CDATA[Given a passing step......................................................passed]]></system-out>\n' +
' </testcase>\n' +
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <failure type="Error" message="error"><![CDATA[error]]></failure>\n' +
' <system-out><![CDATA[Given a failing step......................................................failed]]></system-out>\n' +
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <failure type="Error" message="Error: include invalid character"><![CDATA[Error: include invalid character]]></failure>\n' +
' <system-out><![CDATA[Given a failing step with invalid character...............................failed]]></system-out>\n' +
Expand Down Expand Up @@ -218,7 +218,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <system-out><![CDATA[Given a flaky step........................................................passed]]></system-out>\n' +
' </testcase>\n' +
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <failure/>\n' +
' <system-out><![CDATA[Given a pending step.....................................................pending]]></system-out>\n' +
Expand Down Expand Up @@ -289,7 +289,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="1" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="0" skipped="1" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <skipped/>\n' +
' <system-out><![CDATA[Given a passing step......................................................passed\n' +
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0" tests="1">\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0">\n' +
' <failure/>\n' +
' <system-out><![CDATA[Given a passing step...................................................undefined]]></system-out>\n' +
Expand Down Expand Up @@ -366,7 +366,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.002" tests="1">\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.002" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.002">\n' +
' <system-out><![CDATA[Given a passing step......................................................passed\n' +
'When a passing step.......................................................passed]]></system-out>\n' +
Expand Down Expand Up @@ -408,7 +408,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.002" tests="2">\n' +
'<testsuite failures="1" skipped="0" name="cucumber-js" time="0.002" tests="2" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my templated scenario" time="0.001">\n' +
' <system-out><![CDATA[Given a passing step......................................................passed]]></system-out>\n' +
' </testcase>\n' +
Expand Down Expand Up @@ -460,7 +460,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.002" tests="2">\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.002" tests="2" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my rule: first example" time="0.001">\n' +
' <system-out><![CDATA[Given a passing step......................................................passed]]></system-out>\n' +
' </testcase>\n' +
Expand Down Expand Up @@ -511,7 +511,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.002" tests="2">\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.002" tests="2" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="(unnamed feature)" name="(unnamed rule): (unnamed scenario)" time="0.001">\n' +
' <system-out><![CDATA[Given a passing step......................................................passed]]></system-out>\n' +
' </testcase>\n' +
Expand Down Expand Up @@ -553,7 +553,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <system-out><![CDATA[Given I have <![CDATA[cukes]]]]><![CDATA[> in my belly................................passed]]></system-out>\n' +
' </testcase>\n' +
Expand Down Expand Up @@ -593,7 +593,7 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="my test suite" time="0.001" tests="1">\n' +
'<testsuite failures="0" skipped="0" name="my test suite" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <system-out><![CDATA[Given a passing step......................................................passed]]></system-out>\n' +
' </testcase>\n' +
Expand Down Expand Up @@ -628,12 +628,37 @@ describe('JunitFormatter', () => {
// Assert
expect(output).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1">\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0.001" tests="1" timestamp="1970-01-01T00:00:00">\n' +
' <testcase classname="my feature" name="my scenario" time="0.001">\n' +
' <system-out><![CDATA[Given a passing step......................................................passed]]></system-out>\n' +
' </testcase>\n' +
'</testsuite>'
)
})
})
describe('testsuite timestamp', () => {
it('has start time of the tests', async () => {
// Arrange

// Act
const first = await testFormatter({ type: 'junit' })

// Assert
expect(first).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0" tests="0" timestamp="1970-01-01T00:00:00"/>'
)

// Arrange
clock.tick(1000)
// Act
const second = await testFormatter({ type: 'junit' })

// Assert
expect(second).xml.to.deep.equal(
'<?xml version="1.0"?>\n' +
'<testsuite failures="0" skipped="0" name="cucumber-js" time="0" tests="0" timestamp="1970-01-01T00:00:01"/>'
)
})
})
})
Loading