Search code examples
azure-devopsazure-pipelinespytestpytest-cov

How to fail an azur devOps pipeline on coverage fail?


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: "3.10"
    displayName: "Use Python 3.10"
  - script: python -m pip install toto pytest pytest-cov pytest-deadfixtures
    displayName: "Install dependencies"
  - script: pytest --color=yes --cov --cov-report=html
    displayName: "Test with pytest"

Solution

  • You should switch to term output:

    - bash: |
        OUTPUT=$(pytest --color=yes --cov --cov-report=term | grep 'TOTAL' | awk '{print $(NF)}')
    
        echo $OUTPUT
    
        COVERAGE="${OUTPUT%\%}"  # Remove the percent sign
        if (( COVERAGE < 80 )); then
          echo "Code coverage is below 80%";
          exit 1;
        else 
          echo "Code coverage is $COVERAGE%";
        fi
      displayName: 'Run pytest'