在azure Devops中,在Multi-Agent上运行单元测试(VSTest),如何收集代码覆盖率文件并发布到Artifact中。 pipeline中仅显示测试结果.trx文件,无法查看.coverage文件。
YAML 模板文件:
# Unit Test Execution
- job: UnitTests_Execution
displayName : 'Unit Tests execution'
dependsOn: 'UnitTest_Build'
condition: and(succeeded(), ${{ and(eq(parameters.RunCondition, 'True'), eq(parameters.RunUnitTest, 'True')) }})
variables:
BuildPlatform: 'any cpu'
BuildConfiguration: 'release'
pool:
name: 'Self-Hosted Agent'
strategy:
parallel: 8
steps:
- checkout: none
- task: DownloadPipelineArtifact@2
displayName: 'Download Build Artifacts'
inputs:
artifactName: unitTests
targetPath: '$(System.ArtifactsDirectory)/unitTests'
- task: VSTest@2
displayName: 'Unit Tests'
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.ArtifactsDirectory)/unitTests'
resultsFolder: '$(System.ArtifactsDirectory)\TestResults\'
runOnlyImpactedTests: false
vsTestVersion: '${{ parameters.VsVersion }}'
# runInParallel: true
codeCoverageEnabled: true
testRunTitle: 'Unit Testing service layer'
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
rerunFailedTests: true
- task: PublishPipelineArtifact@1
displayName: 'Publish Artifact: Code Coverage'
inputs:
targetPath: '$(build.ArtifactStagingDirectory)/TestResults'
artifact: CodeCoverageResult
使用多代理,我尝试收集代码 covearge 文件并合并到单个文件中并发布到 Sonar Cloud Dashboard 中
例外:管道日志中仅显示测试结果 .trx 文件,不显示 .coverage 文件。但在单代理中,除了多代理概念之外,它正在工作。
使用多代理作业时,我可以重现相同的问题。
使用多代理作业时,vstest 任务似乎不支持“代码覆盖率”选项。
Pipeline 运行将指向相同的 Test Run 链接,而不执行真正的测试方法。
要解决此问题,您可以使用参数循环作业来运行多个作业。
这是一个例子:
parameters:
- name: runtimes
type: object
default: [1,2,3,4,5,6,7,8]
jobs:
- ${{ each runtime in parameters.runtimes }}:
- job: UnitTests_Execution_${{ runtime }}
displayName : 'Unit Tests execution ${{ runtime }}'
dependsOn: 'UnitTest_Build'
condition: and(succeeded(), ${{ and(eq(parameters.RunCondition, 'True'), eq(parameters.RunUnitTest, 'True')) }})
variables:
BuildPlatform: 'any cpu'
BuildConfiguration: 'release'
pool:
name: 'Self-Hosted Agent'
steps:
- checkout: none
- task: DownloadPipelineArtifact@2
displayName: 'Download Build Artifacts'
inputs:
artifactName: unitTests
targetPath: '$(System.ArtifactsDirectory)/unitTests'
- task: VSTest@2
displayName: 'Unit Tests'
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.ArtifactsDirectory)/unitTests'
resultsFolder: '$(System.ArtifactsDirectory)\TestResults\'
runOnlyImpactedTests: false
vsTestVersion: '${{ parameters.VsVersion }}'
# runInParallel: true
codeCoverageEnabled: true
testRunTitle: 'Unit Testing service layer'
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
rerunFailedTests: true
- task: PublishPipelineArtifact@1
displayName: 'Publish Artifact: Code Coverage'
inputs:
targetPath: '$(build.ArtifactStagingDirectory)/TestResults'
artifact: CodeCoverageResult
在这种情况下,它也会运行 8 个并行作业。它可以显示预期的行为。
这是结果:.trx 文件和 .coverage 文件