如何在覆盖率失败时使 Azure DevOps 管道失败?

huangapple go评论64阅读模式
英文:

How to fail an azur devOps pipeline on coverage fail?

问题

我正在使用Microsoft DevOps Forge构建我的流水线。如果覆盖率低于80%,我想让测试流水线失败,但我在Microsoft文档中找不到相关信息。

这是我的test.yaml文件:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: windows-latest

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: "3.10"
    displayName: "使用Python 3.10"
  - script: python -m pip install toto pytest pytest-cov pytest-deadfixtures
    displayName: "安装依赖项"
  - script: pytest --color=yes --cov --cov-report=html
    displayName: "使用pytest进行测试"
英文:

I'm building my pipelines in the Microsoft dev Ops forge. I would like to make the test pipeline to fail if the coverage is < 80% but I don't find the information in the microsoft documentation.

Here is my test.yaml file:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: windows-latest

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: &quot;3.10&quot;
    displayName: &quot;Use Python 3.10&quot;
  - script: python -m pip install toto pytest pytest-cov pytest-deadfixtures
    displayName: &quot;Install dependencies&quot;
  - script: pytest --color=yes --cov --cov-report=html
    displayName: &quot;Test with pytest&quot;

答案1

得分: 0

你应该切换到term输出:

- bash: |
    OUTPUT=$(pytest --color=yes --cov --cov-report=term | grep 'TOTAL' | awk '{print $(NF)}')

    echo $OUTPUT

    COVERAGE="${OUTPUT%\%}"  # 删除百分号
    if (( COVERAGE < 80 )); then
      echo "代码覆盖率低于80%";
      exit 1;
    else 
      echo "代码覆盖率为 $COVERAGE%";
    fi    
  displayName: '运行 pytest'
英文:

You should switch to term output:

- bash: |
    OUTPUT=$(pytest --color=yes --cov --cov-report=term | grep &#39;TOTAL&#39; | awk &#39;{print $(NF)}&#39;)

    echo $OUTPUT

    COVERAGE=&quot;${OUTPUT%\%}&quot;  # Remove the percent sign
    if (( COVERAGE &lt; 80 )); then
      echo &quot;Code coverage is below 80%&quot;;
      exit 1;
    else 
      echo &quot;Code coverage is $COVERAGE%&quot;;
    fi    
  displayName: &#39;Run pytest&#39;

huangapple
  • 本文由 发表于 2023年7月7日 02:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定