【问题标题】:How to fail AzureDevops pipeline when test files are not found找不到测试文件时如何使 AzureDevops 管道失败
【发布时间】:2021-07-29 23:15:18
【问题描述】:

我的管道中有以下 DotNet 测试任务

          displayName: 'unit tests'
          inputs:
              command: test
              projects: '**/*Unit*.csproj'
              publishTestResults: true
              arguments: '/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=results/'

如果没有与项目模式匹配的文件:'**/*Unit*.csproj',我如何使管道失败? 目前,它会显示当前的错误信息并继续下一个任务

##[warning]Project file(s) matching the specified pattern were not found.

【问题讨论】:

  • 这张票有更新吗?如果该建议可以为您提供帮助,请随时告诉我。只是提醒this

标签: azure .net-core azure-devops dotnetcorecli


【解决方案1】:

使用Visual Studio Test 任务。它有一个minimumExpectedTests参数,所以如果你把它设置为1,如果运行了0个测试,任务就会失败。

【讨论】:

    【解决方案2】:

    也可以运行一个 bash 脚本来检查测试结果文件是否存在(并且结果的数量大于 0):

    - bash: |
        if [ $(test_results_host_folder)**/*.trx ] && [ $(grep -E "<UnitTestResult" $(test_results_host_folder)**/*.trx -c) -gt 0 ]; then
          echo "##vso[task.setVariable variable=TESTRESULTFOUND]true"
        fi
      displayName: Check if test results file & results >= 1
    - script: |
          echo No test result found
          exit 1
      displayName: No test result found
      condition: ne(variables.TESTRESULTFOUND, 'true')
    

    【讨论】:

    • 感谢您的建议。
    【解决方案3】:

    如果你的 yaml 管道中有阶段,那么你可以这样做,如果上一个阶段失败,这将不会运行下一个阶段

    stages:
    - stage: unittests
      displayName: 'unit tests'
    
    - stage: nextstage
      dependsOn: unittests
      displayName: 'unit tests'
    

    【讨论】:

    【解决方案4】:

    据我所知,我们无法将任务本身设置为使管道在找不到文件时失败。

    解决方法:

    您可以使用Build Quality Checks extension构建质量检查任务

    此任务可以扫描所有设置的任务并检查警告。如果警告的数量大于设置的上限,管道将失败。

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-25
      • 2021-07-08
      • 2022-12-09
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      相关资源
      最近更新 更多