Python coverage badges, how to get them?
You can click on those badges and it'll generally take you to the service that provides them.
The coverage badge is provided by https://coveralls.io/:
Coveralls is a web service to help you track your code coverage over time, and ensure that all your new code is fully covered.
There is but one prerequisite:
- Your code must be hosted on GitHub
Once you have signed up and included the required configuration and integrations or packages when developing, you are given a image URL to include in your project documentation; the python-coveralls
project has:
.. image:: https://coveralls.io/repos/z4r/python-coveralls/badge.png?branch=master :target: https://coveralls.io/r/z4r/python-coveralls
in their README for example, which renders as:
If you want to generate badges on your own, you could try to load the total coverage percentage and then create an image, someting like this:
from PIL import Image, ImageDraw, ImageFontfrom coverage import coveragecov = coverage()cov.load()total = cov.report()# total = 79.0im = Image.new("RGB", (120, 20))fnt = ImageFont.load_default()d = ImageDraw.Draw(im)d.text((10, 5), "coverage:", fill=(255, 255, 255), font=fnt)d.rectangle([(80, 0), (150, 20)], fill=(220, 0, 0))d.text((90, 5), "{:.0f}%".format(total), fill=(0, 0, 0), font=fnt)
I have written a python badge generation package that produces badges very visually similar to the main badge services. It is highly flexible, you can import and use in your python code, or run from the command line. It is simple, and self-contained.
You can set the badge label and value, and you can set the color based on thresholds. There are pre-built settings for pylint, coverage, and pipeline success, but you can create any badge you like.
Here is a link to the github project with more detailed documentation: https://github.com/jongracecox/anybadge
Install with pip install anybadge
Example python code:
import anybadge# Define thresholds: <2=red, <4=orange <8=yellow <10=greenthresholds = {2: 'red', 4: 'orange', 6: 'yellow', 10: 'green'}badge = anybadge.Badge('pylint', 2.22, thresholds=thresholds)badge.write_badge('pylint.svg')
Example command line use:
anybadge --label pylint --value 2.22 --file pylint.svg 2=red 4=orange 8=yellow 10=green