Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Change how classname is generated to be usable in sonar
Browse files Browse the repository at this point in the history
Modified how classname is generated to be usable in sonar

Updated Readme.md

Modified README.md

Fix: Change how classname is generated to be usable in sonar

Modified how classname is generated to be usable in sonar

Updated Readme.md

Modified README.md

Removed extra semicolons and spaces
mon.asuncion committed Aug 3, 2015
1 parent ec01cff commit f481782
Showing 2 changed files with 86 additions and 46 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -46,6 +46,72 @@ You can pass list of reporters as a CLI argument too:
karma start --reporters junit,dots
```

## Produce test result with schema acceptable in sonar

To make this possible, it's required to make the classnames of each tests to match its file name.

For Example:
```js
describe('analytics.AnalyticsModule_test', function(){

var analytics;
beforeEach(module('ECApp'));
beforeEach(module('angularytics'));
beforeEach(module('AnalyticsModule'));
...
```
should have a file name AnalyticsModule_test.js
This will produce test result with schema acceptable in sonar.
Grunt file reporters property example:
```js
reporters: ['junit', 'coverage', 'progress'],
junitReporter: {
outputDir: $junitResults,
suite: 'models'
},
coverageReporter: {
type: 'lcov',
dir: $coverageOutputDir,
subdir: '.'
},
preprocessors: {
'src/main/webapp/public/js/ec3.3/**/*.js': 'coverage',
'src/main/webapp/public/js/ec3/**/*.js': 'coverage'
},
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher',
'ec-karma-junit-reporter23',
'karma-coverage'
]
```
Sonar property example:
```js
sonar.projectName=js
sonar.sources=site-main-php/src/main/webapp/public/js
sonar.projectBaseDir=.
sonar.exclusions=site-main-php/src/main/webapp/public/js/lib/*.js,site-main-php/src/main/webapp/public/js/tests/**/*.php,site-main-php/src/main/webapp/public/js/tests/**/*.js,site-main-php/src/main/webapp/public/js/ec3.3/vendor/**
sonar.javascript.lcov.reportPath=site-main-php/target/coverage/lcov.info
sonar.javascript.jstestdriver.reportsPath=site-main-php/target/surefire-reports/
sonar.tests=site-main-php/src/main/webapp/public/js/tests
```
Example junit xml report:
```xml
<?xml version="1.0"?>
<testsuite name="PhantomJS 1.9.8 (Linux)" package="models" timestamp="2015-03-10T13:59:23" id="0" hostname="admin" tests="629" errors="0" failures="0" time="11.452">
<properties>
<property name="browser.fullName" value="Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.8 Safari/534.34"/>
</properties>
<testcase name="(C.2) Checks if an empty object is returned when error 404 is encountered" time="0.01" classname="PhantomJS_1_9_8_(Linux).models.AnalyticsModule_test"/>
<testcase name="(C.3) Checks if an empty array is returned when error 405 is encountered" time="0.013" classname="PhantomJS_1_9_8_(Linux).models.AnalyticsModule_test"/>
</testsuite>
...
```
----
For more information on Karma see the [homepage].
66 changes: 20 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
@@ -3,11 +3,11 @@ var path = require('path')
var fs = require('fs')
var builder = require('xmlbuilder')


var JUnitReporter = function (baseReporterDecorator, config, logger, helper, formatError) {
var log = logger.create('reporter.junit')
var reporterConfig = config.junitReporter || {}
var pkgName = reporterConfig.suite || ''
var outputFileName = reporterConfig.outputFile

if (!reporterConfig.outputDir) {
throw new Error('You must set an output directory for JUnitReporter via the outputDir config property')
@@ -29,25 +29,17 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for
var timestamp = (new Date()).toISOString().substr(0, 19)
var suite = suites[browser.id] = builder.create('testsuite')
suite.att('name', browser.name)
.att('package', pkgName)
.att('timestamp', timestamp)
.att('id', 0)
.att('hostname', os.hostname())
.att('package', pkgName)
.att('timestamp', timestamp)
.att('id', 0)
.att('hostname', os.hostname());
suite.ele('properties')
.ele('property', {name: 'browser.fullName', value: browser.fullName})
.ele('property', {name: 'browser.fullName', value: browser.fullName})
}

var writeXmlForBrowser = function (browser) {
var outputBrowserPath = path.join(outputDir, browser.name.replace(/ /g, '_'))
var outputFile = outputDir + 'TESTS-' + browser.name.replace(/ /g, '_') + '.xml'
var xmlToOutput = suites[browser.id]
var outputFile

if (outputFileName) {
outputDir = outputBrowserPath
outputFile = path.join(outputBrowserPath, outputFileName)
} else {
outputFile = outputBrowserPath + '.xml'
}

pendingFileWritings++
helper.mkdirIfNotExists(outputDir, function () {
@@ -61,9 +53,9 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for
if (!--pendingFileWritings) {
fileWritingFinished()
}
})
})
}
});
});
};

this.onRunStart = function (browsers) {
suites = Object.create(null)
@@ -78,46 +70,28 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for

this.onBrowserComplete = function (browser) {
var suite = suites[browser.id]
var result = browser.lastResult

if (!suite) {
// This browser did not signal `onBrowserStart`. That happens
// if the browser timed out during the start phase or javascript
// exception has occured.
initliazeXmlForBrowser(browser)
if (browser.lastResult.error) {
suite = suites[browser.id]
suite.ele('error').dat(allMessages.join() + '\n')
allMessages = []
}
} else {
var result = browser.lastResult

suite.att('tests', result.total)
suite.att('errors', result.disconnected || result.error ? 1 : 0)
suite.att('failures', result.failed)
suite.att('time', (result.netTime || 0) / 1000)
suite.att('tests', result.total)
suite.att('errors', result.disconnected || result.error ? 1 : 0)
suite.att('failures', result.failed)
suite.att('time', (result.netTime || 0) / 1000)

if (result.disconnected) {
suite.ele('error').att('message', 'Browser disconnected')
}

suite.ele('system-out').dat(allMessages.join() + '\n')
allMessages = []
suite.ele('system-err')
}
suite.ele('system-out').dat(allMessages.join() + '\n')
suite.ele('system-err')

writeXmlForBrowser(browser)
}

this.onRunComplete = function () {
suites = null
suites = null;
allMessages.length = 0
}

this.specSuccess = this.specSkipped = this.specFailure = function (browser, result) {
var spec = suites[browser.id].ele('testcase', {
name: result.description, time: ((result.time || 0) / 1000),
classname: (pkgName ? pkgName + ' ' : '') + result.suite.join(' ').replace(/\./g, '_')
classname: browser.name.replace(/ /g, '_').replace(/\./g, '_') + '.' + (pkgName ? pkgName + '.' : '') + result.suite[0]
})

if (result.skipped) {
@@ -146,4 +120,4 @@ JUnitReporter.$inject = ['baseReporterDecorator', 'config', 'logger', 'helper',
// PUBLISH DI MODULE
module.exports = {
'reporter:junit': ['type', JUnitReporter]
}
}

0 comments on commit f481782

Please sign in to comment.