Fail a github action if coverage requirements are not met
For those of you that after reach push want to be informed by email that your code repository is covered by a particular percentage of test coverage (what is test coverage?), here is a little helper for you:
1 How
tests.yml:
name: Tests
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
export SECRET_KEY="34234234234fsdfsdfsdffsdfsd24324dfsdfs"
coverage run manage.py test
- name: Check Test Coverage
run: |
COVERAGE_OUTPUT=$(coverage report --skip-covered)
COVERAGE_PERCENTAGE=$(echo "$COVERAGE_OUTPUT" | awk '/TOTAL/ {print $NF}')
echo "coverage percentage is $COVERAGE_PERCENTAGE"
echo COVERAGE_PERCENTAGE=$COVERAGE_PERCENTAGE >> $GITHUB_ENV
- name: Break job if coverage is not more than 95%
if: ${{ env.COVERAGE_PERCENTAGE < '95%' }}
run: exit 1
Now try to run this action with act locally or with a github action and see this build break if the test coverage is lower than 95%.