Azure DevOps PR测试结果全部显示为'new'问题求助
我将Azure DevOps流水线配置为合并到develop分支的Pull Request(PR)分支策略构建验证,该流水线会自动针对所有目标为develop的PR运行。流水线执行dotnet test、发布测试结果及代码覆盖率,YAML配置如下:
parameters: - name: configuration type: string default: 'Release' steps: - task: DotNetCoreCLI@2 inputs: command: 'test' projects: '**/*.Tests.csproj' arguments: '--filter "Category!=Local&TestCategory!=Local" -c ${{ parameters.configuration }} --collect:"XPlat Code Coverage" --logger "trx;LogFileName=$(Agent.TempDirectory)/TestResults.trx" -- ResultsDirectory=$(Agent.TempDirectory) RunConfiguration.DisableAppDomain=true' workingDirectory: '$(System.DefaultWorkingDirectory)' publishTestResults: false displayName: 'Test code' - task: PublishTestResults@2 displayName: 'Publish Test Results' inputs: testResultsFormat: 'VSTest' testRunSystem: 'VSTest' testResultsFiles: '**/*.trx' searchFolder: '$(Agent.TempDirectory)' mergeTestResults: false publishRunAttachments: true buildConfiguration: ${{ parameters.configuration }} buildPlatform: 'Any CPU' runTitle: 'Unit Tests' condition: always() - task: DotNetCoreCLI@2 inputs: command: custom custom: tool arguments: install --tool-path . dotnet-reportgenerator-globaltool displayName: Install ReportGenerator tool continueOnError: true - script: ./reportgenerator -reports:$(System.DefaultWorkingDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura" displayName: Create reports continueOnError: true - task: PublishCodeCoverageResults@2 displayName: 'Publish code coverage' inputs: summaryFileLocation: $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml continueOnError: true
我的主开发分支是develop,创建develop/feature1这类特性分支并向develop提交PR后,流水线运行成功,测试通过且生成了覆盖率数据,但PR的Tests标签下始终显示所有测试为“new”——即使未修改测试代码、develop分支已有成功测试基线、PR创建前已在develop上执行过流水线。
我已完成以下排查:
- 手动在
develop运行流水线确认基线存在,且该运行记录在Azure DevOps中可见、成功未删除; PublishTestResults任务生成了有效的trx文件;- 分支策略可正确触发PR流水线;
- 尝试自定义
runTitle无效; - 多次在
develop分支运行成功流水线,但PR仍显示所有测试为new。
请问为何Azure DevOps不使用目标分支的基线测试运行进行PR对比?需要哪些额外配置或条件才能正确关联基线与分支策略流水线?
核心原因
Azure DevOps关联PR测试与目标分支基线,需要满足测试运行的关键标识完全匹配,包括测试运行名称、构建配置、平台,以及流水线定义一致性。你的配置存在几个关键缺失点:
- 分支策略未启用目标分支的自动基线运行;
PublishTestResults任务的合并设置与标识配置不匹配;- 测试结果的关联逻辑依赖流水线定义与分支的一致性匹配。
具体修复步骤
1. 启用目标分支基线自动运行
进入项目分支策略设置(Repos > Branches > develop > Branch policies),找到对应的构建验证流水线:
- 点击“...”选择“Edit”;
- 勾选“Automatically run on new commits to this branch”并保存。
这样develop分支有新提交时会自动运行该流水线,生成可被PR测试对比的最新基线。
2. 调整PublishTestResults任务配置
修改任务参数,确保与基线运行的配置一致:
- task: PublishTestResults@2 displayName: 'Publish Test Results' inputs: testResultsFormat: 'VSTest' testRunSystem: 'VSTest' testResultsFiles: '**/*.trx' searchFolder: '$(Agent.TempDirectory)' mergeTestResults: true # 合并多测试项目结果为单个运行,匹配基线逻辑 publishRunAttachments: true buildConfiguration: ${{ parameters.configuration }} buildPlatform: 'Any CPU' runTitle: 'Unit Tests (${{ parameters.configuration }} - Any CPU)' # 加入配置和平台,确保标识唯一且一致 condition: always()
3. 确保流水线定义一致性
PR触发的流水线和目标分支上运行的流水线必须是同一个YAML定义,避免使用不同的流水线ID或参数配置。如果之前在develop上手动运行的是其他流水线,需改为使用当前的PR验证流水线。
4. 验证测试标识一致性
确保PR分支与目标分支中,测试类、测试方法的完全限定名(命名空间、类名、方法名)完全一致,否则Azure DevOps会将其识别为新测试。
内容的提问来源于stack exchange,提问作者Roman Matkovskyi

