Have html report include a coverage "badge"

Issue #427 closed
Former user 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"/>
</linearGradient>
<mask id="a">
<rect width="92" height="20" rx="3" fill="#fff"/>
</mask>
<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>
<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>
</g>
</svg>
"""

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 (10)

  1. Björn Dahlgren

    Sorry, wasn't logged in when creating this.
    EDIT: you may obviously delete the line: colors = 'red orange yellow yellowgreen green brightgreen'.split()

  2. Björn Dahlgren

    Yes, but how would I use that from my README if I only have a static webserver hosting the html report of the coverage?

  3. Florian Bruhin

    FWIW, I see @bjodah's usecase and now would like to do the same. The idea is that some kind of CI generates HTML reports for each push (see the leftmost column at http://qutebrowser.org:8010/waterfall for example), and then the readme could link to the .svg with some static link which shows the newest version, e.g. show an image like http://www.qutebrowser.org/coverage/archlinux/latest/coverage.svg in my case.

  4. 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.

  5. 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)

  6. 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.

  7. Log in to comment