Issue with code coverage in SonarCloud for dotnet 8 using AzureDevOps Services YAML pipeline

Hi Community,

In our team we are facing an issue while trying to export test results and test coverage of our dotnet solution.
The dotnet solution is written using c# and dotnet 8.

Here by are the information about our configuration.
Thank you for your help :slight_smile:

  • ALM used : Azure DevOps service
  • CI system used : Azure DevOps service
  • Scanner command :
variables:
  buildConfiguration: 'Release'
  testResultFolder : '$(Agent.TempDirectory)/TestResults/'

- task: DotNetCoreCLI@2
   inputs:
      command: 'restore'
      feedsToUse: 'select'
   displayName: 'Restore package'
	  
- task: SonarCloudPrepare@2
   displayName: 'Prepare analysis configuration'
   inputs:
      SonarCloud: 'SonarCloud'
      organization: 'XXX'
      scannerMode: 'MSBuild'
      projectKey: 'YYY'
      projectName: 'Project'
      extraProperties: |
        sonar.verbose=true
        sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml
        sonar.cs.vstest.reportsPaths= $(testResultFolder)/**/*.trx
		  
- task: DotNetCoreCLI@2
  inputs:
	command: 'build'
	arguments: '--configuration $(buildConfiguration) --no-restore --no-incremental'
  displayName: 'Build de la solution en  $(buildConfiguration)'

# Run all tests as parallel to speed-up build time using a bash script. See attachments for full version.
# This script loops over each tests projects and execute the following command line for each (`$file` is the csproj file path):
# dotnet test "$file" --no-restore --no-build --configuration $(buildConfiguration) --filter Category!=Integration --settings ./CI/coverage.runsettings --results-directory $(testResultFolder) --logger trx --collect "Code Coverage"
- task: Bash@3
  displayName: 'Run all tests as parallel to speed-up build time'
  inputs:
	filePath: CI/RunAllTests.sh
	arguments: $(testResultFolder) $(buildConfiguration) 
	workingDirectory: $(Build.SourcesDirectory)
      continueOnError: true	
	  
- task: SonarCloudAnalyze@2
    displayName: 'Run SonarCloud analysis'

- task: SonarCloudPublish@2
    displayName: 'Publish results on build summary'
  • Languages of the repository : C#
  • Error observed:
    Depite sonar scanner analyse step is finding and converting .coverage files to .coveragexml:
    • Code coverage is 0% on SonarCloud
    • No tests are detected in SonarCloud

In Analysis task I can share those lines to help for diagnosis

...
12:20:34.494 INFO: ------------- Run sensors on project
12:20:34.524 **DEBUG: '[Deprecated] C#** Integration Tests Coverage Report Import' skipped because one of the required properties is missing
12:20:34.524 DEBUG: 'Java CPD Block Indexer' skipped because there is no related file in current project
12:20:34.525 DEBUG: Sensors : C# -> Analysis Warnings import -> C# File Caching Sensor -> C# Tests Coverage Report Import -> C# Unit Test Results Import -> Zero Coverage Sensor
12:20:34.525 INFO: Sensor C# [csharp]
...

2024-07-16T12:20:35.3681052Z 12:20:35.353 INFO: Sensor C# File Caching Sensor [csharp] (done) | time=89ms
2024-07-16T12:20:35.3681291Z 12:20:35.353 INFO: Sensor C# Tests Coverage Report Import [csharp]
2024-07-16T12:20:35.3681689Z 12:20:35.354 DEBUG: Analyzing coverage with wildcardPatternFileProvider with base dir '/home/vsts/work/1/.' and file separator '/'.
2024-07-16T12:20:35.3682144Z 12:20:35.356 DEBUG: Pattern matcher extracted prefix/absolute path '/home/vsts/work/1/.' from the given pattern '**/*.coveragexml'.
2024-07-16T12:20:35.3682518Z 12:20:35.364 DEBUG: Gathering files for wildcardPattern '**/*.coveragexml'.
2024-07-16T12:20:35.6502835Z 12:20:35.649 DEBUG: Pattern matcher returns '0' files.
2024-07-16T12:20:35.6505777Z 12:20:35.650 WARN: Could not find any coverage report file matching the pattern '**/*.coveragexml'. Troubleshooting guide: https://community.sonarsource.com/t/37151
2024-07-16T12:20:35.6510218Z 12:20:35.650 DEBUG: Analyzing coverage after aggregate found '0' coverage files.
2024-07-16T12:20:35.6522370Z 12:20:35.651 DEBUG: The total number of file count statistics is '0'.
2024-07-16T12:20:35.6523099Z 12:20:35.651 INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=298ms
2024-07-16T12:20:35.6525322Z 12:20:35.651 INFO: Sensor C# Unit Test Results Import [csharp]
2024-07-16T12:20:35.6535338Z 12:20:35.653 DEBUG: Pattern matcher extracted prefix/absolute path '/home/vsts/work/_temp/TestResults' from the given pattern '/home/vsts/work/_temp/TestResults//**/*.trx'.
2024-07-16T12:20:35.6538866Z 12:20:35.653 DEBUG: Gathering files for wildcardPattern '**/*.trx'.
2024-07-16T12:20:35.6550186Z 12:20:35.654 DEBUG: Pattern matcher returns '10' files.
2024-07-16T12:20:35.6554176Z 12:20:35.655 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.6559767Z 12:20:35.655 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_54[1].trx'.
2024-07-16T12:20:35.7007162Z 12:20:35.700 DEBUG: Parsed Visual Studio Test Times - duration: 22035.
2024-07-16T12:20:35.7131459Z 12:20:35.712 DEBUG: Parsed Visual Studio Test Counters - total: 43, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 43.
2024-07-16T12:20:35.7154646Z 12:20:35.715 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7166350Z 12:20:35.716 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_54.trx'.
2024-07-16T12:20:35.7178349Z 12:20:35.717 DEBUG: Parsed Visual Studio Test Times - duration: 21385.
2024-07-16T12:20:35.7204549Z 12:20:35.720 DEBUG: Parsed Visual Studio Test Counters - total: 3, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 3.
2024-07-16T12:20:35.7227063Z 12:20:35.722 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7232951Z 12:20:35.723 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_53[1].trx'.
2024-07-16T12:20:35.7243526Z 12:20:35.724 DEBUG: Parsed Visual Studio Test Times - duration: 20647.
2024-07-16T12:20:35.7254733Z 12:20:35.725 DEBUG: Parsed Visual Studio Test Counters - total: 10, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 10.
2024-07-16T12:20:35.7276962Z 12:20:35.727 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7292669Z 12:20:35.729 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_55[1].trx'.
2024-07-16T12:20:35.7302693Z 12:20:35.730 DEBUG: Parsed Visual Studio Test Times - duration: 19428.
2024-07-16T12:20:35.7309696Z 12:20:35.730 DEBUG: Parsed Visual Studio Test Counters - total: 0, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 0.
2024-07-16T12:20:35.7316596Z 12:20:35.731 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7323632Z 12:20:35.732 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_53.trx'.
2024-07-16T12:20:35.7333593Z 12:20:35.733 DEBUG: Parsed Visual Studio Test Times - duration: 20092.
2024-07-16T12:20:35.7356691Z 12:20:35.735 DEBUG: Parsed Visual Studio Test Counters - total: 9, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 9.
2024-07-16T12:20:35.7363479Z 12:20:35.736 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7369351Z 12:20:35.736 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_56[1].trx'.
2024-07-16T12:20:35.7378384Z 12:20:35.737 DEBUG: Parsed Visual Studio Test Times - duration: 26717.
2024-07-16T12:20:35.7439545Z 12:20:35.743 DEBUG: Parsed Visual Studio Test Counters - total: 64, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 64.
2024-07-16T12:20:35.7446935Z 12:20:35.744 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7447384Z 12:20:35.744 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_55.trx'.
2024-07-16T12:20:35.7459637Z 12:20:35.745 DEBUG: Parsed Visual Studio Test Times - duration: 19117.
2024-07-16T12:20:35.7467791Z 12:20:35.746 DEBUG: Parsed Visual Studio Test Counters - total: 3, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 3.
2024-07-16T12:20:35.7474197Z 12:20:35.747 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7479763Z 12:20:35.747 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_53[2].trx'.
2024-07-16T12:20:35.7488880Z 12:20:35.748 DEBUG: Parsed Visual Studio Test Times - duration: 23715.
2024-07-16T12:20:35.7504634Z 12:20:35.750 DEBUG: Parsed Visual Studio Test Counters - total: 18, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 18.
2024-07-16T12:20:35.7515498Z 12:20:35.751 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7523207Z 12:20:35.752 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_54[2].trx'.
2024-07-16T12:20:35.7532255Z 12:20:35.753 DEBUG: Parsed Visual Studio Test Times - duration: 27864.
2024-07-16T12:20:35.7550431Z 12:20:35.754 DEBUG: Parsed Visual Studio Test Counters - total: 31, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 31.
2024-07-16T12:20:35.7576158Z 12:20:35.757 DEBUG: The current user dir is '/home/vsts/work/1'.
2024-07-16T12:20:35.7582001Z 12:20:35.758 INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/TestResults/_fv-az625-444_2024-07-16_12_19_56.trx'.
2024-07-16T12:20:35.7590928Z 12:20:35.758 DEBUG: Parsed Visual Studio Test Times - duration: 24750.
2024-07-16T12:20:35.7607875Z 12:20:35.760 DEBUG: Parsed Visual Studio Test Counters - total: 8, failed: 0, errors: 0, timeout: 0, aborted: 0, executed: 8.
2024-07-16T12:20:35.7637544Z 12:20:35.763 INFO: Sensor C# Unit Test Results Import [csharp] (done) | time=112ms
2024-07-16T12:20:35.7645435Z 12:20:35.764 INFO: Sensor Zero Coverage Sensor
2024-07-16T12:20:35.8047299Z 12:20:35.804 INFO: Sensor Zero Coverage Sensor (done) | time=40ms

Hi,

Welcome to the community!

According to your log, there are no coverage files.

Is it possible to check your build agent workspace and see whether and where coverage reports are actually generated?

 
Ann

Hello @ganncamp ,

Thank you for your answer. Unfortunately I spent too much time on trying to make it work lately and I had (and still do) to move on to develop features for now.

I’ll come back to this topic in few weeks (I hope asap).

Thank you again.

I use below tasks and values for Dot Net 8 in ADO pipelines, SonarCloud analysis works for me

In SonarCloud analysis step, I pass these parameters:

sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/Coverage/coverage.xml          
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/testresults.trx

For test task, I use

     - task: DotNetCoreCLI@2
       condition: succeeded()
       displayName: Unit Tests
       inputs:
         command: 'test'
         arguments: '--logger "trx;LogFileName=testresults.trx" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput="$(Agent.TempDirectory)/Coverage/coverage.xml"'
         workingDirectory: '$(System.DefaultWorkingDirectory)/$(VS_Test_Sln_Path)'
       continueOnError: true

Also, ensure these NuGet packages are present in the test project file to generate coverage reports from MSBuild:

    <PackageReference Include="coverlet.msbuild" Version="6.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="6.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>