Skip to content

Instantly share code, notes, and snippets.

@tomgross
Last active Apr 10, 2019
Embed
What would you like to do?
Check the coverage of Python tests and compare with previous run in gitlab
# This script checks the coverage and test if it keeps at least the same
# Add this to your .gitlab-ci.yml
#
# coveragetest:
# ...
# cache:
# paths:
# - coverage_value.txt
# script:
# - bin/coverage run bin/test
# - bin/coverage report
# - bin/coverage report | python check_coverage.py
import re
import sys
import os.path
def main():
coverage_report = sys.stdin.read()
if not 'TOTAL' in coverage_report:
print('No coverage data found in stdin. -> FAILING')
print(coverage_report)
sys.exit(1)
# extract coverag (last element after whitspace without the `%` sign)
# TOTAL 116 22 81%
coverage_value = int(coverage_report.split()[-1][:-1])
if os.path.isfile('coverage_value.txt'):
with open('coverage_value.txt') as f:
try:
old_coverage_value = int(f.read())
except ValueError: # no int found
old_coverage_value = -1
if old_coverage_value != -1:
exit_status = int(coverage_value < old_coverage_value)
else:
exit_status = 0
if exit_status:
print('Coverage decreased by %s%%' % (old_coverage_value - coverage_value))
else:
print('Coverage is %s%%' % coverage_value)
else:
# no old file found, so we can't compare. Assume this is ok
exit_status = 0
with open('coverage_value.txt', 'w') as f:
f.write(str(coverage_value))
sys.exit(exit_status)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment