流水线预期失败后如何正常显示测试结果?
流水线问题修复:测试结果不显示及失败标记优化
问题分析
- 测试结果不显示:RunTests阶段生成的测试结果文件无法在Publish阶段被读取,因为不同阶段的Agent工作目录相互隔离,文件不能直接跨阶段访问。
- 流水线标记失败:Publish阶段的
PublishTestResults@2任务设置了failTaskOnFailedTests: true,只要存在测试失败就会触发任务失败,进而导致整个流水线被标记为失败。
修复方案
1. 跨阶段传递测试结果文件
在RunTests阶段执行测试后,添加PublishPipelineArtifact任务将测试结果文件夹上传为流水线工件,让Publish阶段可以下载后再发布结果。
2. 控制流水线失败标记(按需选择)
如果希望即使测试失败,流水线仍标记为成功,可将PublishTestResults@2的failTaskOnFailedTests改为false,或者给该任务添加continueOnError: true。
3. 清理冗余任务
- 删除CopyThirdParty阶段中空的
CmdLine@2任务 - 删除Build阶段Configure任务中重复的CMake配置步骤
- 在RunTests的PowerShell脚本中先创建TestResults文件夹,避免因文件夹不存在导致无法生成测试报告
修改后的完整YAML配置
trigger: none # Variable to store where the initial code checkout path variables: - name: BuildDir value: Test stages: - stage: Checkout pool: $(PoolName) jobs: - job: CheckoutRedwoodAndSealink displayName: Checking out steps: - checkout: git://Redwood/Redwood@working/conifer path: 'Redwood' displayName: Checking out Redwood - checkout: self path: 'Sealink' displayName: Checking out Sealink - task: PowerShell@2 displayName: Change the value of BuildDir # Store the checkout location inputs: targetType: 'inline' script: 'Write-Host "##vso[task.setvariable variable=BuildDir;isOutput=true]$(Agent.BuildDirectory)"' name: setvar - stage: CopyThirdParty dependsOn: Checkout pool: $(PoolName) variables: # Set the build directory BuildDir: $[stageDependencies.Checkout.CheckoutRedwoodAndSealink.outputs['setvar.BuildDir'] ] jobs: - job: CopyRedwoodTP displayName: Copying Redwood Third Party steps: - checkout: none - task: CopyFiles@2 inputs: SourceFolder: 'C:\Redwood\third-party' Contents: '**' TargetFolder: '$(BuildDir)\Redwood\third-party' OverWrite: true - stage: Build dependsOn: - Checkout - CopyThirdParty pool: $(PoolName) variables: # Set the build directory BuildDir: $[stageDependencies.Checkout.CheckoutRedwoodAndSealink.outputs['setvar.BuildDir'] ] jobs: - job: Configure displayName: Configuring CMake steps: - checkout: none - task: CMake@1 inputs: cmakeArgs: -S ./ -B ./build workingDirectory: $(BuildDir)/Sealink - job: Build displayName: Building dependsOn: Configure steps: - checkout: none - task: CMake@1 inputs: cmakeArgs: '--build ./build --config Debug' workingDirectory: $(BuildDir)/Sealink continueOnError: true # if first build failed - try again - job: Build2 condition: failed() dependsOn: Build steps: - checkout: none - task: CMake@1 displayName: Configuring CMake (2) inputs: cmakeArgs: -S ./ -B ./build workingDirectory: $(BuildDir)/Sealink - task: CMake@1 displayName: Building (2) inputs: cmakeArgs: '--build ./build --config Debug' workingDirectory: $(BuildDir)/Sealink - stage: RunTests pool: $(PoolName) dependsOn: - Build - Checkout variables: BuildDir: $[stageDependencies.Checkout.CheckoutRedwoodAndSealink.outputs['setvar.BuildDir'] ] jobs: - job: RunTests displayName: Running Unit Tests continueOnError: true steps: - checkout: none - task: PowerShell@2 inputs: targetType: 'inline' script: | # 创建测试结果文件夹 New-Item -ItemType Directory -Path ./TestResults -Force # 执行测试并生成报告 $(BuildDir)/Sealink/build/UnitTesting/Debug/Test.exe --gtest_output="xml:./TestResults/" # 上传测试结果为流水线工件 - task: PublishPipelineArtifact@1 inputs: targetPath: './TestResults' artifact: 'TestResults' publishLocation: 'Container' - stage: Publish pool: $(PoolName) dependsOn: - RunTests - Checkout jobs: - job: publish steps: - checkout: none # 下载测试结果工件 - task: DownloadPipelineArtifact@2 inputs: artifact: 'TestResults' path: '$(System.DefaultWorkingDirectory)/TestResults' - task: PublishTestResults@2 displayName: Publishing unit test results inputs: testResultsFormat: 'JUnit' testResultsFiles: '*.xml' searchFolder: '$(System.DefaultWorkingDirectory)/TestResults' # 根据需求设置:如果允许测试失败但流水线成功,改为false failTaskOnFailedTests: false
内容的提问来源于stack exchange,提问作者RoachFIsher
相关产品推荐
相关产品推荐

