Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cucumberjs json report parser #12

Merged
merged 3 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ dist
# TernJS port file
.tern-port

.DS_Store
.DS_Store
.vscode
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# test-results-parser

Parse test results from JUnit, TestNG, xUnit, Mocha and many more
Parse test results from JUnit, TestNG, xUnit, Mocha(json), CucumberJS(json) and many more

## Support

Expand All @@ -9,4 +9,5 @@ Parse test results from JUnit, TestNG, xUnit, Mocha and many more
| TestNG | ✅ |
| JUnit | ✅ |
| xUnit | ✅ |
| Mocha (json & mochawesome) | ✅ |
| Mocha (json & mochawesome) | ✅ |
| CucumberJS | ✅ |
103 changes: 103 additions & 0 deletions src/parsers/cucumberjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Parser for both Mocha Json report and Mochawesome json
*/
const path = require('path');

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"];
if (rawCase.state && rawCase.state === "failed") {
test_case.status = 'FAIL';
test_case.failure = rawCase.errorStack;
}
else {
test_case.status = 'PASS';
}
return test_case;
}

function getTestSuite(rawSuite) {
const suite = new TestSuite();
suite.name = rawSuite["name"];
suite.total = rawSuite["tests"];
suite.passed = rawSuite["passes"];
suite.failed = rawSuite["failures"];
suite.duration = rawSuite["duration"];
suite.status = suite.total === suite.passed ? 'PASS' : 'FAIL';
const raw_test_cases = rawSuite.elements;
if (raw_test_cases) {
for (let i = 0; i < raw_test_cases.length; i++) {
suite.cases.push(getTestCase(raw_test_cases[i]));
}
}
return suite;
}

function getTestResult(json) {
const result = new TestResult();
const { stats, suites } = preprocess(json);
result.name = suites["name"] || "";
result.total = stats["tests"];
result.passed = stats["passes"];
result.failed = stats["failures"];
const errors = stats["errors"];
if (errors) {
result.errors = errors;
}
result.duration = stats["duration"] || 0;

if (suites.length > 0) {
for (let i = 0; i < suites.length; i++) {
result.suites.push(getTestSuite(suites[i]));
}
}
result.status = result.total === result.passed ? 'PASS' : 'FAIL';
return result;
}

/**
* Function to format the raw json report
* @param {*} rawjson
* @returns formatted json object
*/
function preprocess(rawjson) {
const formattedResult = { stats: {}, suites: [] };

rawjson.forEach(testSuite => {
testSuite.elements.forEach(testCase => {
testCase.state = testCase.steps.every(step => step.result.status === "passed") ? "passed" : "failed";
testCase.duration = testCase.steps.map(step => step.result.duration).reduce((total, currVal) => total + currVal, 0) / 1000000;
testCase.duration = parseFloat(testCase.duration.toFixed(2));
testCase.errorStack = testCase.steps.filter(step => step.result.status === "failed").map(step => step.result.error_message)[0] || "";
})
testSuite.tests = testSuite.elements.length;

if (testSuite.tests) {
testSuite.failures = testSuite.elements.filter(testCase => testCase.state === "failed").length;
testSuite.passes = testSuite.elements.filter(testCase => testCase.state === "passed").length;
testSuite.duration = testSuite.elements.map(testCase => testCase.duration).reduce((total, currVal) => total + currVal, 0);
}
formattedResult.suites.push(testSuite);
});

formattedResult.stats.suites = formattedResult.suites.length;
for (const statsType of ["tests", "passes", "failures", "errors", "duration"]) {
formattedResult.stats[statsType] = formattedResult.suites.map(testSuite => testSuite[statsType]).reduce((total, currVal) => total + currVal, 0) || 0;
}
return formattedResult;
}

function parse(options) {
const cwd = process.cwd();
const json = require(path.join(cwd, options.files[0]));
return getTestResult(json);
}

module.exports = {
parse
}
3 changes: 3 additions & 0 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const testng = require('./testng');
const junit = require('./junit');
const xunit = require('./xunit');
const mocha = require('./mocha');
const cucumberjs = require('./cucumberjs');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename the file to just cucumber

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


