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 retries property in reports #206

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
64 changes: 39 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ function getJenkinsClassname (test, options) {
return titles.join(options.suiteTitleSeparatedBy);
}

function formatErrorMessage(err, removeInvalidCharacters) {
var message;
if (err.message && typeof err.message.toString === 'function') {
message = err.message + '';
} else if (typeof err.inspect === 'function') {
message = err.inspect() + '';
} else {
message = '';
}
var failureMessage = err.stack || message;
if (!Base.hideDiff && err.expected !== undefined) {
var oldUseColors = Base.useColors;
Base.useColors = false;
failureMessage += "\n" + Base.generateDiff(err.actual, err.expected);
Base.useColors = oldUseColors;
}
return {
_attr: {
message: removeInvalidCharacters(message) || '',
type: err.name || ''
},
_cdata: removeInvalidCharacters(failureMessage)
};
}

/**
* JUnit reporter for mocha.js.
* @module mocha-junit-reporter
Expand Down Expand Up @@ -330,7 +355,9 @@ MochaJUnitReporter.prototype.getTestcaseData = function(test, err) {
_attr: {
name: flipClassAndName ? classname : name,
time: (typeof test.duration === 'undefined') ? 0 : test.duration / 1000,
classname: flipClassAndName ? name : classname
classname: flipClassAndName ? name : classname,
retries: test._currentRetry,
conclusion: err ? 'failure' : 'success',
}
}]
};
Expand All @@ -356,31 +383,14 @@ MochaJUnitReporter.prototype.getTestcaseData = function(test, err) {
testcase.testcase.push({'system-err': this.removeInvalidCharacters(stripAnsi(test.consoleErrors.join('\n')))});
}

if (test.prevAttempts) {
const self = this;
test.prevAttempts.forEach(function (attempt) {
testcase.testcase.push({flakyFailure: formatErrorMessage(attempt.err, self.removeInvalidCharacters)});
});
}
if (err) {
var message;
if (err.message && typeof err.message.toString === 'function') {
message = err.message + '';
} else if (typeof err.inspect === 'function') {
message = err.inspect() + '';
} else {
message = '';
}
var failureMessage = err.stack || message;
if (!Base.hideDiff && err.expected !== undefined) {
var oldUseColors = Base.useColors;
Base.useColors = false;
failureMessage += "\n" + Base.generateDiff(err.actual, err.expected);
Base.useColors = oldUseColors;
}
var failureElement = {
_attr: {
message: this.removeInvalidCharacters(message) || '',
type: err.name || ''
},
_cdata: this.removeInvalidCharacters(failureMessage)
};

testcase.testcase.push({failure: failureElement});
testcase.testcase.push({failure: formatErrorMessage(err, this.removeInvalidCharacters)});
}
return testcase;
};
Expand Down Expand Up @@ -468,6 +478,7 @@ MochaJUnitReporter.prototype.getXml = function(testsuites) {
_suiteAttr.timestamp = new Date(_suiteAttr.timestamp).toISOString().slice(0, -5);
_suiteAttr.failures = 0;
_suiteAttr.skipped = 0;
_suiteAttr.retries = 0;

_cases.forEach(function(testcase) {
var lastNode = testcase.testcase[testcase.testcase.length - 1];
Expand All @@ -477,6 +488,9 @@ MochaJUnitReporter.prototype.getXml = function(testsuites) {
if (typeof testcase.testcase[0]._attr.time === 'number') {
testcase.testcase[0]._attr.time = testcase.testcase[0]._attr.time.toFixed(3);
}
if (typeof testcase.testcase[0]._attr.retries === 'number') {
_suiteAttr.retries += testcase.testcase[0]._attr.retries;
}
});

if (antMode) {
Expand Down
32 changes: 23 additions & 9 deletions test/mock-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = function(stats, options) {
timestamp: "1970-01-01T00:00:00", // ISO timestamp truncated to the second
tests: "0",
failures: "0",
time: "0.000"
time: "0.000",
retries: "0"
}
}
]
Expand All @@ -34,15 +35,18 @@ module.exports = function(stats, options) {
timestamp: "1970-01-01T00:00:00",
tests: "3",
failures: "2",
time: "100.001"
time: "100.001",
retries: "0"
}
},
{
testcase: {
_attr: {
name: "Foo Bar can weez the juice",
classname: "can weez the juice",
time: "0.101"
time: "0.101",
retries: "0",
conclusion: "success"
}
}
},
Expand All @@ -52,7 +56,9 @@ module.exports = function(stats, options) {
_attr: {
name: "Foo Bar can narfle the garthog",
classname: "can narfle the garthog",
time: "2.002"
time: "2.002",
retries: "0",
conclusion: "failure"
}
},
{
Expand All @@ -72,7 +78,9 @@ module.exports = function(stats, options) {
_attr: {
name: "Foo Bar can behave like a flandip",
classname: "can behave like a flandip",
time: "30.003"
time: "30.003",
retries: "0",
conclusion: "failure"
}
},
{
Expand All @@ -96,15 +104,18 @@ module.exports = function(stats, options) {
timestamp: "1970-01-01T00:01:40", // new Date(100001).toISOString().slice(0, -5)
tests: "1",
failures: "0",
time: "400.005"
time: "400.005",
retries: "0"
}
},
{
testcase: {
_attr: {
name: "Another suite! works",
classname: "works",
time: "400.004"
time: "400.004",
retries: "0",
conclusion: "success"
}
}
}
Expand Down Expand Up @@ -148,7 +159,8 @@ module.exports = function(stats, options) {
tests: "1",
failures: "0",
skipped: "1",
time: "0.000"
time: "0.000",
retries: "0"
}
},
{
Expand All @@ -157,7 +169,9 @@ module.exports = function(stats, options) {
_attr: {
name: "Pending suite! pending",
classname: "pending",
time: "0.000"
time: "0.000",
retries: "0",
conclusion: "success"
}
},
{
Expand Down
15 changes: 15 additions & 0 deletions test/resources/JUnit.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ Permission to waive conditions of this license may be requested from Windy Road
<xs:documentation xml:lang="en">Time taken (in seconds) to execute the test</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="retries" type="xs:int" use="required">
<xs:annotation>
<xs:documentation xml:lang="en">Number of times the testcase was re-run.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="conclusion" type="xs:string" use="required">
<xs:annotation>
<xs:documentation xml:lang="en">Conclusion of the testcase: success or failure</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="system-out">
Expand Down Expand Up @@ -192,6 +202,11 @@ Permission to waive conditions of this license may be requested from Windy Road
<xs:documentation xml:lang="en">The total number of tests in the suite that errorrd. An errored test is one that had an unanticipated problem. e.g., an unchecked throwable; or a problem with the implementation of the test.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="retries" type="xs:int" use="required">
<xs:annotation>
<xs:documentation xml:lang="en">The total number of tests in the suite that were rerun.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="time" type="xs:decimal" use="required">
<xs:annotation>
<xs:documentation xml:lang="en">Time taken (in seconds) to execute the tests in the suite</xs:documentation>
Expand Down
3 changes: 3 additions & 0 deletions test/resources/jenkins-junit.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="classname" type="xs:string" use="optional"/>
<xs:attribute name="status" type="xs:string" use="optional"/>
<xs:attribute name="retries" type="xs:string" use="optional"/>
<xs:attribute name="conclusion" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

Expand All @@ -90,6 +92,7 @@
<xs:attribute name="tests" type="xs:string" use="required"/>
<xs:attribute name="failures" type="xs:string" use="optional"/>
<xs:attribute name="errors" type="xs:string" use="optional"/>
<xs:attribute name="retries" type="xs:string" use="optional"/>
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="disabled" type="xs:string" use="optional"/>
<xs:attribute name="skipped" type="xs:string" use="optional"/>
Expand Down