nedbat / coveragepy

The code coverage tool for Python
https://coverage.readthedocs.io
Apache License 2.0
2.96k stars 428 forks source link

Have html report include a coverage "badge" #427

Open nedbat opened 8 years ago

nedbat commented 8 years ago

Originally reported by Anonymous


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.


nedbat commented 8 years ago

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.

nedbat commented 8 years ago

Original comment by Björn Dahlgren (Bitbucket: bjodah, GitHub: bjodah)


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)

nedbat commented 8 years ago

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.

nedbat commented 8 years ago

Original comment by Florian Bruhin (Bitbucket: The-Compiler, GitHub: The-Compiler)


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.

nedbat commented 8 years ago

Original comment by Björn Dahlgren (Bitbucket: bjodah, GitHub: bjodah)


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

nedbat commented 8 years ago

Isn't it easier to use the shields.io URL syntax?: https://img.shields.io/badge/--.svg

https://img.shields.io/badge/coverage-13%25-f00.svg

nedbat commented 8 years ago

Original comment by Björn Dahlgren (Bitbucket: bjodah, GitHub: bjodah)


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

jonkiparsky commented 1 year ago

@nedbat I see that you reviewed this relatively recently, so I assume this is still a live issue.

If I were considering whether to take this issue on, the hard part would be understanding which of the suggested changes is desired. If that were made clear I would hope this would be a pretty accessible task.