Skip to content

Commit

Permalink
feat: add tests and time to testsuites node
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Oct 31, 2023
1 parent 5222709 commit 208647b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/cli/send/junitUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('transformToJunit', () => {
expect(result).toBe(
`
<?xml version="1.0" encoding="UTF-8"?>
<testsuites errors="0" disabled="0" failues="0">
<testsuites tests="1" errors="0" disabled="0" failues="0" time="1.001">
<testsuite name="test.http" tests="1" failures="0" skipped="0" package="." time="1.001" file="test.http">
<testcase name="test" classname="test.http" assertions="1" time="1.001"/>
</testsuite>
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('transformToJunit', () => {
expect(result).toBe(
`
<?xml version="1.0" encoding="UTF-8"?>
<testsuites errors="0" disabled="0" failues="0">
<testsuites tests="1" errors="0" disabled="0" failues="0" time="3.005">
<testsuite name="test.http" tests="2" failures="0" skipped="0" package="." time="2.003" file="test.http">
<testcase name="test" classname="test.http" assertions="1" time="1.001"/>
<testcase name="test2" classname="test.http" assertions="1" time="1.002"/>
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('transformToJunit', () => {
expect(result).toBe(
`
<?xml version="1.0" encoding="UTF-8"?>
<testsuites errors="0" disabled="1" failues="0">
<testsuites tests="1" errors="0" disabled="1" failues="0" time="0.000">
<testsuite name="test.http" tests="1" failures="0" skipped="1" package="." time="0.000" file="test.http">
<testcase name="test" classname="test.http" assertions="0" time="0.000">
<skipped/>
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('transformToJunit', () => {
expect(result).toBe(
`
<?xml version="1.0" encoding="UTF-8"?>
<testsuites errors="0" disabled="0" failues="1">
<testsuites tests="1" errors="0" disabled="0" failues="1" time="0.000">
<testsuite name="test.http" tests="1" failures="1" skipped="0" package="." time="0.000" file="test.http">
<testcase name="test" classname="test.http" assertions="2" time="0.000">
<failure message="Assertions fail" type="unknown">failed result</failure>
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('transformToJunit', () => {
expect(result).toBe(
`
<?xml version="1.0" encoding="UTF-8"?>
<testsuites errors="0" disabled="0" failues="0">
<testsuites tests="1" errors="0" disabled="0" failues="0" time="1.001">
<testsuite name="test.http" tests="1" failures="0" skipped="0" package="." time="1.001" file="test.http">
<testcase name="test" classname="test.http" assertions="1" time="1.001">
<properties>
Expand Down
19 changes: 10 additions & 9 deletions src/cli/send/junitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { EOL } from 'os';
import { DOMImplementation } from '@xmldom/xmldom';
import { formatXml } from 'xmldom-format';

function toFloatSeconds(durationMillis: number): string {
return (durationMillis / 1000).toFixed(3);
}

/**
* output in junit format
* @param output json object with all test results
Expand All @@ -24,9 +20,11 @@ export function transformToJunit(output: SendJsonOutput): string {
document.appendChild(xmlNode);
const root = document.createElement('testsuites');
document.appendChild(root);
root.setAttribute('tests', `${output.summary.totalRequests}`);
root.setAttribute('errors', '0');
root.setAttribute('disabled', `${output.summary.disabledRequests}`);
root.setAttribute('failues', `${output.summary.failedTests}`);
root.setAttribute('time', `${toFloatSeconds(output.requests.reduce(sumDuration, 0))}`);

for (const [filename, requests] of Object.entries(groupedRequests)) {
root.appendChild(transformToTestSuite(document, filename, requests));
Expand All @@ -38,10 +36,6 @@ export function transformToJunit(output: SendJsonOutput): string {
});
}

function sum(x: number, y: SendOutputRequest) {
return x + (y.duration || 0);
}

// eslint-disable-next-line no-undef
function transformToTestSuite(document: XMLDocument, file: string, requests: Array<SendOutputRequest>) {
const summary = createTestSummary(requests);
Expand All @@ -52,7 +46,7 @@ function transformToTestSuite(document: XMLDocument, file: string, requests: Arr
setAttribute(root, 'failures', summary.failedRequests);
setAttribute(root, 'skipped', summary.disabledRequests);
setAttribute(root, 'package', dirname(file).replace(process.cwd(), ''));
setAttribute(root, 'time', toFloatSeconds(requests.reduce(sum, 0) || 0));
setAttribute(root, 'time', `${toFloatSeconds(requests.reduce(sumDuration, 0))}`);
setAttribute(root, 'file', file);

for (const request of requests) {
Expand Down Expand Up @@ -133,6 +127,13 @@ function transformToProperties(document: XMLDocument, request: SendOutputRequest
}
return undefined;
}
function sumDuration(x: number, y: SendOutputRequest) {
return x + (y.duration || 0);
}

function toFloatSeconds(durationMillis: number): string {
return (durationMillis / 1000).toFixed(3);
}

// eslint-disable-next-line no-undef
function setAttribute(node: HTMLElement, key: string, value: string | number | undefined) {
Expand Down

0 comments on commit 208647b

Please sign in to comment.