Reputation: 5500
I have an .Net Core application along with unit test cases. For that I have configured the Build pipeline in Azure DevOps. In that pipeline I have integrated SonarQube tasks (prepare analysis, run code analysis, and publish quality gate results).
I can see the report in SonarQube server after successful run. But in that report, I didn’t see the Code Coverage Results and Unit test results. Even though I used Cobertura for unit tests.
Upvotes: 2
Views: 6707
Reputation: 21
Two changes need to be done here
1.- task: DotNetCoreCLI@2
inputs:
command: 'test'
projects: 'tests/**/*.csproj'
arguments: '--collect "XPlat Code Coverage;Format=opencover"' # This is all you need to add!
Explanation: sonar is not support Cobertura format, you can convert to opencover format
I have the same issue i just fixed it now.
Reference Link https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
- task: SonarQubePrepare@1
inputs:
SonarQube: 'SonarQube'
scannerMode: 'MSBuild'
projectKey: 'my-project-key'
projectName: 'my-project'
extraProperties: |
sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
Explanation: you tell explicity the path of report generated, otherwise azure agent is not understand where to find the report.
Upvotes: 1
Reputation: 397
Build pipeline generates code coverage files would it be coverlet or opencover, the problem is that Azure Devops pipeline creates the reports outside the working directory on the build agent, it uses the _temp folder inside _work whereas soanrquube searches inside the working directory. I am facing this issue with coverlet as well as Visual studio coverage tool. You can read the following 2 threads https://github.com/coverlet-coverage/coverlet/issues/1399
https://github.com/microsoft/azure-pipelines-tasks/issues/11536.
I posted an issue on snoarqube community site but did not get any response so far. The solution I think is either make the test step in the build pipeline change the directory or configure somewher sonarqube to search the agent build directory _temp which is above the working directory
Upvotes: 0
Reputation: 1470
Your pipeline seems to contain the right steps, so there can be two issues:
The easiest way to validate if the code coverage file is generated correctly, is by publishing it as an artifact. Now check what format the output file is. If there is no output file, please check if you did include /p:CollectCoverage=true --logger trx
to the test command. If you are running the build pipeline on Linux, you should also add /p:CoverletOutputFormat=opencover
and install the coverlet.collector NuGet package in the .NET Test Project.
If you configured step 1 correctly, it is still possible that the generated files are not sent to Sonarqube. The best way to see what is going wrong, is by checking the build logs of the Run Code Analysis and Publish Quality Gate Results steps.
The most common issue, is that Sonarscanner is checking the wrong directory. In the prepare step, please specify where the files are located, like:
sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
Upvotes: 3