diff --git a/README.md b/README.md index 50d5036..8f1e733 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,7 @@ less results.tap | tap-xunit --package="MyCompany.MyTool" > results.xml ``` -By default TAP comments are used as test-suite names and considered to mark test boundaries. CLI flag ```--dontUseCommentsAsTestNames``` can be used to turn that feature off, in which case comments are ignored and -all assertions go inside a single `````` with name ```Default``` +By default TAP comments are used as test-suite names and considered to mark test boundaries. Without the feature all assertions go inside a single `````` with name ```Default``` Alternatively with CLI flag ```--oneAssertionPerTestcase``` comments are concatenated and included as ``````. Or with flag ```--dontUseCommentsAsTestNames``` the test-suite names feature is turned off, and comments are ignored. Because not all publishers pick up on `````` alternatively ```--outputAsFailure``` moves the comments to the `````` element but only if no diagnostic data objects are given. ## Library ``` @@ -65,6 +64,15 @@ This feature exists because many xUnit reporters assume '.' in test-suite name i If specified, all test-suites will be prefixed with the given package name. NOTE: ```replaceWithUnicodeDot``` option does not apply to package and . can be used to specify package hierarchy. +#### oneAssertionPerTestcase +*default*: false + +Overrides dontUseCommentsAsTestNames, always group comments with assertion before and output in `````` + +#### outputAsFailure +*default*: false + +Change behaviour for oneAssertionPerTestcase to move diagnostic comments from system-output to failure. Included only if no other diagnostics are given, and only for failure results. # License MIT diff --git a/lib/converter.js b/lib/converter.js index a203dde..7e722ab 100644 --- a/lib/converter.js +++ b/lib/converter.js @@ -23,6 +23,12 @@ var defaults = { // Whether tap parser should be in strict mode or not, false by default. strict: false, + + // Instead of CommentsAsTestNames concatenate comments extra data, + // which is output as system-output in XML. + oneAssertionPerTestcase: false, + + outputToFailure: false, }; function converter(options) { @@ -39,15 +45,34 @@ function converter(options) { var planPresent = false; tapParser.on('comment', function(comment) { - // comment specifies boundaries between testsuites, unless feature disabled. - if (options.dontUseCommentsAsTestNames) { - return; - } - if (noMoreTests) { - return; + if (options.oneAssertionPerTestcase) { + if (options.outputToFailure) { + assert_idx = testCase.asserts.length-1; + if ("undefined" == typeof testCase.asserts[assert_idx].comment) { + testCase.asserts[assert_idx].comment = comment.substr(1).trim(); + } else { + testCase.asserts[assert_idx].comment += '\n'+comment.substr(1).trim(); + } + } else { + if (testCase.extra.length > 0) { + testCase.extra = [ + testCase.extra[0] + comment + ] + } else { + testCase.extra.push(comment); + } + } + } else { + // comment specifies boundaries between testsuites, unless feature disabled. + if (options.dontUseCommentsAsTestNames) { + return; + } + if (noMoreTests) { + return; + } + // create new test + testCase = newTest(comment); } - // create new test - testCase = newTest(comment); }); tapParser.on('plan', function() { @@ -55,11 +80,14 @@ function converter(options) { }); tapParser.on('assert', function(assert) { - // no test name was given, so all asserts go in a single test - if (!testCase) { + if (options.oneAssertionPerTestcase) { testCase = newTest('Default'); + } else { + // no test name was given, so all asserts go in a single test + if (!testCase) { + testCase = newTest('Default'); + } } - testCase.asserts.push(assert); }); @@ -105,7 +133,7 @@ function converter(options) { }); if (tapParser.sawValidTap && planPresent) { - var xmlString = serialize(testSuites); + var xmlString = serialize(testSuites, options); outStream.push(xmlString + '\n'); } else { // Fail, no valid tap found (normally means no plan line present) diff --git a/lib/serialize.js b/lib/serialize.js index c6ba01c..946b486 100644 --- a/lib/serialize.js +++ b/lib/serialize.js @@ -1,7 +1,7 @@ var xmlbuilder = require('xmlbuilder'); var sanitizeString = require('./sanitize-string'); -module.exports = function serialize (testCases) { +module.exports = function serialize (testCases, options) { var rootXml = xmlbuilder.create('testsuites'); testCases.forEach(function(suite) { var suiteElement = rootXml.ele('testsuite'); @@ -31,12 +31,18 @@ module.exports = function serialize (testCases) { failureElement = testCaseElement.ele('failure'); if(a.diag) { failureElement.txt(formatFailure(a.diag)); + } else if(a.comment) { + if(options.outputToFailure) { + failureElement.txt(sanitizeString(a.comment)); + } } } - if(i === suite.asserts.length -1) { - suite.extra.forEach(function (extraContent) { - testCaseElement.ele('system-out', sanitizeString(extraContent)); - }); + if (!options.outputToFailure) { + if (i === suite.asserts.length -1) { + suite.extra.forEach(function (extraContent) { + testCaseElement.ele('system-out', sanitizeString(extraContent)); + }); + } } }); }); diff --git a/package.json b/package.json index f6687a8..deccba9 100644 --- a/package.json +++ b/package.json @@ -35,11 +35,11 @@ "xml2js": "^0.4.17" }, "dependencies": { - "through2": "~2.0.0", - "xmlbuilder": "~4.2.0", "duplexer": "~0.1.1", + "minimist": "~1.2.0", "tap-parser": "~1.2.2", - "xtend": "~4.0.0", - "minimist": "~1.2.0" + "through2": "~2.0.0", + "xmlbuilder": "~4.2.0", + "xtend": "~4.0.0" } }