Have html report include a coverage "badge"

Issue #427 new
Anonymous created an issue

This is a feature request rather than an actual issue. I apologize if this is not the apropirate forum for this.

I was wondering if it would be possible to generate a svg badge for inclusion in the html report. My use-case is to have a badge in my README pointing to the master's branch html coverage report folder (which is published automatically by my CI server).

I reverse engineered shields.io's version into this script:

# this template was generated from shields.io on 2015-10-11
template = """
<svg xmlns="http://www.w3.org/2000/svg" width="92" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
<mask id="a">
<rect width="92" height="20" rx="3" fill="#fff"/>
<g mask="url(#a)">
<path fill="#555" d="M0 0h63v20H0z"/>
<path fill="{0:s}" d="M63 0h29v20H63z"/>
<path fill="url(#b)" d="M0 0h92v20H0z"/>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="31.5" y="15" fill="#010101" fill-opacity=".3">coverage</text>
<text x="31.5" y="14">coverage</text>
<text x="76.5" y="15" fill="#010101" fill-opacity=".3">{1:s}%</text>
<text x="76.5" y="14">{1:s}%</text>

def write_cov_badge_svg(path, percent):
    colors = 'red orange yellow yellowgreen green brightgreen'.split()
    colors = '#e05d44 #fe7d37 #dfb317 #a4a61d #97CA00 #4c1'.split()
    limits_le = 50, 60, 70, 80, 90, 100
    c = next(clr for lim, clr in zip(limits_le, colors) if percent <= lim)
    with open(path, 'wt') as f:
        f.write(template.format(c, str(percent)))

if __name__ == '__main__':
    import sys
    assert len(sys.argv) == 3
    write_cov_badge_svg(sys.argv[1], int(sys.argv[2]))

the resulting svg file is about 0.7 kB.

Comments (9)

  1. Ned Batchelder repo owner

    Hmm, I understand now, thanks. I wonder if this is better left to a separate tool, if coverage.py could make the total coverage number available in some convenient way.

  2. Björn Dahlgren

    That would definitely work! I guess I could "grep" for it, but it would be nice if there was a documented way to get hold of that number (so that the grep script is stable with respect to updates of coverage.py)

  3. Ned Batchelder repo owner

    The documented way to get it now is to use the XML report, and parse the XML. The HTML report could include a small file containing the results, either as text, or JSON. There is a status.json file, but that is for internal use, and does not include the final results.

  4. Log in to comment