Hi all, we trying to set code coverage for Pull Request analysis and cannot find any info on:
When PR is raised, code coverage checks whole code or only what is in PR?
Just when I do run mvn clean test, it pulls whole thing to run all tests and we have a lot, which takes over 20 min and I believe it is overkill for PR scan.
Pull request analysis will be scoped to the code changes in the PR and the coverage on that new code. If you’re utilizing one of the CI/CD tools listed here, it’ll automatically detect pull request parameters (if you’re on SonarQube Developer Edition or higher).
Can you provide more information about your use case? What command are you using to execute the SonarQube scan? Is the scan running as part of a CI/CD tool pipeline? Are you seeing inaccurate results in SonarQube after your PR scan?
If I’m understanding correctly, your current problem is that your project has a lot of tests that take a long time to run. That by itself doesn’t sound like an issue with SonarQube’s functionality. You could try running sonar:sonar as a dedicated step to determine how long the actual SonarQube PR scan takes.
Thanks for this additional information. Apologies, I could’ve provided more info on code coverage specifically in my initial response.
Correct, SonarQube will only provide coverage metrics if you have a coverage report configured/generated. See this note from the documentation:
SonarQube itself does not calculate coverage. To include coverage results in your analysis, you must set up a third-party coverage tool and configure SonarQube to import the results produced by that tool.
SonarQube just requires that you provide a report in order for it to give you coverage metrics, and that’s regardless of whether is a full analysis or a PR analysis. SonarQube itself isn’t enforcing any requirements on whether you run your whole test suite or not. Theoretically, you could play around with your maven test command like I mentioned before and try generating a jacoco report based on the subset of tests you run. However, that might impact the accuracy of your code coverage metrics.
And then addressing this piece:
As part of your PR, did you make any changes to the source or test files in your project? If you didn’t, that would explain the no coverage or duplication results. Estimated % after merge is based on what it thinks your overall code coverage will be if the code changes in the PR are merged into your default branch. Again, this is based on the data provided by your code coverage report. Hope this helps!