0% Code Coverage Reported But Correct Number Unit Tests (Using Azure Pipeline)

Hello. I hope I am posting this in the right channel. I am new to using SonarQube. I am trying to deploy coverage report to SonarQube (Enterprise Edition Version 9.9) as part of my Azure pipeline. Coverage reports are generated by Visual Studio (application being scanned is an asp.net web site using C#). When the pipeline finished, I can see that the SQ report reports the correct number of Unit Tests but the Code Coverage percent is 0% (coverage should be much higher). I am trying to get SonarQube to accurately report the Code Coverage percentage. Relevant pipeline script:

- task: SonarQubePrepare@5
  displayName:  'Prepare SQ'
  inputs:
     SonarQube: 'SonarQube'
     scannerMode: 'MSBuild'
     projectKey: 'someKey'
     extraProperties:
        sonar.verbose=true
- task: VSBuild@1
  displayName: 'Build'
  inputs:
    solution: '$(solution)'
    configuration: '$(buildConfiguration)'

-task: DotNetCoreCLI@2
   displayName: Run Server Unit Tests
   inputs:
     command: 'test'
     projects: **/*myproject.ProjectTests.csproj'
     arguments: '--collect "Code Coverage"'
-task: CopyFiles@2
   inputs:
      SourceFolder: '$(Agent.HomeDirectory)\_work\_temp'
      Contents: '**'
      TargetFolder: '$(Common.TestResultsDirectory)'

This code will generate .trk file (about 137kb) and folder that has a *.coverage and *.coveragexml files in the _temp folder of my build server and copies the files to "C:/MyAgent/_work/1/TestResults folder. I can also see in the Run Server Unit Test task that the Unit Tests ran successfully (and the number of passed tests).

In the SonarQube Analysis task, there are a couple of things that caught my eye:

  1. The output states that its “Parsing the Visual Studio Coverage XML Report” and that it is "Adding this code coverage report (*.coveragxml) to the cache for later use. It also provides Coverage Report Statistics (56 files, 0 main files with coverage, 56 test files, 0 project excluded files and 0 other files). It then states that “Code Coverage Report doesn’t contain any coverage data for the included file” . It then gives a link for support, which I tried to do but has not worked.

  2. After this warning, I see a list of CS files that are listed as “Missing blame information”

All tasks complete without error. But, when I log into SQ, I can see that the total number of Unit Tests are correct but the Code Coverage percentage is wrong. Can someone assist with how to get SQ to accurately report Code Coverage?

thanks!

Further, I can see that coverage file does get created and has all the line coverage data in it. But, when it gets copied over to the TestResults, it is buried in a sub directory. Could SQ not be finding it? This is what gets copied to the TestResults:

  • C:/agent/_work/1/TestResults (Folder)
    • ABC$_CDE_Date.trx (trx File)
    • ABC$_CDE_Date (folder)
      • ln (folder)
        • ABC (folder)
          • ABC$_CDE_Date.coverage
          • ABC$_CDE_Date.coveragexml
    • someguid (folder)
      - ABC$_CDE_Date.coverage (coverage file with all the line coverage details)

Hi,

I find moving the files around suspicious for two reasons

  • Coverage reports are supposed to be picked up automatically in C# analysis, as long as you run them in the proper sequence (which you appear to do). But they’re no doubt being looked for in the default/original output location. So the fact that you’re moving them after generation could be the problem.
  • If the paths in your coverage reports are relative, rather than absolute, then they’re going to be relative to the report’s original location. Once you move the reports, then the paths won’t match up anymore; analysis won’t be able to figure out which files are covered.

The part about missing blame information is a red herring in this context, but still something you want to address. Without blame data, analysis won’t be able to identify which code is “new”, or to assign issues correctly. But we can tackle that in a different thread.

 
Ann

Sorry for the delay in my response (getting over a virus). So, if I leave all the files where they get deployed to, then not only do I get 0% code coverage, but also the Number of Unit Tests are at 0 as well. If I copy the files to the TempFolder then I at least get the the Number of Unit Test correctly reported. Originally, the files get deployed to the Build Servers _temp directory. I am not sure why they are not getting picked up by SQ auto discovery? Maybe they are getting deleted first?

Hi,

Can you post your analysis log?

The analysis / scanner log is what’s output from the analysis command. Hopefully, the log you provide - redacted as necessary - will include that command as well.

This guide will help you find them.

 
Ann