Ignoring empty files from coverage report
Asked Answered
M

6

30

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 the coverage report. I can't just add */__init__.py to omit as some of my __init__.py files have code.

Maldon answered 6/1, 2014 at 14:18 Comment(3)
I was about to ask this same question. I'd like to figure this out too.Kathline
I was looking for the answer to this and only found this question...Strasser
I have asked in the issue tracker for this feature: bitbucket.org/ned/coveragepy/issue/315/… A workaround could be to autogenerate a .coveragerc file, where you add all empty files to [run] omit..Beluga
C
20

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

Chapter answered 8/3, 2020 at 2:3 Comment(1)
Add it to setup.cfg for flask.Chura
C
8

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.

Convertible answered 10/6, 2014 at 9:22 Comment(2)
It would be nice if coverage.py considered empty files as covered or allowed for their exclusion.Indulge
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.Currajong
S
7

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
Shackle answered 27/8, 2021 at 9:36 Comment(0)
U
6

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

Uis answered 25/8, 2014 at 8:26 Comment(1)
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).Specialism
S
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

Snood answered 23/6, 2020 at 17:44 Comment(0)
S
-2

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.

Santossantosdumont answered 20/12, 2017 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.