Skip to content

Commit

Permalink
added support to include TestSuite in filename, with config setting i…
Browse files Browse the repository at this point in the history
…ncludeTestSuiteInFilename
  • Loading branch information
sn1ckers committed Nov 1, 2016
1 parent 5a68273 commit 9759ab9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module.exports = function(config) {
useBrowserName: true, // add browser name to report and classes names
nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
properties: {} // key value pair of properties to add to the <properties> section of the report
properties: {}, // key value pair of properties to add to the <properties> section of the report
includeTestSuiteInFilename: true // if true appends the name of the testsuite to the file, $outputFile_Test-Suite.
}
});
};
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for
var useBrowserName = reporterConfig.useBrowserName
var nameFormatter = reporterConfig.nameFormatter || defaultNameFormatter
var classNameFormatter = reporterConfig.classNameFormatter
var includeTestSuiteInFilename = reporterConfig.includeTestSuitInFilename || false
var properties = reporterConfig.properties

var suites
Expand Down Expand Up @@ -83,6 +84,10 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for
return // don't die if browser didn't start
}

if (includeTestSuiteInFilename) {
newOutputFile = appendTestSuiteName(newOutputFile, xmlToOutput.suiteName)
}

pendingFileWritings++
helper.mkdirIfNotExists(path.dirname(newOutputFile), function () {
fs.writeFile(newOutputFile, xmlToOutput.end({pretty: true}), function (err) {
Expand All @@ -99,6 +104,19 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for
})
}

var appendTestSuiteName = function (filePath, testSuiteName) {
var parsedPath = path.parse(filePath)
var filename

if (testSuiteName) {
testSuiteName = '_' + testSuiteName
}

filename = parsedPath['name'] + testSuiteName + parsedPath['ext']

return path.join(parsedPath['root'], parsedPath['dir'], filename)
}

var getClassName = function (browser, result) {
var browserName = browser.name.replace(/ /g, '_').replace(/\./g, '_') + '.'

Expand Down Expand Up @@ -161,6 +179,8 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for
spec.ele('failure', {type: ''}, formatError(err))
})
}

testsuite.suiteName = result.suite[0] ? result.suite[0] : ''
}

// wait for writing all the xml files, before exiting
Expand Down
37 changes: 36 additions & 1 deletion test/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var fakeHelper = {
var fakeConfig = {
basePath: __dirname,
junitReporter: {
outputFile: ''
outputFile: '',
includeTestSuitInFilename: true
}
}

Expand Down Expand Up @@ -104,4 +105,38 @@ describe('JUnit reporter', function () {
// never pass a null value to XMLAttribute via xmlbuilder attr()
expect(reporter.onBrowserComplete.bind(reporter, badBrowserResult)).not.to.throw(Error)
})

it('should include testsuite name in filename', function () {
var fakeBrowser = {
id: 'Android_4_1_2',
name: 'Android',
fullName: 'Android 4.1.2',
lastResult: {
error: false,
total: 1,
failed: 0,
netTime: 10 * 1000
}
}

var fakeResult = {
suite: [
'Test-Suite',
'using it',
'get request'
],
description: 'should not fail',
log: []
}

reporter.onRunStart([ fakeBrowser ])
reporter.specSuccess(fakeBrowser, fakeResult)
reporter.onBrowserComplete(fakeBrowser)
reporter.onRunComplete()

expect(fakeFs.writeFile).to.have.been.called

var filePath = fakeFs.writeFile.firstCall.args[0]
expect(filePath).to.have.string('_Test-Suite')
})
})

0 comments on commit 9759ab9

Please sign in to comment.