0
votes

I was using Azure pipelines to run Unit tests and SonarQube integration. When I use cobertura for unit tests, I am unable to get code coverage results passed onto SonarQube, although I see the results in pipeline logs. However, this same thing works when I use OpenCover.

I also see this warning in pipeline logs although I am uncertain if this is related: “Missing blame information for the following files”

The pipeline currently looks as follows

 - task: DotNetCoreCLI@2
    inputs:
      command: ‘test’
      projects: | 
        <test-proj>
      arguments: ‘/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura’
    displayName: ‘Run Tests’

 - task: SonarQubePrepare@4
    inputs: 
      SonarQube: <Service-Endpoint>
      scannerMode: ‘MSBuild’
      projectKey: ‘test:service’
      projectName: ‘test:service’
      extraproperties: | 
         sonar.flex.cobertura.reportPaths=$(Build.SourcesDirectory)/**/coverage.cobertura.xml 
  
 - task: DotNetCoreCLI@2
     <Running DotNet build>

 - task: SonarAnalyze@4
  

On running the DotNet Test stage, the test results are obtained in this path - E:\agent-5\2\s\test\ApiTest\coverage.cobertura.xml

Any suggestions would be appreciated.

2
How's your issue going? Is the following reply helpful?Cece Dong - MSFT

2 Answers

1
votes

The order of your steps needs to change from test -> prepare for tests -> build -> analyze to prepare for tests -> build -> test -> analyze, as in:

 - task: SonarQubePrepare@4
    inputs: 
      SonarQube: <Service-Endpoint>
      scannerMode: ‘MSBuild’
      projectKey: ‘test:service’
      projectName: ‘test:service’
      extraproperties: | 
        sonar.flex.cobertura.reportPaths=$(Build.SourcesDirectory)/**/coverage.cobertura.xml 
  
 - task: DotNetCoreCLI@2
     <Running DotNet build>

 - task: DotNetCoreCLI@2
    inputs:
      command: ‘test’
      projects: | 
        <test-proj>
      arguments: ‘/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura’
    displayName: ‘Run Tests’

 - task: SonarAnalyze@4
1
votes

Update: Apparently, SonarQube does not allow publishing Cobertura results file by default (Or atleast I could not get it to run).

Used a workaround - I had to pass the OpenCover test results file to SonarQube. And for pushing the results onto Azure Devops, I had to use reportgenerator tool to convert the results to Cobertura , before passing them to Azure Devops.