64

How do I exclude entire files from coverage.py reports?

According to the documentation you can exclude code by matching lines. I want to exclude entire files, so that the reports don't include 3rd party libraries. Am I missing something? Can it be done?

flybywire
  • 261,858
  • 191
  • 397
  • 503

5 Answers5

74

You can omit modules with the --omit flag. It takes a comma-separated list of path prefixes. So for example:

coverage run my_program.py
coverage report --omit=path/to/3rdparty
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
33

In addition to the options in the other answers, you can also configure the ignored files via setup.cfg:

[coverage:run]
omit =
    some/directory/*
    debug_*.py

See the documentation for details.

Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
15

Omitting some files worked for me using coverage API. Well it is the same kind what Ned suggested.

Here it is how I did it:

cov = coverage.coverage(omit='/usr/lib/python2.6/site-packages/*')

Yogesh K
  • 173
  • 1
  • 6
10

Create a new file .coveragerc and add the following lines

[run]
branch = True
omit =
    directory/*
muTheTechie
  • 1,443
  • 17
  • 25
9

With pyproject.toml

[tool.coverage.run]
omit = [
    "some/directory/*",
    "other/lib.py"
]
phi
  • 10,572
  • 3
  • 21
  • 30