3

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.

user15338350
  • 203
  • 1
  • 4
  • 9

2 Answers2

2

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.

user15338350
  • 203
  • 1
  • 4
  • 9
  • 1
    You could [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), this can be beneficial to other community members reading this thread. – Cece Dong - MSFT Mar 12 '21 at 09:59
  • The root cause of this problem is that SonarQube does not support Cobertura format and you need OpenCover, or dotCover format. Using reportgenerator is one of the solutions but it's unnesesary complicated: you can make "dotnet test" command to return the report in Opencover format by passing special parameter and then pick up the opencover report. Check my answer below. – Alex Langer Feb 20 '23 at 18:31
1

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
WaitingForGuacamole
  • 3,744
  • 1
  • 8
  • 22
  • The code coverage reports are coming fine on Azure pipeline. However, they are not getting passed on to SonarQube when cobertura are used. – user15338350 Mar 06 '21 at 20:26
  • Once you have prepared sonarqube task it will start monitoring the logs produced by build and start sending them to SonarQube, this will pick up code coverage as well hence order of the task is important. – Sujit Singh Mar 06 '21 at 22:59