pythonunit-testingcode-coveragenosecoverage.py

How to exclude a file from coverage.py?


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.


Solution

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