Why test coverage reports needs to be generated between begin and end?

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    -Community Edition Version 8.5 (build 37579)
  • what are you trying to achieve
    -trying to import test execution report into sonarqube server
  • what have you tried so far to achieve this

SonarQube Test coverage documentation says

SonarQube doesn’t run your tests or generate reports. To include coverage results in your analysis, you need to set up a third-party coverage tool to generate reports and configure SonarQube to import those reports.

Then on the same page under Importing .NET reports it says

To import .NET reports, the report generation process must be executed after the begin step and before the end MSBuild command

So sonarQube requires only a report to include coverage results in your analysis. It doesn’t care how the report is generated as long as it is in exepected format. Then why is it necessary to generate report between begin and end step. Can’t we generate report BEFORE the begin step and then reference the report path in the begin step?

The documentation for the Scanner for .NET says the order of operations to analyse C#/VB.NET code is begin- build - end. Since you have to build the the code before you can test, it follows that the .NET tests have to be be run between begin and end too.

The begin step has to be run before the build step as it affects what happens during the build. It fetches configuration information from the Sonar server that it passes to the build. It also triggers additional processing during the build to collect information about the MSBuild projects being analysed. The collected information is used in the end step.

We are using Jenkins CI/CD. If unit tests fail I don’t want Jenkin’s job continue building. For code coverage SonarQube only interested in the report. So I thought I can execute unit tests outside of begin and then wired-up the generated report to begin step.

So here are the step I am currently following and it seems to be working (but I am not sure if I am missing anything) Each step below is executed as Windows Batch Command

Step 1
Execute unit tests. This will build the unit test project & referenced projects and generate the code coverage report

dotnet test "%WORKSPACE%\Tests\My.UnitTests.csproj" --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover

Step 2
Build the complete solution between begin and end step to analyze the code. ( This will also build unit test project as a part of complete build)

dotnet "C:\sonar-scanner-msbuild-\SonarScanner.MSBuild.dll" begin /k:"MyKey" /d:sonar.host.url="http://myhost" /d:sonar.login="xxxx" /d:sonar.cs.opencover.reportsPaths="%WORKSPACE%\Tests\**\TestResults\*\coverage.opencover.xml"

dotnet build "%WORKSPACE%\MyComplete.sln" -nr:false

dotnet "C:\sonar-scanner-msbuild-\SonarScanner.MSBuild.dll" end /d:sonar.login="xxx"

If you don’t mind building the product code twice, once to test it and once to analyse it, then your approach will work.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.