Python coverage badges, how to get them?

Question:

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

Asked By: Prometheus

||

Answers:

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:

1

Answered By: Martijn Pieters

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

Answered By: Carsten

Based on the answer by Carsten, there is now a MIT licensed tool on PyPI for generating SVG coverage badges:

https://github.com/dbrgn/coverage-badge
https://pypi.python.org/pypi/coverage-badge

Answered By: Danilo Bargen

You can use Badge which is hosted at http://badge.kloud51.com

The source code is available at Github: https://github.com/SavandBros/badge you can look at the code and see how it’s been generated if you’d like to learn about it.

Answered By: Alireza Savand

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=green
thresholds = {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
Answered By: JGC

All of the answers above depend on some library or third-party providers (coveralls et al).

In my case, I needed to generate such a badge for code coverage. I wanted it to be simple and not load my docker image with unnecessary library and be less CPU intensive.
I realized having an image/svg+xml makes more sense than generating a png file in this case.

Here is a simple bash code script and below are the advantages I think over generating jpeg/png file

# generate coverage icon
COVERAGE_BADGE="${COVERAGE_DIR}/coverage.svg"

#Get this coverage from whatever tool you are using. In our case it was go tool cover
COVERAGE_TEXT="78.3%"
SVG_XML_DATA='<svg  width="124" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="124" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h52v20H0z"/>
<path fill="#97CA00" d="M52 0h72v20H52z"/>
<path fill="url(#b)" d="M0 0h124v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,Geneva,sans-serif" font-size="11">
<text x="26" y="15" fill="#010101" fill-opacity=".3">gocov</text>
<text x="26" y="14">gocov</text>
<text x="87" y="15" fill="#010101" fill-opacity=".3">PLACEHOLDER</text>
<text x="87" y="14">PLACEHOLDER</text>
</g>
</svg>'

SVG_XML_DATA_FILLED=$(sed "s/PLACEHOLDER/$COVERAGE_TEXT/g" <<<"$SVG_XML_DATA")
echo "$SVG_XML_DATA_FILLED" > $COVERAGE_BADGE

# upload bagde to s3
aws s3api put-object 
    --bucket dev.team 
    --content-type "image/svg+xml" 
    --key "coverage/${CODEBUILD_SOURCE_VERSION}.svg" 
    --body "${COVERAGE_BADGE}"

We used s3 to host svg but you technically host it on any http server

The advantages over png generation were pretty clear

  1. No dependency on any library
  2. Most browsers support rendering
    image/svg+xml
  3. It’s all text, no binary generation. This was critical
    in our case as code coverage ran on every commit and we wanted to
    reduce our AWS CPU time.
Answered By: rajeshnair

To complete Carsten’s answer and the associated comments, you can now use the genbadge commandline tool (installed with pip install genbadge) to generate a badge for a few tools including pytest, coverage and flake8. Options are provided to generate this badge using shields.io HTTP API or a local SVG template included in the package, resulting in badges such as:

enter image description here

The command

> genbadge coverage

Should suit your needs. See genbadge documentation for details, in particular to see how you can make those badges redirect the user to the test/coverage/flake8 report. (I’m the author by the way 😉 )

Answered By: smarie

It cost me about one day to show this badge, finally I chose Github Action+Codecov, and the steps can not be too simple:

See https://github.com/codecov/codecov-action

Answered By: Waket Zheng

Here is how I did it for a special use case. Python package. Sphynx documentation. I wanted to click on the badge and go to a coverage html table. My Python package is called pycax.

This requires pytest, coverage and coverage-badge Python packages.

In my docs/Makefile

codecov:
    python3 -m pytest -rxs --cov=pycax --cov-report term-missing ../pycax
    @coverage html -d $(BUILDDIR)/html/_codecoverage
    @rm $(BUILDDIR)/html/_codecoverage/.gitignore
    @coverage-badge -o $(BUILDDIR)/html/coverage.svg
    @echo
    @echo "Code coverage finished. The HTML pages are in $(BUILDDIR)/html/_codecoverage."

To make my documentation I run (well, actually it happens in a GitHub Action)

cd docs
make html codecov

In my index.rst file I have

|coverage| 

<body>

.. |coverage| image:: https://<username>.github.io/pycax/coverage.svg
   :target: https://<username>.github.io/pycax/_codecoverage

Note, the docs are built in docs/_build/html and that it getting pushed to gh-pages branch on remote

Answered By: Eli Holmes
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.