25

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.

shabda
  • 1,668
  • 1
  • 18
  • 28
  • 1
    I was about to ask this same question. I'd like to figure this out too. – andy Jan 22 '14 at 03:04
  • I was looking for the answer to this and only found this question... – Tim Tisdall Mar 21 '14 at 14:19
  • 2
    I have asked in the issue tracker for this feature: https://bitbucket.org/ned/coveragepy/issue/315/option-to-omit-empty-files-eg-__init__py A workaround could be to autogenerate a `.coveragerc` file, where you add all empty files to `[run] omit`.. – blueyed Jul 10 '14 at 00:24

6 Answers6

15

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

bwl1289
  • 1,655
  • 1
  • 12
  • 10
  • Add it to [setup.cfg](https://flask.palletsprojects.com/en/master/tutorial/tests/#running-the-tests) for flask. – simanacci Aug 18 '20 at 13:16
9

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • It would be nice if coverage.py considered empty files as `covered` or allowed for their exclusion. – nu everest Nov 16 '15 at 01:07
  • 11
    From the docs: "The --skip-covered switch will leave out any file with 100% coverage." So it will skip both files with 0 or many statements. OP asked to just skip empty files, so not exactly. – heri0n Oct 24 '16 at 15:22
5

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

user3974640
  • 61
  • 1
  • 1
  • 2
    Asterisk at beginning worked for me under the `[run]` section, e.g.: `omit = */__init__.py`. I'm using pytest 5.0.1 and `cov`-plugin version 2.7.1 (i.e. pytest-cov). – colidyre Aug 29 '19 at 09:34
3

I set pyproject.toml like this:

[tool.coverage]
    [tool.coverage.run]
    omit = [
        # omit anything in a .local directory anywhere
        '*/.local/*',
        '__init__.py',
        'tests/*',
        '*/tests/*',
        # omit anything in a .venv directory anywhere
        '.venv/*'
    ]

    [tool.coverage.report]
    skip_empty = true
Waket Zheng
  • 5,065
  • 2
  • 17
  • 30
2

coverage report now supports the --skip-empty directive, which will conveniently omit those empty __init__.py files, while continuing to include any with contents!

Adapted from the docs

  • --skip-empty skip any file with no executable statements
  • --skip-covered skip any file with 100% coverage

https://coverage.readthedocs.io/en/stable/cmd.html#coverage-summary

This has been available since coverage 5.0 via GitHub PR 864

ti7
  • 16,375
  • 6
  • 40
  • 68
0

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.

153957
  • 404
  • 6
  • 18