COBERTURA configuration within SONAR - sonarqube

I am using SONAR for Code Quality checks of my projects. In one project I would like to know the code coverage of a library which is included in the classpath (maven dependency).
Is it possible to configure SONAR (with embedded COBERTURA) to also instrument the specific library for code coverage analysis? As cobertura instruments the bytecode this should be possible but I do not know if it is supported by cobertura (even indepentend from SONAR).
Any hints are welcome.
Regards
Klaus

You would have to set up cobertura(maven target) yourself and import the results(See dynamic analysis)
sonar.dynamicAnalysis=reuseReports
sonar.cobertura.reportPath=PATH_TO_RESULT
But I will not help much:
you would need the src files of the jar to see the coverage, otherwise you would just get % numbers and I'm not even sure sonar will show the extra covered files
the coverage for your whole project will always include the % of the library, so it will go down
It is better to test each project with its own unit tests on its own.

Related

How can I to see the code coverage per each test?

Is it possible to achieve a code coverage per each single test method with JaCoCo and Maven and to see it SonarQube?
I wish to see additionally by which unit test(s) a line or condition has been covered. Instead it shows me only the coverage of all tests combined, in the JaCoCo report and in SonarQube.
The generated jacoco.xml file does not contain any information on tests but only the source classes and methods. I think many years ago at some other place I managed to achieve a per test coverage, but I do not remember the solution.
There is a old blog which says to add a argline to Surefire configuration in Maven POM file: -XX:-UseSplitVerifier. But that does not work with newer Java versions (11 and above) as it turned out.
Does someone know if that is possible with the latest versions of JaCoCo, Maven, JUnit 5, SonarQube and Java?

SonarQube coverage on multi-modules project

I have a multi-modules project in SonarQube. One of the modules did not have any unit-test so far. Now that I added a unit-test in this module, the overall coverage of the project decreased! Does it mean that when a module has no test, it is not included at all in the global coverage calculation?
You have correctly deduced the situation: If there's no coverage at all, it's excluded from the calculations by default. On a language-by-language basis (available in Java) "force coverage to zero" functionality is available.

NCover Exclude Attribute in Sonar

I am have successfully setup Sonar to do some analysis on my C# project where we are already making use of NCover. Our code has NCover exclude attributes and we would like to use these in Sonar to make the analysis meaningful. I have managed to find a few things on the web (e.g. http://marc.info/?l=sonar-user&m=133896735312253) but the links to the JIRA is dead and doensnt ever seem to have been addressed. Does anyone know if it is possible to make use of NCover exclude attributes within Sonar? If not then we will have to look at alternatives.
The SonarQube C# plugin (since version 3.x) only imports code coverage reports generated by 3rd party tools. The information on which lines are to be covered by tests (i.e. "executable" lines), as well as which lines actually were covered by tests, are provided by the report generated by the tool.
These NCover exclusions should be applied by NCover itself, and should not require special support from SonarQube.

Integration Test Code Coverage using jacoco and maven

I have a maven test project which tests our dev code. These 2 are different maven projects.
Test project access dev project via maven dependency.
I want to measure integration test code coverage against dev project using jacoco and maven plugin.
Previously I have done it using jacoco and sonar where test code generates jacoco.exec file and then I manually built dev project passing jacoc.exec as arg in jenkins and get code coverage report.
I was not basically looking for 100% automated way of doing this.
Have read couple of articles which uses jacoco with maven but all those uses single project.
Appreciate any help/pointer.
Thanks
The 'dirty trick' here is to accumulate Jacoco coverage reports. As you have different projects there is no legal way to do this as by design maven projects are to be built without direct dependency.
But in real world nothing is ideal:
You point JaCoco report file with fixed path. By the way you can do it relatively to your Maven repository which is pretty clean approach.
You build your projects in 2 phases: first phase build all projects running all possible tests. It's better to have separate JaCoco report files per unit and integration tests. But they should be the same among projects and projects shall be configured to accumulate reports.
Doing second pass you run your mvn sonar:sonar magics. This will bring you 2 sonar projects (as I understand now you have the same picture) but coverage will be much more precise. The key is unified reports calculated on previous stage.
Here is complete enough illustration of this idea and lot of references to more simple projects. And here is another question which illustrates idea.
Hope this helps. I do the same approach for my multi-module projects to get coverage for 'lower layer' modules when doing tests for 'higher layer' ones.

Maven Multi-Module plus Extrenal Tests plus JaCoCo plus Sonar

On our company we have the several modules on project and each module has several unit tests, but we have system tests that based on classes and not modules. Our system tests use several classes of each module (not all). We cannot calculate intergration coverage and unit coverage of this tests. We want to merge results of system tests to calculate the coverage of all probuct.
Anyone have any idea how we can do this? Anyone can provide any tutorial with examples?
You can find a sample application that reproduces this case here: https://github.com/SonarSource/sonar-examples/tree/master/projects/code-coverage/combined%20ut-it/combined-ut-it-multimodule-maven-jacoco
This should help you.

Resources