如何从coverage.py中排除文件?

2024-05-13 11:26:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用nosetestcoverage.py插件。是否可以从覆盖率报告中排除整个文件或文件夹?我的用例在我的项目文件夹中有一个显然不在我的测试套件中的外部库。


Tags: 文件项目py文件夹插件套件报告coverage
1条回答
网友
1楼 · 发布于 2024-05-13 11:26:58

是的,他们在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.

相关问题 更多 >