I use nosetest
's coverage.py
plugin. Is it somehow possible to exclude entire files or folders from the coverage report? My use case is having an external library in my project folder that obviously isn't covered by my test suite.
Another solution is to put the file or folder that you want to exclude in a directory that is not a package (i.e. does not have an __init__
file in it). Coverage will not cover it then.
The docs say:
Only importable files (ones at the root of the tree, or in directories with a
__init__.py
file) will be considered.
If you want to exclude a block of code, use a # pragma: no cover
comment.
Some examples:
def foo(self, param): # pragma: no cover <--
""" Exclude an entire function """
# None of this will be included in coverage
def bar(self, param):
""" Exclude a branch of code """
if param:
# This part is included in code coverage
do_this()
else: # pragma: no cover <--
# Not included in coverage
other_thing()
More in the docs.
Yeah, they have pretty extensive support for this in the docs.
When running your code, the coverage run command will by default measure all code, unless it is part of the Python standard library.
You can specify source to measure with the --source command-line switch, or the [run] source configuration value. The value is a list of directories or package names. If specified, only source inside these directories or packages will be measured. Specifying the source option also enables coverage.py to report on unexecuted files, since it can search the source tree for files that haven’t been measured at all. Only importable files (ones at the root of the tree, or in directories with a _init_.py file) will be considered, and files with unusual punctuation in their names will be skipped (they are assumed to be scratch files written by text editors).
You can further fine-tune coverage.py’s attention with the --include and --omit switches (or [run] include and [run] omit configuration values). --include is a list of file name patterns. If specified, only files matching those patterns will be measured. --omit is also a list of file name patterns, specifying files not to measure. If both include and omit are specified, first the set of files is reduced to only those that match the include patterns, then any files that match the omit pattern are removed from the set.
The include and omit file name patterns follow typical shell syntax: * matches any number of characters and ? matches a single character. Patterns that start with a wildcard character are used as-is, other patterns are interpreted relative to the current directory.
The source, include, and omit values all work together to determine the source that will be measured.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With