Ignoring empty files from coverage report

2020-06-08 13:27发布

问题:

coverage.py will include init.py in its report and show it as 0 lines, but with 100% coverage.

I want to exclude all blank files from coverage report. I can't just add */__init__.py to omit as some of my __init__.py files have code.

回答1:

This feature doesn't exist in coverage.py. Does it help that you can sort the HTML report to move 100% files to the bottom, or files with 0 statements to the bottom?

UPDATE: As of coverage.py 4.0, the --skip-covered option is available to do exactly what is requested.



回答2:

You can set the .coveragerc file like this:

[run]
omit = test/* \
       *\__init__.py 

or

[run]
omit = com*\__init__.py \ 
       test/*

it seems that omit do not allow pattern startwith asterisk (*)



回答3:

From the docs and docs: "New in version 5.0: The contexts and skip_empty parameters." In your tox.ini file or .coveragerc file add the following:

[coverage:report]
skip_empty = true

"If skip_empty is true, don’t report on empty files (those that have no statements)."

"skip_empty (boolean, default False): Don’t include empty files (those that have 0 statements) in the report. See Coverage summary for more information."



回答4:

To exclude all empty files, i.e. files without any statements, and thus 100% coverage you can use:

$ coverage report | grep -v " 0      0      0      0   100%"

Unfortunately, this does not exclude those files from the coverage html report and is more cumbersome than a simple option.