将代码覆盖率结果添加到 Azure Pipelines for .NET6 web API 和 docker
Add Code Coverage results to Azure Pipelines for .NET6 web API with docker
我正在尝试将代码覆盖率结果添加到 .NET 6 Web API 的 Azure 管道中,它被包装到 docker 容器中。
我用这些:
Nuget 添加:“coverlet.msbuild”
Docker 文件:
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/
我的流水线各自的行:
- 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'
我得到的是运行管道:
Calculating coverage result...
Generating report '/src/WebAPIProject.DataAccessLayer.Tests/TestResults/Coverage/coverage.json'
Generating report
'/src/WebAPIProject.DataAccessLayer.Tests/TestResults/Coverage/coverage.cobertura.xml'
但是代码覆盖率结果任务删除了这个:
##[warning]No code coverage results were found to publish.
与上述方法一样,测试非常有效,我希望 Coverage 也能正常工作,但事实并非如此。代码中的错误是什么?
找到答案了。
首先,我在项目
中修改了Dockerfile的/p:CoverletOutput
参数
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/
需要在命令行中添加另一个 docker cp
以复制 Coverage 结果,因此 cmdline 任务现在如下所示:
- 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
其中还涉及修改发布代码覆盖率结果任务,例如:
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage results'
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Build.ArtifactStagingDirectory)/coverage/coverage.cobertura.xml'
pathToSources: '$(Build.ArtifactStagingDirectory)/coverage/'
我的收获是,即使我设置了 Testresult 的文件夹来发布覆盖率结果,出于某种原因,docker 还没有将这些放入文件夹中。因此,通过上面的这个小解决方法,我终于可以同时复制测试和覆盖率结果。
我正在尝试将代码覆盖率结果添加到 .NET 6 Web API 的 Azure 管道中,它被包装到 docker 容器中。
我用这些: Nuget 添加:“coverlet.msbuild”
Docker 文件:
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/
我的流水线各自的行:
- 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'
我得到的是运行管道:
Calculating coverage result...
Generating report '/src/WebAPIProject.DataAccessLayer.Tests/TestResults/Coverage/coverage.json'
Generating report
'/src/WebAPIProject.DataAccessLayer.Tests/TestResults/Coverage/coverage.cobertura.xml'
但是代码覆盖率结果任务删除了这个:
##[warning]No code coverage results were found to publish.
与上述方法一样,测试非常有效,我希望 Coverage 也能正常工作,但事实并非如此。代码中的错误是什么?
找到答案了。 首先,我在项目
中修改了Dockerfile的/p:CoverletOutput
参数
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/
需要在命令行中添加另一个 docker cp
以复制 Coverage 结果,因此 cmdline 任务现在如下所示:
- 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
其中还涉及修改发布代码覆盖率结果任务,例如:
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage results'
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Build.ArtifactStagingDirectory)/coverage/coverage.cobertura.xml'
pathToSources: '$(Build.ArtifactStagingDirectory)/coverage/'
我的收获是,即使我设置了 Testresult 的文件夹来发布覆盖率结果,出于某种原因,docker 还没有将这些放入文件夹中。因此,通过上面的这个小解决方法,我终于可以同时复制测试和覆盖率结果。