Skip to content

Commit

Permalink
Populate Test Suite Attributes
Browse files Browse the repository at this point in the history
The specification for the testsuites tag in Junit includes
multiple attributes, as per http://llg.cubic.org/docs/junit/
This commit populates disabled, errors, failures, tests and time.
It does so by recursively collecting those metrics from nested
testsuite tags in jasmineDone and then passing them to
wrapOutputAndWriteFile.
  • Loading branch information
FuadBalashov committed Jun 1, 2017
1 parent 1abc7e6 commit ab44324
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
45 changes: 45 additions & 0 deletions spec/JUnitXmlReporterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ describe("JUnitXmlReporter", function(){
});
});

describe("testsuites tag generation", function() {
beforeEach(function() {
setupReporterWithOptions({consolidateAll:true, consolidate:true});
triggerRunnerEvents();
});

it("testsuites tags should default disabled, errors, failures to 0 when undefined", function() {
var testSuitesTags = writeCalls[0].xmldoc.getElementsByTagName('testsuites');
expect(testSuitesTags.length).toBe(1);
var testSuitesTag = testSuitesTags[0];
expect(testSuitesTag.getAttribute('disabled')).toBe('0');
expect(testSuitesTag.getAttribute('errors')).toBe('0');
expect(testSuitesTag.getAttribute('failures')).toBe('0');
expect(testSuitesTag.getAttribute('tests')).toBe('1');
});
});

describe("generated xml output", function(){
var subSuite, subSubSuite, siblingSuite;
function itShouldIncludeXmlPreambleInAllFiles() {
Expand Down Expand Up @@ -285,6 +302,34 @@ describe("JUnitXmlReporter", function(){
});
});

describe("testsuites tag generation", function() {
beforeEach(function() {
var skipped = fakeSpec(subSubSuite, "should be skipped two levels down");
skipped.result.status = "pending";
var disabled = fakeSpec(subSubSuite, "should be disabled two levels down");
disabled.result.status = "disabled";
setupReporterWithOptions({consolidateAll:true, consolidate:true});
triggerRunnerEvents();
});

it("testsuites tags should include disabled, errors, failures, and tests (count) when defined", function() {
var testSuitesTags = writeCalls[0].xmldoc.getElementsByTagName('testsuites');
expect(testSuitesTags.length).toBe(1);
var testSuitesTag = testSuitesTags[0];
expect(testSuitesTag.getAttribute('disabled')).toBe('1');
expect(testSuitesTag.getAttribute('errors')).toBe('0');
expect(testSuitesTag.getAttribute('failures')).toBe('1');
expect(testSuitesTag.getAttribute('tests')).toBe('8');
});

it("testsuites tags should include time", function() {
var testSuitesTags = writeCalls[0].xmldoc.getElementsByTagName('testsuites');
expect(testSuitesTags.length).toBe(1);
var testSuitesTag = testSuitesTags[0];
expect(testSuitesTag.getAttribute('time')).not.toBe('');
});
});

describe("suite result generation", function() {
var suites;
beforeEach(function() {
Expand Down
49 changes: 39 additions & 10 deletions src/junit_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,17 @@
self.suiteDone(fakeFocusedSuite);
}
var output = '';
var testSuitesResults = { disabled: 0, errors: 0, failures: 0, tests: 0, time: 0 };
for (var i = 0; i < suites.length; i++) {
output += self.getOrWriteNestedOutput(suites[i]);
var suiteResults = self.getNestedSuiteData(suites[i]);
for (var key in suiteResults) {
testSuitesResults[key] += suiteResults[key];
};
}
// if we have anything to write here, write out the consolidated file
if (output) {
wrapOutputAndWriteFile(self.filePrefix, output);
wrapOutputAndWriteFile(self.filePrefix, output, testSuitesResults);
}
//log("Specs skipped but not reported (entire suite skipped or targeted to specific specs)", totalSpecsDefined - totalSpecsExecuted + totalSpecsDisabled);

Expand All @@ -306,6 +311,27 @@
}
};

self.formatSuiteData = function(suite) {
return {
disabled: suite._disabled || 0,
errors: 0,
failures: suite._failures || 0,
tests: suite._specs.length || 0,
time: (suite._endTime.getTime() - suite._startTime.getTime()) || 0
};
};

self.getNestedSuiteData = function (suite) {
var suiteResults = self.formatSuiteData(suite);
for (var i = 0; i < suite._suites.length; i++) {
var childSuiteResults = self.getNestedSuiteData(suite._suites[i]);
for (var key in suiteResults) {
suiteResults[key] += childSuiteResults[key];
};
}
return suiteResults;
};

self.getOrWriteNestedOutput = function(suite) {
var output = suiteAsXml(suite);
for (var i = 0; i < suite._suites.length; i++) {
Expand All @@ -315,7 +341,7 @@
return output;
} else {
// if we aren't supposed to consolidate output, just write it now
wrapOutputAndWriteFile(generateFilename(suite), output);
wrapOutputAndWriteFile(generateFilename(suite), output, self.getNestedSuiteData(suite));
return '';
}
};
Expand Down Expand Up @@ -467,17 +493,20 @@
self.logEntries.splice(0, self.logEntries.length);
}
}

// To remove complexity and be more DRY about the silly preamble and <testsuites> element
var prefix = '<?xml version="1.0" encoding="UTF-8" ?>';
if (self.stylesheetPath) {
prefix += '\n<?xml-stylesheet type="text/xsl" href="' + self.stylesheetPath + '" ?>';
function getPrefix({disabled, errors, failures, tests, time} = {}) {
// To remove complexity and be more DRY about the silly preamble and <testsuites> element
var prefix = '<?xml version="1.0" encoding="UTF-8" ?>';
if (self.stylesheetPath) {
prefix += '\n<?xml-stylesheet type="text/xsl" href="' + self.stylesheetPath + '" ?>';
}
prefix += '\n<testsuites disabled="' + disabled + '" errors="' + errors + '" failures="' + failures +
'" tests="' + tests + '" time="' + time + '">';
return prefix;
}
prefix += '\n<testsuites>';
var suffix = '\n</testsuites>';
function wrapOutputAndWriteFile(filename, text) {
function wrapOutputAndWriteFile(filename, text, testSuitesResults) {
if (filename.substr(-4) !== '.xml') { filename += '.xml'; }
self.writeFile(filename, (prefix + text + suffix));
self.writeFile(filename, (getPrefix(testSuitesResults) + text + suffix));
}
};
})(this);

0 comments on commit ab44324

Please sign in to comment.