Azure Devops CI/CD CodeCoverage with yml

Viewed 261

I have tried to create a code coverage for my webapi. I have followed this link.

I have changed the YML file and got the below build failed error in Azure Devops.

enter image description here

I have used the following code for this. How to fix this?

steps:
- task: DotNetCoreCLI@2
  displayName: 'Build project'
  inputs:
    projects: '**/*.csproj'
    arguments: '--output $(Build.BinariesDirectory) --configuration Release'

- task: DotNetCoreCLI@2
  displayName: 'Install .NET Core tools from local manifest'
  inputs:
    command: custom
    custom: tool
    arguments: 'restore'

- task: VSTest@2
  displayName: 'Unit Tests'
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*Tests*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(Build.BinariesDirectory)'

    arguments: '--no-build --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.BinariesDirectory)/TestResults/Coverage/'
    publishTestResults: true
    projects: '**/*.Tests.csproj'

- task: DotNetCoreCLI@2
  displayName: 'Create code coverage report'
  inputs:
    command: custom
    custom: tool
    arguments: 'run reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:HtmlInline_AzurePipelines'

- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage report'
  inputs:
    codeCoverageTool: 'cobertura'
    summaryFileLocation: '$(Build.SourcesDirectory)/**/coverage.cobertura.xml'
1 Answers

Your Unit Test task places the output under $(Build.BinariesDirectory), but your reports task specifies $(Build.SourcesDirectory). Now, this may be correct if binaries are within source, but without those values, I can't be sure.

I'd rule out I would verify that the file exists where you expect it to be. Try adding this to your pipeline before the coverage task:

  - pwsh: |
      Write-Host "Checking Sources"
      Get-ChildItem $(Build.SourcesDirectory)/coverage.cobertura.xml -Recurse
      Write-Host "Checking Binaries"
      Get-ChildItem $(Build.BinariesDirectory)/coverage.cobertura.xml -Recurse

Run the pipeline again and look at the log output for that task. If the file exists, then it could be related to the tool installation.

Related