You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让VSTest测试失败时标记为失败而非警告?

解决VSTest步骤失败时Job状态未标记为失败的问题

核心问题分析

你的VSTest步骤设置了continueOnError: true,这会导致测试失败时该步骤仅标记为警告(部分成功),而非直接失败。整个GetAndRunSmokes Job的状态会因此保持为成功,进而使得依赖该Job的:

  • succeeded()条件的NotifyTeamsChannelIfTestsPass任务错误执行
  • failed()条件的NotifyTeamsChannelIfTestsFail任务无法触发

同时注意到依赖任务中存在拼写错误:dependsOn: DeletaGetAndRunSmokes里的Deleta是笔误,应该改为GetAndRunSmokes,否则依赖关系不生效。

解决方案

方案1:直接移除continueOnError: true(推荐)

如果不需要在测试失败后继续执行GetAndRunSmokes里的后续步骤(当前该Job中VSTest是最后一步),直接移除continueOnError: true即可。这样测试失败时VSTest步骤会直接标记为失败,整个Job状态变为失败,后续依赖的条件判断会正常工作。

修改后的VSTest步骤:

- task: VSTest@3
  displayName: VsTest - Dashboard
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    testSelector: testAssemblies
    testAssemblyVer2: |
      D:\a\1\s\DeltaSmokeTests\bin\Release\net9.0\SmokeTests.dll
      !**\obj\**
    testFiltercriteria: 'TestCategory=DeltaDashboard'
    testRunTitle: 'Delta Regression DeltaDashboard'
    runTestsInIsolation: true
    rerunFailedTests: true
    rerunFailedThreshold: 50
    rerunMaxAttempts: 2
  condition: succeededOrFailed()
  timeoutInMinutes: 10

同时修正所有依赖任务的dependsOn值:

- job: NotifyTeamsChannelIfTestsPass
  dependsOn: GetAndRunSmokes  # 修正拼写错误
  condition: succeeded()
  # ... 其余内容不变

- job: NotifyTeamsChannelIfTestsFail
  dependsOn: GetAndRunSmokes  # 修正拼写错误
  condition: failed() 
  # ... 其余内容不变

- job: CreateReportFiles
  dependsOn: GetAndRunSmokes  # 修正拼写错误
  condition: always()
  # ... 其余内容不变

方案2:保留continueOnError但强制标记Job失败

如果需要在测试失败后仍执行GetAndRunSmokes里的后续步骤(当前场景不需要,但如果有后续步骤时可用),可以添加一个PowerShell步骤,检查VSTest的结果并手动标记Job失败。

示例配置:

- task: VSTest@3
  displayName: VsTest - Dashboard
  inputs:
    # ... 其余输入不变
  continueOnError: true  # 保留该设置
  condition: succeededOrFailed()
  timeoutInMinutes: 10
  name: VSTestStep  # 给步骤命名,方便后续引用

- task: PowerShell@2
  displayName: Check Test Results and Mark Job Failed
  condition: eq(variables['VSTestStep.TestRunFailed'], 'true')
  inputs:
    targetType: 'inline'
    script: |
      # 使用Logging Command标记Job失败
      Write-Host "##vso[task.complete result=Failed;]Tests failed"

这个方案中,VSTest步骤失败后会继续执行后续的PowerShell步骤,通过VSTestStep.TestRunFailed变量判断测试是否失败,若失败则用Logging Command强制将Job标记为失败,确保后续依赖的条件判断正常工作。

验证效果

修改完成后,当VSTest步骤测试失败时:

  • GetAndRunSmokes Job会标记为失败
  • NotifyTeamsChannelIfTestsFail任务会触发
  • NotifyTeamsChannelIfTestsPass任务不会执行
  • CreateReportFiles任务因condition: always()仍会正常执行

内容的提问来源于stack exchange,提问作者Kev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 11:44:50