'How can I set a minimum unit test coverage with GitLab CI?
I have a project hosted on GitLab that already has working CI configuration. I would like to add the notion of "minimal code coverage".
What I would love is to force a positive delta (the code coverage of the Merge Request must be greater than the one of the target branch, except if it's already 100%).
I would settle to a "Minimum 80% coverage" kind of rule, but I'm sure I can do better.
I can't find anything in the doc, except the keyword coverage that grabs the coverage to display on the main page.
I would like something like this in .gitlab-ci.yml:
check_coverage:
stage: test
rules:
- if: '$CI_OPEN_MERGE_REQUESTS && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH'
script:
- compare-coverage.sh $CI_CURRENT_COVERAGE $CI_TARGET_COVERAGE # something that fails if delta < 0
How can I achieve that?
Solution 1:[1]
I did it that way:
# .gitlab-ci.yml
image: python:3.9
stages:
- tests
- min-coverage
variables:
MIN_COVERAGE: 80 # set the threshold at 80%
unit-test:
stage: tests
before_script:
- pip install pytest pytest-cov
script:
- coverage run -m pytest
- coverage json
- coverage report -m
artifacts:
paths:
- coverage.json
coverage: '/TOTAL.+ ([0-9]{1,3}%)/'
min-coverage-test:
stage: min-coverage
before_script:
- apt-get update && apt-get install jq -y
script:
- jsonStr="$(cat $CI_PROJECT_DIR/coverage.json)"
- coverage="$(echo $jsonStr | jq .totals.percent_covered)"
- if (( $coverage < $MIN_COVERAGE )) ; then echo "$coverage% of code coverage below threshold of $MIN_COVERAGE%" && exit 1 ; else exit 0 ; fi
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
