Skip to content

Commit

Permalink
Improve JUnit report format support
Browse files Browse the repository at this point in the history
Close #1
  • Loading branch information
GaelGirodon committed Apr 26, 2024
1 parent c31764e commit 1a45c90
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ support this format too, natively or using an additional reporter, e.g.:
- **Jest**: `jest --reporters="jest-junit"`
- **PHPUnit**: `phpunit --log-junit report.xml`

The number of tests and failures will be extracted from the `<testsuites>` tag.
The number of tests and failures will be extracted from `<testsuite>` tags.

➡️ `{repo}-[{ref}-]junit-tests.json`

Expand Down
140 changes: 83 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@vercel/ncc": "^0.38.1",
"c8": "^9.1.0",
"eslint": "^8.57.0",
"mocha": "^10.3.0",
"mocha": "^10.4.0",
"mocha-junit-reporter": "^2.2.1"
}
}
19 changes: 10 additions & 9 deletions src/reports/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ export async function getReports(root) {
for (const r of reports) {
core.info(`Load JUnit report '${r}'`);
const report = await fs.readFile(r, { encoding: 'utf8' });
const testsMatches = report
.match(/(?<=<testsuites[^>]+tests=")[0-9]+(?=")/);
const failedMatches = report
.match(/(?<=<testsuites[^>]+failures=")[0-9]+(?=")/);
if (testsMatches?.length !== 1 || failedMatches?.length !== 1) {
const testSuites = report.match(/<testsuite[^s]([^>]+)>/g);
if (!testSuites) {
core.info('Report is not a valid JUnit report');
continue; // Invalid report file, trying the next one
}
const tests = parseInt(testsMatches[0]);
const failed = parseInt(failedMatches[0]);
const passed = tests - failed;
badges.push({ type: 'tests', data: { passed, failed, tests } })
const data = { passed: 0, failed: 0, tests: 0 };
for (const ts of testSuites) {
data.failed += parseInt(ts.match(/failures="([0-9]+)"/)?.[1] ?? "0");
data.failed += parseInt(ts.match(/errors="([0-9]+)"/)?.[1] ?? "0");
data.tests += parseInt(ts.match(/tests="([0-9]+)"/)?.[1] ?? "0");
}
data.passed = data.tests - data.failed;
badges.push({ type: 'tests', data })
break; // Successfully loaded a report file, can return now
}
core.info(`Loaded ${badges.length} JUnit report(s)`);
Expand Down
2 changes: 1 addition & 1 deletion src/reports/junit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('reports/junit', function () {
const reports = await getReports(join(process.cwd(), 'test/data/junit'));
assert.equal(reports.length, 1)
assert.deepStrictEqual(reports, [
{ type: 'tests', data: { passed: 8, failed: 2, tests: 10 } }
{ type: 'tests', data: { passed: 6, failed: 4, tests: 10 } }
]);
});
});
Expand Down
20 changes: 19 additions & 1 deletion test/data/junit/test-results.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Tests" time="1.2345" tests="10" failures="2">
<testsuites name="Suites" time="2.563" tests="10" failures="3" errors="1">
<testsuite name="Suite 0" tests="0" time="0.000" failures="0" errors="0">
</testsuite>
<testsuite name="Suite 1" tests="2" time="0.502" failures="0" errors="0">
<testcase name="Case 1.1" time="0.284"></testcase>
<testcase name="Case 1.2" time="0.217"></testcase>
</testsuite>
<testsuite name="Suite 2" tests="5" time="1.297" failures="2" errors="1">
<testcase name="Case 2.1" time="0.285"></testcase>
<testcase name="Case 2.2" time="0.271"></testcase>
<testcase name="Case 2.3" time="0.224"></testcase>
<testcase name="Case 2.4" time="0.297"></testcase>
<testcase name="Case 2.5" time="0.219"></testcase>
</testsuite>
<testsuite name="Suite 3" tests="3" time="0.763" failures="1" errors="0">
<testcase name="Case 3.1" time="0.264"></testcase>
<testcase name="Case 3.2" time="0.261"></testcase>
<testcase name="Case 3.3" time="0.237"></testcase>
</testsuite>
</testsuites>

0 comments on commit 1a45c90

Please sign in to comment.