Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to name a test classname with a dot in it to match Sonar schema #69

Open
Odi55555 opened this issue Sep 30, 2015 · 5 comments
Open

Comments

@Odi55555
Copy link

I'm tyring to rename my unit tests to get an JUnit output that is readable from Sonar. But I'm following the naming conventions of John Papas Angular Styleguide that includes dots in the filename. For examle:
avengers.controller.js
If this file is in dir/subdir/avengers.controller.js my classname shoult be:

describe('dir.subdir.avengers.controller.js', function(){...

according to the README here.
But this would try to find the file in
dir/subdir/avengers/controller.js

How should I name my test classname to find the file?

@acwatson
Copy link

acwatson commented Oct 5, 2015

Ran into this problem myself today. I think that the issue is really with the JsTestDriverSensor.java implementation from the sonar-javascript-plugin v2.8. That class has a method to get the test file names (AKA the "spec" file names). The method implementation cannot work when you have dots in the file name because it replaces all dots with "/". It does this because it assumes you are using dots as directory delimiters and that your file naming strategy is compatible with Java, (which doesn't allow dots in class names). Here is the method implementation I'm speaking of:

protected String getUnitTestFileName(String className) {
// For JsTestDriver assume notation com.company.MyJsTest that maps to com/company/MyJsTest.js
String fileName = getUnitTestClassName(className);
fileName = fileName.replace('.', File.separatorChar);
fileName = fileName + ".js";
return fileName;
}

@ryepup
Copy link

ryepup commented Nov 5, 2015

Spent some time on this, found a way to make it work w/ john papa's guidelines.

In sonar-project.properties, pull in the tests using the jstest option, not the jstestdriver:

# tell sonar our tests and source are intermingled
sonar.sources=src
sonar.tests=src
# spec.js files aren't "source"
sonar.exclusions=**/*.spec.js
# tests are spec.js files
sonar.test.inclusions=**/*.spec.js
# use the jstest sensor, look in the "tests" folder for results
sonar.javascript.jstest.reportsPath=tests/

jstest doesn't do any transformations to the test name (see https://github.com/SonarCommunity/sonar-javascript/blob/master/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensor.java)

Then, in your tests, the first describe block has to match the path and filename exactly; so dir/subdir/avengers.controller.spec.js needs to start with describe('dir/subdir/avengers.controller.spec.js', ...)

Then, in your karma.conf.js, configure the junit reporter:

junitReporter: {
  useBrowserName: false,
  outputDir: 'tests',
  outputFile: 'TESTS-results.xml' // sonarqube is looking for TESTS-*.xml
}

When you run tests, karma-junit-reporter will write a tests/TESTS-results.xml file with XML that looks like:

<testcase name="does things" time="0.014" classname="dir/subdir/avengers.controller.spec.js"/>

With those in place, sonar-runner can read the xml, use the classname to find the path to the test, combines that with the sonar.tests setting to produce the full path to test file, and then be happy.

@acwatson
Copy link

acwatson commented Nov 5, 2015

thanks @ryepup. I ended up writing my own sonar plugin to handle the test report xml. My plugin does not require that the describe block have any particular name. I found it too restrictive and brittle to limit what developers could name the describe blocks. For anyone interested, see the free plugin I wrote here: https://github.com/acwatson/sonar-karma-test-report-plugin

@americoneto1
Copy link

Thank you @ryepup! I was trying to do this for a long time!

@jehon
Copy link

jehon commented Jan 16, 2017

I see that sonar will use a new schema (see bug #78). I think that this problem should dissapear as soon as this new schema is implemented.
Is my analysis correct and of any help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants