coverage.py: exclude files

PythonTestingSoftware QualityCode Coveragecoverage.py

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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.

Solution 3 - Python

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/*')

Solution 4 - Python

Create a new file .coveragerc and add the following lines

[run]
branch = True
omit =
    directory/*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionflybywireView Question on Stackoverflow
Solution 1 - PythonNed BatchelderView Answer on Stackoverflow
Solution 2 - PythonFlorian BruckerView Answer on Stackoverflow
Solution 3 - PythonYogesh KView Answer on Stackoverflow
Solution 4 - PythonmuTheTechieView Answer on Stackoverflow