Code coverage checking in GitHub Actions for Python

Ian Connor
2 min readJan 13, 2021

There is a joke around brushing and teeth. That is, Only brush the teeth you want to keep. Dentist struggle to get this message across and, despite all the evidence, lots of people still don’t brush and floss daily.

To me, this is the perfect analogy for tests in software development. You can get away without tests and, some people live their own lives without them. However, they are the exception, not the rule. From my experience, code will fail without tests. If you are like me and want to ensure you are testing, keeping your test coverage up, is vital.

I have created this straightforward script that will read in a coverage.xml file and ensure the coverage is above a certain percentage. As your coverage increases, you can increase this number to ensure you and your team doesn’t go backwards.

check_coverage code:

echo ‘cat //coverage/@line-rate’ | xmllint — shell coverage.xml | awk -F’[=”]’ ‘!/>/{print $(NF-1)}’

I am using GitHub Actions in this project, but this should fit easily into any CI (bitbucket, Jenkins, drone-ci, etc.). It is simply a bash script that exits with an error that should interrupt any CI pipeline.

github action example:

--

--