Windows Presentation Foundation (WPF) c# code not showing code coverage in SQ dashboard though unit tests success

Hi Team,

Windows Presentation Foundation (WPF) c# code not showing code coverage % in SQ dashboard though unit tests are success in Azure Devops pipelines. Here I have attached files (Run Code Analysis & Publish Quality Gate Result). Please suggest us to get code coverage % in Sonarqube dashboard for successful unit tests.

12_Run Code Analysis.txt (390.4 KB)
13_Publish Quality Gate Result.txt (31.2 KB)

Thanks,
Upendar

HI Team, All the test cases are passed thru VSTest task, but Run code analysis not showing the code coverage in the result.
I have doubt in the below run code analysis log, “Analyzing coverage after aggregate found ‘0’ coverage files”----becoz of this may be code coverage is not coming. How could we resolve this. ?
“Adding this code coverage report to the cache for later reuse: F:\tfs_work_temp\TestResults\54ea57e9-26da-4d9b-bc00-f831addc4d9a\TFSBuild_LOUSSPWTP1129_2021-12-06.03_27_40.coveragexml
DEBUG: Analyzing coverage after aggregate found ‘0’ coverage files.
DEBUG: The total number of file count statistics is ‘0’.
INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=0ms”

Please suggest on this. thanks, Upendar

Hello @upendaravireni

Did you check the Troubleshooting .NET code coverage import guide?

11_VsTest - testAssemblies.txt (350.0 KB)
12_Run Code Analysis.txt (433.7 KB)
Hi Andrei, thanks for your reply. I didn’t checked the Troubleshooting .NET code coverage import guide, becoz don’t have covering tools.

PFA logs of VSTest, Runcode analysis tasks, I tried the below options also, but no use.
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)\TestResults*.trx
sonar.cs.vscoveragexml.reportsPaths=$(Agent.TempDirectory)\TestResults*.coveragexml

Please review these and advise me to resolve this. thanks, Upendar

Hey @upendaravireni - just a note that the VSTest step in Azure DevOps pipelines does not produce a .coveragexml file but in fact produces a .coverage file.

I solved this with adding three steps to my pipeline:

  1. Use the NuGet tool to install the Microsoft.CodeCoverage package to a tools directory.
  2. Used a PowerShell step that leverages the coverage tool to find all .coverage files in a temp directory and convert them to .coveragexml file
  3. Use the reportgenerator@5 step to convert the .coveragexml file to a SonarQube report.

Now in your SonarQubePrepare@4 step, you can consume the reports as follows:
extraProperties: 'sonar.coverageReportPaths=$(Common.TestResultsDirectory)\SonarQube.xml'

PS: You could in theory skip the reportgenerator conversion and simply use extraProperties: 'sonar.cs.vscoveragexml.reportsPaths=$(Common.TestResultsDirectory)\*.coveragexml' instead.


Full pipeline YAML tasks:

- task: SonarQubePrepare@4
  ...
    extraProperties: 'sonar.coverageReportPaths=$(Common.TestResultsDirectory)\SonarQube.xml'

- task: NuGetCommand@2
  displayName: 'Run nuget install Microsoft.CodeCoverage'
  inputs:
    command: 'custom'
    arguments: 'install Microsoft.CodeCoverage -ExcludeVersion -OutputDirectory $(Build.SourcesDirectory)\tools'

# ...
# Restore and build here
# ... 
# Run some tests here
# ...

- task: PowerShell@2
  displayName: 'Create coveragexml files'
  inputs:
    targetType: 'inline'
    script: |
       $coverageFiles = Get-ChildItem "$(Common.TestResultsDirectory)" -File -Recurse -Include @("*.coverage");
       foreach ($coverageFile in $coverageFiles) {
        $coverageXmlFile = $coverageFile.BaseName + ".coveragexml";
        $coverageXmlFilePath = Join-Path $(Common.TestResultsDirectory) $coverageXmlFile;
        Write-Host "Converting $coverageFile to $coverageXmlFilePath";
        $createCoverageXmlFileCmd = "$(Build.SourcesDirectory)\tools\Microsoft.CodeCoverage\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:$coverageXmlFilePath $coverageFile";
        Invoke-Expression -Command $createCoverageXmlFileCmd;
      }

- task: reportgenerator@5
  displayName: 'Create coverage report'
  inputs:
    reports: '$(Common.TestResultsDirectory)\*.coveragexml'
    targetdir: '$(Common.TestResultsDirectory)'
    reporttypes: 'HtmlInline_AzurePipelines;Cobertura;SonarQube'
1 Like

Hi @KrylixZA , thanks for your reply.
We’re using nuget tool installer, artifactory nuget, prepare analysis configuration, MSbuild, VSTest, runcode analysis, publish quality gate result, publish build artifacts tasks in our Azure build pipeline. Now I’m able to find .trx file like below.

"Code coverage command line tool: I:\VS2019\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe
Fetching code coverage report information from TFS…
Attempting to locate a test results (.trx) file…
Looking for TRX files in: F:\tfs_work\6110\TestResults, F:\tfs_work\6110\s\TestResults
The following test results files were found: F:\tfs_work\6110\s\TestResults\TFSBuild_LOUSSPWTP1135_2021-12-07_06_40_02.trx
Absolute path to coverage file:

F:\tfs_work\6110\s\TestResults\TFSBuild_LOUSSPWTP1135_2021-12-07_06_40_02\In\LOUSSPWTP1135\TFSBuild_LOUSSPWTP1135_2021-12-07.06_39_50.coverage
The following code coverage attachments were found from the trx files: F:\tfs_work\6110\s\TestResults\TFSBuild_LOUSSPWTP1135_2021-12-07_06_40_02\In\LOUSSPWTP1135\TFSBuild_LOUSSPWTP1135_2021-12-07.06_39_50.coverage"
But still getting same like earlier as
“Adding this code coverage report to the cache for later reuse: F:\tfs_work\6110.\s\TestResults\TFSBuild_LOUSSPWTP1135_2021-12-07_06_40_02\In\LOUSSPWTP1135\TFSBuild_LOUSSPWTP1135_2021-12-07.06_39_50.coveragexml
Analyzing coverage after aggregate found ‘0’ coverage files.
The total number of file count statistics is ‘0’.”

I understood as, statistics count shouldn’t be ‘0’. Need to find way to get the some values.

Thanks,Upendar

Have you tried to use the report generator to force conversion to a SonarQube report type and then use the additional properties in the SonarQube prepare step to look for that particular coverage report?

Eventually through a process of elimination there will be nothing left to fix :stuck_out_tongue:

I’m using SQ prepare task parameter, sonar.cs.vscoveragexml.reportsPaths=***.coveragexml

But not sure exactly about using the report generator to force conversion to a SonarQube report type. Can you tell me how to proceed on this.

I am a bit puzzled.

You need to generate the code coverage reports using a code coverage tool.

You can find instructions to do that here: [Coverage & Test Data] Generate Reports for C#, VB.net

We have targeted the proper Test path in VSTest task, which allowed to get the coverage in the pipeline and towards sonarqube dashboard. thanks.

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