Assign value of classnames of test results to relative path of spec file
Hi,
We are creating Sonar reports based on the data generated by karma-junit-reporter. We recently upgraded to Sonar 4.5.1 and It is not able to generate report based on the TEST-results.xml file generated. It is expecting the classnames to be relative paths of the corresponding spec files. It will be of great help if you can implement this feature or provide guidance on how to achieve it.
It will be of great help if you can implement this feature or provide guidance on how to achieve it.
@vojtajina @btford @pkozlowski-opensource It is really sad that no one is looking into the issues logged in this project and the project is not actively developed since October 2014. At the least, we expect a response from the authors clarifying if the issue would be taken care.
@sravikiran can you provide example of how it's look now and how you expect it should look
Thanks
@maksimr Thanks for responding. Give me a day, I will get back to you with the right data.
+1 for this.
need a different reporter type that corresponds to js-test-driver. See below:http://docs.sonarqube.org/display/SONAR/Unit+Test+results+import
@maksimr Sorry for the delay in responding. I was off for a few days and was too busy at work after that so I almost missed responding to your question.
Following is an instance of testcase that gets generated by the Karma JUnit Reporter:
<testcase name="should coordinate the bootstrap functions" time="0" classname="PhantomJS 1.9.7 (Windows 7).Activator Factory run">
Following is what Sonar expects:
<testcase name="should coordinate the bootstrap functions" time="0" classname="src/js/tests/mytestfile.js">
@sravikiran thanks!
Hm... I don't think we have file path in reporter on success
@maksimr Yeah, we don't have. Karma Jasmine Adapter is not sending file path in the event object.
Hi,
@sravikiran @maksimr have you any news on possibility to be compatible with sonar ?
thanks
@mathilde-pellerin I don't have any update. I am still waiting to hear from @maksimr.
We have the same problem, Sonar does not accept classnames that look like this:
classname="PhantomJS 1.9.8 (Linux).LodashHelpers"
It logs this error
[Step 4/4] 21:26:55.162 WARN - Test result will not be saved for test class "9.8 (Linux).LodashHelpers", because SonarQube associated resource has not been found using file name: "9/8 (Linux)/LodashHelpers.js"
+1 :)
All of us are waiting for an answer and I think the authors are still busy with something.
@vojtajina @btford @pkozlowski-opensource @maksimr Please update here if any work is started on this issue. If you keep us updated, we will try to help you as well!
@sravikiran does this https://github.com/karma-runner/karma-junit-reporter/pull/36 actually solve the issues, or would it at least help towards a solution?
@Dignifiedquire Thanks for letting me know this. I will try it out.
@Dignifiedquire The fix #36 does not solves the problem because the expectation is classname should be path to file as: classname="src/js/tests/mytestfile.js", but its not, still it is as: classname="PhantomJS_1_9_8_(Linux).models.AnalyticsModule_test" that is not accepted by sonar. @sravikiran what do you think? Did you get any other fix?
@irfanraza I had written a grunt task that runs after karma-junit-reporter to replace class names with a static string. But it solved only half of the problem. Sonar build is passing after making this change, but Sonar fails to show report of individual files. I didn't try #36 and I won't be able to try it now.
you will need to go through each spec file and use reg ex to extract test case names against each spec file. then once coverage xml is created, read the xml and replace the classname by mapping spec file name against test case name ...
We did above to publish valid unit test report xml to SonarQube.
Any news on that? We have the same exact problem. #36 does not fix this issue here.
Any further updates we are using the new https://github.com/tornaia/karma-sonarqube-unit-reporter, and we require the file path to be the correct classname not some english text... SonarQube generic plugin rejects the tests without it
I have custom function transforming the report to what sonar is expecting...
Can you share the custom function or explain your methodology ?
Thanks
below is the gulp task i am using to fix junit results xml to support sonar xml format. Note: all our spec files are using same name as js file. Ex: js file - xyz.js spec file - xyz.spec.js
` gulp.task('replace-sonar-utest', ['clean-tmp', 'replace-sonar-coverage', 'test-for-sonar'], function (done) { var xmlEntities = new (require('html-entities').XmlEntities)(); var XmlDocument = require('xmldoc').XmlDocument; var JASMINE_TESTCASE_REGEX = /[^\w]x?it\s?('"['"]/g; var JASMINE_SUITE_REGEX = /[^\w]x?describe\s?('"['"]/g; var specs = [];
glob.sync('./app/' + pkg + '/*/.spec.js').forEach(function (file) { var tests = [], content = fs.readFileSync(file, 'utf8'); var matches = content.match(JASMINE_TESTCASE_REGEX);
if (matches !== null) {
// #3
for (var i = 0, len = matches.length; i < len; i++) {
tests.push(xmlEntities.encode(matches[i].replace(JASMINE_TESTCASE_REGEX, '$1')).replace(/\\/g, '')); // remove escaped characters
}
var suiteMatches = content.match(JASMINE_SUITE_REGEX);
var suites = [];
for (var j = 0, len1 = suiteMatches.length; j < len1; j++) {
suites.push(xmlEntities.encode(suiteMatches[j].replace(JASMINE_SUITE_REGEX, '$1')).replace(/\\/g, '')); // remove escaped characters
}
specs.push({
'name': path.basename(file, path.extname(file)),
'tests': tests,
'suites': suites
});
}
});
var content = new XmlDocument(fs.readFileSync('./log/test-results.xml')), testsuites; if (content.name === 'testsuite') { testsuites = [content]; } else { testsuites = content.childrenNamed('testsuite'); }
for (var i = 0; i < testsuites.length; i++) { testsuites[i].eachChild(function (testcase) { if (testcase.name === 'testcase') { var name = xmlEntities.encode(testcase.attr.name); var className = xmlEntities.encode(testcase.attr.classname); var matchingSpecs = (_.filter(specs, function (spec) { var s = _.find(spec.suites, function (suite) { return suite === className || xmlEntities.encode(suite) === className; });
if (s) {
return _.find(spec.tests, function (test) {
return test === name || xmlEntities.encode(test) === name;
});
}
}));
var matchingSpec = matchingSpecs[0];
/*if (matchingSpecs.length > 1) {
var m = _.find(specs, function (spec) {
return spec.name === matchingSpec;
});
m.tests = _.without(m.tests, name);
}*/
testcase.attr.name = name;
testcase.attr.classname = matchingSpec.name.replace(/\./g, '_');
}
});
}
// #5
var PREFIX = '
resultContent = resultContent.concat(testsuites); fs.writeFileSync('./.tmp/sonar/results/TESTS-xunit.xml', resultContent.concat(SUFFIX), 'utf8');
done(); }); `