Skip to content

Commit

Permalink
read test failures for jUnit and TestNG
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep committed Oct 30, 2021
1 parent 1688dd4 commit fd66f39
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 95 deletions.
2 changes: 1 addition & 1 deletion 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.2",
"version": "0.0.3",
"description": "Parse test results from JUnit, TestNG, xUnit and many more",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/models/TestCase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare class TestCase {
duration: number;
status: string;
failure: string;
stack_trace: string;
steps: TestStep[];
}

Expand Down
1 change: 1 addition & 0 deletions src/models/TestCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TestCase {
this.duration = 0;
this.status = 'NA';
this.failure = '';
this.stack_trace = '';
this.steps = [];
}

Expand Down
1 change: 1 addition & 0 deletions src/models/TestStep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare class TestStep {
duration: number;
status: string;
failure: string;
stack_trace: string;
}

declare namespace TestStep { }
Expand Down
1 change: 1 addition & 0 deletions src/models/TestStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestStep {
this.duration = 0;
this.status = 'NA';
this.failure = '';
this.stack_trace = '';
}

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

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

function getTestCase(rawCase) {
const test_case = new TestCase();
test_case.name = rawCase["@_name"];
test_case.duration = rawCase["@_time"] * 1000;
if (rawCase.failure && rawCase.failure.length > 0) {
test_case.status = 'FAIL';
test_case.failure = rawCase.failure[0]["@_message"];
} else {
test_case.status = 'PASS';
}
return test_case;
}

function getTestSuite(rawSuite) {
const suite = new TestSuite();
Expand All @@ -11,6 +25,12 @@ function getTestSuite(rawSuite) {
suite.passed = suite.total - suite.failed;
suite.duration = rawSuite["@_time"] * 1000;
suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL';
const raw_test_cases = rawSuite.testcase;
if (raw_test_cases) {
for(let i = 0; i < raw_test_cases.length; i++) {
suite.cases.push(getTestCase(raw_test_cases[i]));
}
}
return suite;
}

Expand Down
20 changes: 20 additions & 0 deletions src/parsers/testng.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ const { getJsonFromXMLFile } = require('../helpers/helper');

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

function getTestCase(rawCase) {
const test_case = new TestCase();
test_case.name = rawCase["@_name"];
test_case.duration = rawCase["@_duration-ms"];
test_case.status = rawCase["@_status"];
if (rawCase.exception) {
test_case.failure = rawCase.exception[0].message;
}
return test_case;
}

function getTestSuiteFromTest(rawTest) {
const suite = new TestSuite();
Expand All @@ -15,6 +27,10 @@ function getTestSuiteFromTest(rawTest) {
suite.total = rawTestMethods.length;
suite.passed = rawTestMethods.filter(test => test['@_status'] === 'PASS').length;
suite.failed = rawTestMethods.filter(test => test['@_status'] === 'FAIL').length;
suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL';
for (let i = 0; i < rawTestMethods.length; i++) {
suite.cases.push(getTestCase(rawTestMethods[i]));
}
return suite;
}

Expand All @@ -34,6 +50,10 @@ function getTestSuite(rawSuite) {
suite.total = rawTestMethods.length;
suite.passed = rawTestMethods.filter(test => test['@_status'] === 'PASS').length;
suite.failed = rawTestMethods.filter(test => test['@_status'] === 'FAIL').length;
suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL';
for (let i = 0; i < rawTestMethods.length; i++) {
suite.cases.push(getTestCase(rawTestMethods[i]));
}
return suite;
}

Expand Down
3 changes: 0 additions & 3 deletions tests/data/junit/empty-suite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
</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>
3 changes: 0 additions & 3 deletions tests/data/junit/skipped-tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<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>
85 changes: 80 additions & 5 deletions tests/parser.junit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ describe('Parser - JUnit', () => {
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: []
cases: [
{
duration: 10000,
errors: 0,
failed: 0,
failure: "PROGRAM.cbl:2 Use a program name that matches the source file name",
id: "",
name: "Use a program name that matches the source file name",
passed: 0,
skipped: 0,
stack_trace: "",
status: "FAIL",
steps: [],
total: 0
}
]
}
]
});
Expand Down Expand Up @@ -55,7 +70,22 @@ describe('Parser - JUnit', () => {
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: []
cases: [
{
duration: 10000,
errors: 0,
failed: 0,
failure: "",
id: "",
name: "Use a program name that matches the source file name",
passed: 0,
skipped: 0,
stack_trace: "",
status: "PASS",
steps: [],
total: 0
}
]
}
]
});
Expand Down Expand Up @@ -84,7 +114,22 @@ describe('Parser - JUnit', () => {
skipped: 0,
duration: 10000,
status: 'PASS',
cases: []
cases: [
{
duration: 10000,
errors: 0,
failed: 0,
failure: "",
id: "",
name: "Use a program name that matches the source file name",
passed: 0,
skipped: 0,
stack_trace: "",
status: "PASS",
steps: [],
total: 0
}
]
}
]
});
Expand Down Expand Up @@ -113,7 +158,22 @@ describe('Parser - JUnit', () => {
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: []
cases: [
{
duration: 10000,
errors: 0,
failed: 0,
failure: "PROGRAM.cbl:2 Use a program name that matches the source file name",
id: "",
name: "Use a program name that matches the source file name",
passed: 0,
skipped: 0,
stack_trace: "",
status: "FAIL",
steps: [],
total: 0
}
]
},
{
id: '',
Expand All @@ -125,7 +185,22 @@ describe('Parser - JUnit', () => {
skipped: 0,
duration: 10000,
status: 'PASS',
cases: []
cases: [
{
duration: 10000,
errors: 0,
failed: 0,
failure: "PROGRAM.cbl:2 Use a program name that matches the source file name",
id: "",
name: "Use a program name that matches the source file name",
passed: 0,
skipped: 0,
stack_trace: "",
status: "FAIL",
steps: [],
total: 0
}
]
}
]
});
Expand Down
Loading

0 comments on commit fd66f39

Please sign in to comment.