NetCore 5 - no code coverage being reported

Template for a good new topic, formatted with Markdown:

  • ALM used - Azure DevOps
  • CI system used - Azure DevOps (Ubuntu agent)
  • Scanner - MsBuild
  • Languages of the repository - C# NetCore 5.0 w/ xunit
  • Error observed - no coverage being reported back to SC

I have been struggling to get SC to report on code coverage for a new project I am setting up. It was understanding that this should work out of the box, I did follow the tutorial when I first setup the project in SC - adding of keys etc. All other metrics are working as expected.

I have followed a number of blog posts in an attempt to get this working but have had no success and now feel like im going around in circles.

Below are a couple of the more recnt posts that I have follwed but failed.

Any pointers will be valuable please.

Thanks.

Jony.

Hi Jony, welcome to the community

Can you share how you have it configured (The relevant YAML if you are using YAML pipelines would be great)

Hopefully, we can help you from there

Tom

The key to making an AzDo pipeline be able to report coverage into sonar is going to be making sure that the test results and coverage files are being generated by the test task, and that those files are being found by the sonar Run Code Analysis task.

The sonar Run Code Analysis task does not have any configuration options that let you tell it where to look for the test results and coverage files, so it will have to be either the test task or a copy files task that places them where the sonar task can get them.

If you enable diagnostic logging and queue a build for a properly working pipeline, when its finished, in the Run Code Analysis task you should see a section that looks like near the beginning this where its finding the test result files.

Attempting to locate a test results (.trx) file...
Looking for TRX files in: D:\a\1\TestResults
No test results files found
Did not find any binary coverage files in the expected location.
Falling back on locating coverage files in the agent temp directory.
Searching for coverage files in D:\a\_temp
All matching files: count=2
	D:\a\_temp\TestResults\e81e495c-ba6b-47d1-a5c4-f2f2e001be22\VssAdministrator_WIN-98BB1APN4O6_2021-07-22.01_10_38.coverage
	D:\a\_temp\TestResults\VssAdministrator_WIN-98BB1APN4O6_2021-07-22_01_10_41\In\WIN-98BB1APN4O6\VssAdministrator_WIN-98BB1APN4O6_2021-07-22.01_10_38.coverage
Unique coverage files: count=1
	D:\a\_temp\TestResults\e81e495c-ba6b-47d1-a5c4-f2f2e001be22\VssAdministrator_WIN-98BB1APN4O6_2021-07-22.01_10_38.coverage

Usually when no coverage is reported in sonar its because this section in the log indicates that no files were found after looking through the usual search paths. If thats what you see, then open the log for the Test task and see where it put the test result files. If it made the test results and coverage files, compare those paths with where the Sonar task is looking for them. Chances are that they are being put into a location where Sonar is not inspecting for.

Its been my experience that the dotnet test task looks like it should let you specify the result file path in the command arguments but that value is overrridden when the pipline executes. So if you want to keep this task you will need to add a copy task to copy the test result files to one of the places where Sonar is looking for them. If you dont want to have another task added to the pipeline, you can replace the dotnet test task with a visual studio test task that has a working config parameter. For either, just make sure you use the pipeline variable when possible, like ($(Agent.TempDirectory)\TestResults)

Thanks for the help.

I have resolved the issue (for now) by adding --collect "Code coverage" to the dotnet test step. I say ‘for now’ as this only works on Windows agents - this unblocks me while we evaluate SC.

Thanks again.

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