Kevin YANG
Kevin YANG

Reputation: 441

Publish code coverage result failed in azuredevops pipeline

I have a .netcode test command and a publish code coverage results task in my pipeline.

config as below:

steps:
- task: DotNetCoreCLI@2
  displayName: 'Test Public API Project '
  inputs:
    command: test
    projects: '**/DWP.CDA.API.Test.csproj'
    arguments: '--output publish_output --configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Threshold=99 /p:ThresholdStat=total /p:CoverletOutput=$(Build.SourcesDirectory)\TestResults\Coverage\ --collect "Code coverage"'

steps:
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(Build.SourcesDirectory)/TestResults/Coverage/*cobertura.xml'
    reportDirectory: '($(Build.SourcesDirectory)/Src/TestResults/Coverage'

But it seems that the publish results doed not take effect,such messages will show

[warning]No code coverage results were found to publish.

Upvotes: 11

Views: 14286

Answers (3)

VivekDev
VivekDev

Reputation: 25389

Yes, as SapuSevn commented, it should be Agent.TempDirectory.

    - task: DotNetCoreCLI@2
      displayName: 'dotnet test'
      inputs:
        command: 'test'
        arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura'
        publishTestResults: true
        projects: 'tests/Frontends/GloboTicket.Web.Tests' # update with your test project directory

    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage report'
      inputs:
        codeCoverageTool: 'Cobertura'
        #summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.cobertura.xml' ## This is not working. What you need is the following.
        summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'

Upvotes: 3

Chris Lamothe
Chris Lamothe

Reputation: 1605

I had a similar issue, take a look at the output

Attachments:

My pipeline was putting the reports into a temp folder

Also my test project was missing a Nuget coverlet.msbuild

Upvotes: 0

Pedro Silva
Pedro Silva

Reputation: 717

Did you install and run the ReportGenerator tool as well to get code coverage report in the proper format? Your warning looks like the build task isn't finding the xml file to publish in the folder that you're looking in.

I've used the following yaml in the past to run and publish code coverage results. You will need to change it to find your projects, but otherwise it should work.

- task: DotNetCoreCLI@2
  displayName: 'Install ReportGenerator'
  inputs:
    command: custom
    custom: tool
    arguments: 'install --global dotnet-reportgenerator-globaltool'

- task: DotNetCoreCLI@2
  displayName: 'Run unit tests - $(buildConfiguration)'
  inputs:
    command: 'test'
    arguments: '--no-build --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)/TestResults/Coverage/'
    publishTestResults: true
    projects: '**/*.Tests.csproj'

- script: |
    reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:HtmlInline_AzurePipelines
  displayName: 'Create code coverage report'

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

Upvotes: 7

Related Questions