function parse(options) {
switch (options.type) {
Expand All @@ -13,6 +14,8 @@ function parse(options) {
return xunit.parse(options);
case 'mocha':
return mocha.parse(options);
case 'cucumberjs':
return cucumberjs.parse(options);
default:
throw `UnSupported Result Type - ${options.type}`;
}
Expand Down
1 change: 1 addition & 0 deletions tests/data/cucumberjs/empty-suite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
175 changes: 175 additions & 0 deletions tests/data/cucumberjs/multiple-suites-multiple-tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
[
{
"description": " Verify calculator functionalities",
"elements": [
{
"description": "",
"id": "addition;addition-of-two-numbers",
"keyword": "Scenario",
"line": 4,
"name": "Addition of two numbers",
"steps": [
{
"arguments": [],
"keyword": "Given ",
"line": 5,
"name": "I have number 6 in calculator",
"match": {
"location": "features\\support\\steps.js:5"
},
"result": {
"status": "passed",
"duration": 1098599
}
},
{
"arguments": [],
"keyword": "When ",
"line": 6,
"name": "I add number 7",
"match": {
"location": "features\\support\\steps.js:9"
},
"result": {
"status": "passed",
"duration": 132100
}
},
{
"arguments": [],
"keyword": "Then ",
"line": 7,
"name": "I should see result 14",
"match": {
"location": "features\\support\\steps.js:17"
},
"result": {
"status": "failed",
"duration": 1330499,
"error_message": "AssertionError [ERR_ASSERTION]: 13 == 14\n + expected - actual\n\n -13\n +14\n\n at CustomWorld.<anonymous> (D:\\workspace\\nodejs\\cc-tests\\features\\support\\steps.js:18:12)"
}
}
],
"tags": [],
"type": "scenario"
},
{
"description": "",
"id": "addition;addition-of-two-numbers-v2",
"keyword": "Scenario",
"line": 9,
"name": "Addition of two numbers v2",
"steps": [
{
"arguments": [],
"keyword": "Given ",
"line": 10,
"name": "I have number 6 in calculator",
"match": {
"location": "features\\support\\steps.js:5"
},
"result": {
"status": "passed",
"duration": 110200
}
},
{
"arguments": [],
"keyword": "When ",
"line": 11,
"name": "I add number 7",
"match": {
"location": "features\\support\\steps.js:9"
},
"result": {
"status": "passed",
"duration": 97300
}
},
{
"arguments": [],
"keyword": "Then ",
"line": 12,
"name": "I should see result 13",
"match": {
"location": "features\\support\\steps.js:17"
},
"result": {
"status": "passed",
"duration": 76900
}
}
],
"tags": [],
"type": "scenario"
}
],
"id": "addition",
"line": 1,
"keyword": "Feature",
"name": "Addition",
"tags": [],
"uri": "features\\addition.feature"
},
{
"description": " Verify calculator functionalities",
"elements": [
{
"description": "",
"id": "subtraction;subtraction-of-two-numbers",
"keyword": "Scenario",
"line": 4,
"name": "Subtraction of two numbers",
"steps": [
{
"arguments": [],
"keyword": "Given ",
"line": 5,
"name": "I have number 10 in calculator",
"match": {
"location": "features\\support\\steps.js:5"
},
"result": {
"status": "passed",
"duration": 79800
}
},
{
"arguments": [],
"keyword": "When ",
"line": 6,
"name": "I subtract number 7",
"match": {
"location": "features\\support\\steps.js:13"
},
"result": {
"status": "passed",
"duration": 134299
}
},
{
"arguments": [],
"keyword": "Then ",
"line": 7,
"name": "I should see result 3",
"match": {
"location": "features\\support\\steps.js:17"
},
"result": {
"status": "passed",
"duration": 309700
}
}
],
"tags": [],
"type": "scenario"
}
],
"id": "subtraction",
"line": 1,
"keyword": "Feature",
"name": "Subtraction",
"tags": [],
"uri": "features\\subtract.feature"
}
]
68 changes: 68 additions & 0 deletions tests/data/cucumberjs/single-suite-single-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[
{
"description": "Verify calculator functionalities",
"elements": [
{
"description": "",
"id": "addition;addition-of-two-numbers",
"keyword": "Scenario",
"line": 5,
"name": "Addition of two numbers",
"steps": [
{
"arguments": [],
"keyword": "Given ",
"line": 6,
"name": "I have number 6 in calculator",
"match": {
"location": "features\\support\\steps.js:5"
},
"result": {
"status": "passed",
"duration": 1211400
}
},
{
"arguments": [],
"keyword": "When ",
"line": 7,
"name": "I entered number 7",
"match": {
"location": "features\\support\\steps.js:9"
},
"result": {
"status": "passed",
"duration": 136500
}
},
{
"arguments": [],
"keyword": "Then ",
"line": 8,
"name": "I should see result 13",
"match": {
"location": "features\\support\\steps.js:13"
},
"result": {
"status": "passed",
"duration": 244700
}
}
],
"tags": [
{
"name": "@green",
"line": 4
}
],
"type": "scenario"
}
],
"id": "addition",
"line": 1,
"keyword": "Feature",
"name": "Addition",
"tags": [],
"uri": "features\\sample.feature"
}
]
Loading