Writing the suites out to separate xml files with the option to consolidate into one file per top level suite...

This commit is contained in:
Kelvin Luck 2013-01-08 13:09:05 +00:00
parent ef8f191c46
commit f19d07a209
3 changed files with 143 additions and 84 deletions

View File

@ -208,16 +208,41 @@ module.exports = function(grunt) {
phantomjs.on('jasmine.reportJUnitResults',function(junitData){ phantomjs.on('jasmine.reportJUnitResults',function(junitData){
if (options.junit && options.junit.path) { if (options.junit && options.junit.path) {
grunt.file.copy(runners.junit, path.join(options.junit.path,'out-TEST.xml'), {
process : function(src) { if (options.junit.consolidate) {
return grunt.util._.template(
src, grunt.util._(junitData.consolidatedSuites).each(
function(suites)
{ {
testsuites: junitData grunt.file.copy(runners.junit, path.join(options.junit.path, suites[0].name.replace(/[^\w]/g, '') + '-TEST.xml'), {
process: function(src) {
return grunt.util._.template(
src,
{
testsuites: suites
}
);
}
})
} }
); );
} } else {
}) junitData.suites.forEach(
function(suiteData)
{
grunt.file.copy(runners.junit, path.join(options.junit.path, suiteData.name.replace(/[^\w]/g, '') + '-TEST.xml'), {
process: function(src) {
return grunt.util._.template(
src,
{
testsuites: [suiteData]
}
);
}
})
}
);
}
} }
}); });

View File

@ -13,6 +13,16 @@
return names.join(' '); return names.join(' ');
} }
function getTopLevelSuiteId(suite)
{
var id;
while (suite) {
id = suite.id;
suite = suite.parentSuite;
}
return id;
}
jasmine.JUnitDataReporter = function() jasmine.JUnitDataReporter = function()
{ {
@ -22,45 +32,48 @@
reportRunnerStarting: function(runner) { reportRunnerStarting: function(runner) {
}, },
reportRunnerResults: function(runner) { reportRunnerResults: function(runner) {
var suites = runner.suites().map( var suitesById = {},
function(suite) suites = runner.suites().map(
{ function(suite)
var failures = 0, {
data = { var failures = 0,
name: getNestedSuiteName(suite), data = {
time: suite.duration / 1000, topLevelSuiteId: getTopLevelSuiteId(suite),
timestamp: suite.timestamp, name: getNestedSuiteName(suite),
tests: suite.specs().length, time: suite.duration / 1000,
errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things timestamp: suite.timestamp,
testcases: suite.specs().map( tests: suite.specs().length,
function(spec) errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things
{ testcases: suite.specs().map(
var failureMessages = []; function(spec)
if (spec.results().failedCount) { {
failures ++; var failureMessages = [];
spec.results().items_.forEach( if (spec.results().failedCount) {
function(expectation) failures ++;
{ spec.results().items_.forEach(
if (!expectation.passed()) { function(expectation)
failureMessages.push(expectation.message); {
} if (!expectation.passed()) {
failureMessages.push(expectation.message);
}
}
);
} }
); return {
} assertions: spec.results().items_.length,
return { className: getNestedSuiteName(spec.suite),
assertions: spec.results().items_.length, name: spec.description,
className: getNestedSuiteName(spec.suite), time: spec.duration / 1000,
name: spec.description, failureMessages: failureMessages
time: spec.duration / 1000, };
failureMessages: failureMessages }
}; )
} };
) data.failures = failures;
}; suitesById[suite.id] = data;
data.failures = failures; return data;
return data; }
} );
);
console.log('Suites:', suites); console.log('Suites:', suites);
}, },
reportSuiteResults: function(suite) { reportSuiteResults: function(suite) {

View File

@ -204,46 +204,67 @@
return names.join(' '); return names.join(' ');
} }
function getTopLevelSuiteId(suite)
{
var id;
while (suite) {
id = suite.id;
suite = suite.parentSuite;
}
return id;
}
PhantomReporter.prototype.generateJUnitSummary_ = function(runner) { PhantomReporter.prototype.generateJUnitSummary_ = function(runner) {
return runner.suites().map( var consolidatedSuites = {},
function(suite) suites = runner.suites().map(
{ function(suite)
var failures = 0, {
data = { var failures = 0,
name: getNestedSuiteName(suite), data = {
time: suite.duration / 1000, name: getNestedSuiteName(suite),
timestamp: suite.timestamp, time: suite.duration / 1000,
tests: suite.specs().length, timestamp: suite.timestamp,
errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things tests: suite.specs().length,
testcases: suite.specs().map( errors: 0, // TODO: These exist in the JUnit XML but not sure how they map to jasmine things
function(spec) testcases: suite.specs().map(
{ function(spec)
var failureMessages = []; {
if (spec.results().failedCount) { var failureMessages = [];
failures ++; if (spec.results().failedCount) {
spec.results().items_.forEach( failures ++;
function(expectation) spec.results().items_.forEach(
{ function(expectation)
if (!expectation.passed()) { {
failureMessages.push(expectation.message); if (!expectation.passed()) {
} failureMessages.push(expectation.message);
}
}
);
} }
); return {
} assertions: spec.results().items_.length,
return { className: getNestedSuiteName(spec.suite),
assertions: spec.results().items_.length, name: spec.description,
className: getNestedSuiteName(spec.suite), time: spec.duration / 1000,
name: spec.description, failureMessages: failureMessages
time: spec.duration / 1000, };
failureMessages: failureMessages }
}; )
} };
) data.failures = failures;
}; if (suite.parentSuite) {
data.failures = failures; consolidatedSuites[getTopLevelSuiteId(suite)].push(data);
return data; } else {
} consolidatedSuites[suite.id] = [data];
); }
return data;
}
);
return {
suites: suites,
consolidatedSuites: consolidatedSuites
};
}; };
jasmine.getEnv().addReporter( new PhantomReporter() ); jasmine.getEnv().addReporter( new PhantomReporter() );