Search code examples
c#.net.net-coresonarqubeopencover

Code coverage analysis across multiple project files with sonarqube


The common pattern in .NET is to have unit tests in a separate project which seems to work fine, however, we are trying to implement a pattern in which unit tests live within the project, alongside the implementation.

When trying to use coverlet to generate coverage files, the sonarscanner tool is accepting multiple files, but it's not reporting on the coverage:

dotnet-sonarscanner /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml"
...    

dotnet test --configuration Release \
                    --no-build \
                    --no-restore \
                    --logger "trx;LogFileName=testresults.trx" \
                    /p:CollectCoverage=true \
                    /p:CoverletOutputFormat=opencover
...

INFO: Parsing the OpenCover report /root/app/src/./Project.Data/coverage.opencover.xml
INFO: Adding this code coverage report to the cache for later reuse: /root/app/src/./Project.Data/coverage.opencover.xml
INFO: Parsing the OpenCover report /root/app/src/./Project.WebAPI/coverage.opencover.xml
INFO: Adding this code coverage report to the cache for later reuse: /root/app/src/./Project.WebAPI/coverage.opencover.xml
INFO: Parsing the OpenCover report /root/app/src/./Project.Domain/coverage.opencover.xml
INFO: Adding this code coverage report to the cache for later reuse: /root/app/src/./Project.Domain/coverage.opencover.xml
WARN: The Code Coverage report doesn't contain any coverage data for the included files. For troubleshooting hints, please refer to https://docs.sonarqube.org/x/CoBh

The provided link does not show any help in this scenario and adding DebugType Full (from a related answer) in the project files yields no different results.


Solution

  • Have a look at the .sonarqube\out\ProjectInfo.log. That will tell you how each project is being classified. If it's classified as a test project the coverage won't be uploaded.

    The Scanner for MSBuild uses a number of criteria to decide whether a project is a test project or not e.g. the project type guids and project capabilities listed in the file (see here). It's quite likely that if you are referencing a well-known test framework then the Scanner will decide the whole project is a test project and not analyse it.

    If that is the case then you might be able to work round it as follows:

    • explicitly mark the projects as not being test projects by setting the MSBuild property <SonarQubeTestProject>false</SonarQubeTestProject>. That will make the scanner analyse the project, including the test files in the project.
    • then use exclusions to ignore the test files so they are not analysed.