messerke
messerke

Reputation: 107

Add Code Coverage results to Azure Pipelines for .NET6 web API with docker

I'm trying to add Code coverage results to my Azure pipeline for a .NET 6 Web API, which is wrapped into a docker container.

I use these: Nuget added: "coverlet.msbuild"

Dockerfile:

RUN dotnet test "WebAPIProject.DataAccessLayer.Tests.csproj" --logger "trx;LogFileName=WebAPIProject.DataAccessLayer.Tests.trx" /p:CollectCoverage=true /p:CoverletOutputFormat=json%2cCobertura /p:CoverletOutput=/src/WebAPIProject.DataAccessLayer.Tests/TestResults/Coverage/

My Pipeline's respective rows:

      - task: Docker@2
    displayName: Build image for testing
    inputs:
      containerRegistry: '$(dockerRegistryServiceConnection)'
      repository: '$(imageRepository)'
      command: 'build'
      Dockerfile: '**/Dockerfile'
      buildContext: '$(Build.Repository.LocalPath)'
      tags: '$(tag)'
  - task: CmdLine@2
    displayName: 'Copy test and Code coverage results files out of the image'
    inputs:
      script: |
        docker build -f ./WebAPIProject.Core/Dockerfile --target build -t $(imageRepository):$(tag) . 
        docker create -ti --name testcontainer $(imageRepository):$(tag) 
        docker cp testcontainer:/src/WebAPIProject.DataAccessLayer.Tests/TestResults/ $(Build.ArtifactStagingDirectory)/TestResults
        docker rm -fv testcontainer
  - task: PublishTestResults@2
    displayName: 'Publish test results'
    inputs:
      testResultsFormat: 'VSTest'
      testResultsFiles: '*.trx'
      searchFolder: '$(Build.ArtifactStagingDirectory)/TestResults'
  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage results'
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: $(Build.ArtifactStagingDirectory)/TestResults/Coverage/coverage.cobertura.xml'
      pathToSources: '$(Build.ArtifactStagingDirectory)/TestResults/Coverage'

What I get is running the pipeline:

Calculating coverage result...
Generating report '/src/WebAPIProject.DataAccessLayer.Tests/TestResults/Coverage/coverage.json'
Generating report 
'/src/WebAPIProject.DataAccessLayer.Tests/TestResults/Coverage/coverage.cobertura.xml'

but the Code coverage result task drops this:

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

As with the method above, the tests are working like a charm, I expect the Coverage working as well, but it doesn't. What is the bug in the code?

Upvotes: 1

Views: 1387

Answers (1)

messerke
messerke

Reputation: 107

Found the answer. First, I modified the Dockerfile's /p:CoverletOutput param in the project

RUN dotnet test "WebAPIProject.DataAccessLayer.Tests.csproj" --logger "trx;LogFileName=WebAPIProject.DataAccessLayer.Tests.trx" /p:CollectCoverage=true /p:CoverletOutputFormat=json%2cCobertura /p:CoverletOutput=/TestResults/Coverage/

Needed to add another docker cp into the command line to copy the Coverage results as well, so the cmdline task looks like the following now:

- task: CmdLine@2
    displayName: 'Copy test and Code coverage results files out of the image'
    inputs:
      script: |
        docker build -f ./WebAPIProject.Core/Dockerfile --target build -t $(imageRepository):$(tag) . 
        docker create -ti --name testcontainer $(imageRepository):$(tag) 
        docker cp testcontainer:/src/WebAPIProject.DataAccessLayer.Tests/TestResults/ $(Build.ArtifactStagingDirectory)/TestResults
        docker cp testcontainer:/TestResults/Coverage/ $(Build.ArtifactStagingDirectory)/coverage
        docker rm -fv testcontainer

which involved modifying the Publish code coverage results task as well, like:

- task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage results'
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Build.ArtifactStagingDirectory)/coverage/coverage.cobertura.xml'
      pathToSources: '$(Build.ArtifactStagingDirectory)/coverage/'

The takeaway for me is that even I set the Testresult's folder to publish coverage results to as well, for some reason, docker hasn't put these into the folder. So with this little workaround above, I finally could achieve copying both test and coverage results.

Upvotes: 2

Related Questions