Prometheus Prometheus - 3 months ago 22
Python Question

Python coverage badges, how to get them?

I am using Python coverage to test my apps. Looking at other developers on GitHub I see they have a small badge which shows the percentage of coverage. Using coverage, how can I generate these badges?

The coverage badge is the one I'm looking at below.

badges

Update: There are packages that generate badges i.e. nose-htmloutput! Cool

Answer

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, ImageFont
from coverage import coverage

cov = coverage()
cov.load()
total = cov.report()

# total = 79.0

im = 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)

simple coverage badge