coveragepy
coveragepy copied to clipboard
Get coverage as percentage
Originally reported by controversial (Bitbucket: controversial, GitHub: controversial)
I'm using the command-line API to measure coverage as part of my testing script. I'd like, however, to be able to get the coverage over all files as a single number from my Coverage
object.
I spent a while poking through some objects and the source. I found the coverage.results.Numbers
class, but after measuring my coverage with the Python API, I can't find a way to get an instance of this class.
- Bitbucket: https://bitbucket.org/ned/coveragepy/issue/503
@spaceone .start and .stop happen during the measurement phase. Computing the percentage is the reporting phase, which involves analyzing code, a slow operation. We'll need to continue to keep them separate.
Original comment by space one (Bitbucket: spaceone, GitHub: spaceone)
coverage.Coverage.stop() could return that result object.
Original comment by space one (Bitbucket: spaceone, GitHub: spaceone)
Nice would be some "Result" object (so that it could be adapted in the future). which has the percentage as a property. The percentage is 100% even if there are branches which were only executed one way? So maybe another property like uncovered_branches (int) would also be nice!
Mulling over how to implement this, it seems like this will be the only result you want? That is, you won't be writing an HTML report and then also using get_percentage(), since cov.html_report() already returns the total coverage number.
Original comment by space one (Bitbucket: spaceone, GitHub: spaceone)
I am also interested in this. I want to do something like: cov.stop() assert cov.get_percentage() == 100
@nedbat I don't know if this is a thing you actually want to include, but adding an option to run coverage percentage
(or some similar subcommand) might be a sprintable goal.
Now there is a command-line way to get the total percentage: coverage report --format=total
(new in 7.0). This request is about an API way to get the total, which I think we still don't have a simple way to do.