Skip to content

Commit

Permalink
added support for JUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep committed Sep 18, 2021
1 parent 79f8dd7 commit 1688dd4
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# test-results-parser

Parse test results from JUnit, TestNG, xUnit and many more

## Support

| Result Type | Support |
|-------------|---------|
| TestNG ||
| JUnit ||
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "test-results-parser",
"version": "0.0.1",
"version": "0.0.2",
"description": "Parse test results from JUnit, TestNG, xUnit and many more",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
7 changes: 5 additions & 2 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const testng = require('./testng');
const junit = require('./junit');

function parse(options) {
switch (options.type) {
case 'testng':
return testng.parse(options)
return testng.parse(options);
case 'junit':
return junit.parse(options);
default:
throw `UnKnown Result Type - ${options.type}`
throw `UnSupported Result Type - ${options.type}`;
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/parsers/junit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { getJsonFromXMLFile } = require('../helpers/helper');

const TestResult = require('../models/TestResult');
const TestSuite = require('../models/TestSuite');

function getTestSuite(rawSuite) {
const suite = new TestSuite();
suite.name = rawSuite["@_name"];
suite.total = rawSuite["@_tests"];
suite.failed = rawSuite["@_failures"];
suite.passed = suite.total - suite.failed;
suite.duration = rawSuite["@_time"] * 1000;
suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL';
return suite;
}

function getTestResult(json) {
const result = new TestResult();
const rawResult = json["testsuites"][0];
result.name = rawResult["@_name"];
result.total = rawResult["@_tests"];
result.failed = rawResult["@_failures"];
const errors = rawResult["@_errors"];
if (errors) {
result.errors = errors;
}
const skipped = rawResult["@_skipped"];
if (skipped) {
result.skipped = skipped;
}
result.total = result.total - result.skipped;
result.passed = result.total - result.failed - result.errors;
result.duration = rawResult["@_time"] * 1000;
const rawSuites = rawResult["testsuite"];
const filteredSuites = rawSuites.filter(suite => suite.testcase);
for (let i = 0; i < filteredSuites.length; i++) {
result.suites.push(getTestSuite(filteredSuites[i]));
}
result.status = result.total === result.passed ? 'PASS' : 'FAIL';
return result;
}

function parse(options) {
const json = getJsonFromXMLFile(options.files[0]);
return getTestResult(json);
}

module.exports = {
parse
}
4 changes: 1 addition & 3 deletions src/parsers/testng.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ function parse(options) {
result.name = _suite.name;
}
}
result.status = result.total === result.passed ? 'PASS' : 'FAIL';
} else if (suitesWithTests.length === 1) {
const suite = suitesWithTests[0];
result.name = suite['@_name'];
Expand All @@ -71,11 +70,10 @@ function parse(options) {
for (let i = 0; i < rawTests.length; i++) {
result.suites.push(getTestSuiteFromTest(rawTests[i]));
}
result.status = result.total === result.passed ? 'PASS' : 'FAIL';

} else {
console.log("No suites with tests found");
}
result.status = result.total === result.passed ? 'PASS' : 'FAIL';
return result;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/data/junit/empty-suite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites id="id" name="result name" tests="1" failures="1" errors="" time="10">
<testsuite id="codereview.cobol.analysisProvider" name="empty name" tests="0" failures="0" time="0">
</testsuite>
<testsuite id="codereview.cobol.analysisProvider" name="suite name" tests="1" failures="1" time="10">
<testcase id="codereview.cobol.rules.ProgramIdRule" name="Use a program name that matches the source file name" time="10">
<failure message="PROGRAM.cbl:2 Use a program name that matches the source file name" type="WARNING">
Some Text
</failure>
</testcase>
</testsuite>
</testsuites>
17 changes: 17 additions & 0 deletions tests/data/junit/multiple-suites.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites id="id" name="result name" tests="2" failures="1" errors="" time="20">
<testsuite id="codereview.cobol.analysisProvider" name="suite name 1" tests="1" failures="1" time="10">
<testcase id="codereview.cobol.rules.ProgramIdRule" name="Use a program name that matches the source file name" time="10">
<failure message="PROGRAM.cbl:2 Use a program name that matches the source file name" type="WARNING">
Some Text
</failure>
</testcase>
</testsuite>
<testsuite id="codereview.cobol.analysisProvider" name="suite name 2" tests="1" failures="0" time="10">
<testcase id="codereview.cobol.rules.ProgramIdRule" name="Use a program name that matches the source file name" time="10">
<failure message="PROGRAM.cbl:2 Use a program name that matches the source file name" type="WARNING">
Some Text
</failure>
</testcase>
</testsuite>
</testsuites>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites id="id" name="name" tests="225" failures="1262" errors="" time="0.001">
<testsuite id="codereview.cobol.analysisProvider" name="COBOL Code Review" tests="45" failures="17" time="0.001">
<testcase id="codereview.cobol.rules.ProgramIdRule" name="Use a program name that matches the source file name" time="0.001">
<testsuites id="id" name="result name" tests="1" failures="1" errors="" time="10">
<testsuite id="codereview.cobol.analysisProvider" name="suite name" tests="1" failures="1" time="10">
<testcase id="codereview.cobol.rules.ProgramIdRule" name="Use a program name that matches the source file name" time="10">
<failure message="PROGRAM.cbl:2 Use a program name that matches the source file name" type="WARNING">
Some Text
</failure>
Expand Down
10 changes: 10 additions & 0 deletions tests/data/junit/skipped-tests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites id="id" name="result name" tests="2" failures="0" errors="" skipped="1" time="10">
<testsuite id="codereview.cobol.analysisProvider" name="suite name" tests="1" failures="0" time="10">
<testcase id="codereview.cobol.rules.ProgramIdRule" name="Use a program name that matches the source file name" time="10">
<failure message="PROGRAM.cbl:2 Use a program name that matches the source file name" type="WARNING">
Some Text
</failure>
</testcase>
</testsuite>
</testsuites>
134 changes: 134 additions & 0 deletions tests/parser.junit.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const { parse } = require('../src');
const assert = require('assert');

describe('Parser - JUnit', () => {

it('single suite with single test', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/single-suite.xml'] });
assert.deepEqual(result, {
id: '',
name: 'result name',
total: 1,
passed: 0,
failed: 1,
errors: 0,
skipped: 0,
duration: 10000,
status: 'FAIL',
suites: [
{
id: '',
name: 'suite name',
total: 1,
passed: 0,
failed: 1,
errors: 0,
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: []
}
]
});
});

it('empty suite with single test', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/empty-suite.xml'] });
assert.deepEqual(result, {
id: '',
name: 'result name',
total: 1,
passed: 0,
failed: 1,
errors: 0,
skipped: 0,
duration: 10000,
status: 'FAIL',
suites: [
{
id: '',
name: 'suite name',
total: 1,
passed: 0,
failed: 1,
errors: 0,
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: []
}
]
});
});

it('suite with skipped tests', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/skipped-tests.xml'] });
assert.deepEqual(result, {
id: '',
name: 'result name',
total: 1,
passed: 1,
failed: 0,
errors: 0,
skipped: 1,
duration: 10000,
status: 'PASS',
suites: [
{
id: '',
name: 'suite name',
total: 1,
passed: 1,
failed: 0,
errors: 0,
skipped: 0,
duration: 10000,
status: 'PASS',
cases: []
}
]
});
});

it('multiple suites', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/multiple-suites.xml'] });
assert.deepEqual(result, {
id: '',
name: 'result name',
total: 2,
passed: 1,
failed: 1,
errors: 0,
skipped: 0,
duration: 20000,
status: 'FAIL',
suites: [
{
id: '',
name: 'suite name 1',
total: 1,
passed: 0,
failed: 1,
errors: 0,
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: []
},
{
id: '',
name: 'suite name 2',
total: 1,
passed: 1,
failed: 0,
errors: 0,
skipped: 0,
duration: 10000,
status: 'PASS',
cases: []
}
]
});
});

});
8 changes: 4 additions & 4 deletions tests/parser.testng.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Parser - TestNG', () => {
cases: []
}
]
})
});
});

it('single suite with multiple tests', () => {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Parser - TestNG', () => {
cases: []
}
]
})
});
});

it('multiple suites with single test', () => {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('Parser - TestNG', () => {
cases: []
}
]
})
});
});

it('multiple suites with multiple tests', () => {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('Parser - TestNG', () => {
cases: []
}
]
})
});
});

});

0 comments on commit 1688dd4

Please sign in to comment.