Skip to content

Commit

Permalink
Support the new XML JUnit format (e.g. for pytest 5.1).
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Sep 6, 2019
1 parent 32fbe09 commit 2937a1f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
32 changes: 30 additions & 2 deletions src/client/testing/common/xUnitParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,39 @@ export class XUnitParser implements IXUnitParser {
// Un-comment this line to capture the results file for later use in tests:
//await fs.writeFile('/tmp/results.xml', data);

const parserResult = await parseXML(data) as { testsuite: TestSuiteResult };
updateTests(tests, parserResult.testsuite);
const parserResult = await parseXML(data);
let junitResults: TestSuiteResult;
const fullResults = parserResult as { testsuites: { testsuite: TestSuiteResult[] }};
if (fullResults.testsuites) {
const junitSuites = fullResults.testsuites.testsuite;
if (!Array.isArray(junitSuites)) {
throw Error('bad JUnit XML data');
}
if (junitSuites.length === 0) {
return;
}
if (junitSuites.length > 1) {
throw Error('got multiple XML results');
}
junitResults = junitSuites[0];
} else {
junitResults = (parserResult as { testsuite: TestSuiteResult }).testsuite;
}
updateTests(tests, junitResults);
}
}

// const parserResult = await parseXML(data);
// const junitResults = getJunitResults(parserResult);
// updateTests(tests, junitResults);
// }
//}
//
//function getTestResults(data: any): TestSuiteResult {
// const full = data as { testsuite: TestSuiteResult };
// return full.testsuite;
//}

// tslint:disable-next-line:no-any
async function parseXML(data: string): Promise<any> {
// tslint:disable-next-line:no-require-imports
Expand Down
42 changes: 41 additions & 1 deletion src/test/testing/common/xUnitParser.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ suite('Testing - parse JUnit XML file', () => {
node.line = line;
}

test('success with single passing test', async () => {
test('legacy - success with single passing test', async () => {
const tests = createResults(`
./
test_spam.py
Expand Down Expand Up @@ -78,6 +78,46 @@ suite('Testing - parse JUnit XML file', () => {
fs.verifyAll();
});

test('success with single passing test', async () => {
const tests = createResults(`
./
test_spam.py
<Tests>
test_spam
`);
const expected = createResults(`
./
test_spam.py
<Tests>
test_spam P 0.001
`);
fixResult(
expected.testFunctions[0].testFunction,
'test_spam.py',
3
);
const filename = 'x/y/z/results.xml';
fs.setup(f => f.readFile(filename))
.returns(() => Promise.resolve(`
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite errors="0" failures="0" hostname="vm-dev-linux-desktop" name="pytest" skipped="0" tests="1" time="0.011" timestamp="2019-09-05T17:17:35.868863">
<testcase classname="test_spam.Tests" file="test_spam.py" line="3" name="test_spam" time="0.001">
</testcase>
</testsuite>
</testsuites>
`));

await parser.updateResultsFromXmlLogFile(
tests,
filename,
PassCalculationFormulae.pytest
);

expect(tests).to.deep.equal(expected);
fs.verifyAll();
});

test('no discovered tests', async () => {
const tests: Tests = createEmptyResults();
const expected: Tests = createEmptyResults();
Expand Down

0 comments on commit 2937a1f

Please sign in to comment.