阻止 python 覆盖范围包括虚拟环境站点包

preventing python coverage from including virtual environment site packages

我是新手,运行遇到了一个 st运行ge 问题。我的报道考虑了我的虚拟环境站点包。 这是覆盖率 运行:

的输出
coverage run test.py
....................
----------------------------------------------------------------------
Ran 20 tests in 0.060s

OK
(atcatalog)- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -jmfrank63@fullstack-audio-text-catalog:~/workspace (git master)       [19:58:45]
$ coverage report
Name                                                                              Stmts   Miss  Cover
-----------------------------------------------------------------------------------------------------
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/__init__               18      0   100%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/_compat                38     20    47%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/app                   528    255    52%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/blueprints            156    118    24%
                             .
                             .
                             .
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/urls               412    215    48%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/utils              242    175    28%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/wrappers           568    298    48%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/wsgi               448    352    21%
atcatalog/__init__                                                                    7      0   100%
atcatalog/views/__init__                                                              0      0   100%
atcatalog/views/publang                                                               7      0   100%
atcatalog/views/pubtext                                                               1      0   100%
atcatalog/views/userlang                                                             13      0   100%
atcatalog/views/users                                                                 5      0   100%
atcatalog/views/usertext                                                             14      0   100%
test                                                                                120      0   100%
-----------------------------------------------------------------------------------------------------
TOTAL                                                                             12530   8044    36%
(atcatalog)- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -jmfrank63@fullstack-audio-text-catalog:~/workspace (git master)       [19:58:55]

这是我位于 home 下的项目目录的结构:

workspace/
├── README.md
├── atcatalog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── static
│   ├── templates
│   └── views
│       ├── __init__.py
│       ├── __init__.pyc
│       ├── publang.py
│       ├── publang.pyc
│       ├── pubtext.py
│       ├── pubtext.pyc
│       ├── userlang.py
│       ├── userlang.pyc
│       ├── users.py
│       ├── users.pyc
│       ├── usertext.py
│       └── usertext.pyc
├── requirements.txt
├── run.py
└── test.py

我最初在项目目录中有虚拟环境,现在使用 virtualenvwrapper 将其移至 ~/Envs,但问题仍然存在。 run.py 和 test.py 没有任何特殊之处,它们都是从 atcatalog 导入应用程序。 我也想办法省略虚拟环境目录,但是 google 没有给出答案(令人惊讶)。 我不认为覆盖范围的目的是测试已经过良好测试的站点包。所以我会将它们排除在 运行.

之外

我怎样才能避免覆盖测试我的网站包?

尝试使用 py.test,然后在 setup.cfg 文件中指定您的测试选项。您需要先 pip install pytest。

例如:

[pytest]
norecursedirs = build docs/_build *.egg .tox *.venv
python_files = tests/functional* tests/integration*
addopts =
    #--verbose
    --tb short
    # Turn on --capture to have brief, less noisy output
    # You will only see output if the test fails
    # Use --capture no if you want to see it all or have problems debugging
    --capture fd
    # --capture no
    # show extra test summary info as specified by chars (f)ailed, (E)error,      (s)skipped, (x)failed, (X)passed.
    - rfEsxX
    --junitxml junit.xml
    --cov workspace --cov-report xml --cov-report term-missing

您可以在此处阅读有关配置 py.test 的更多信息:https://pytest.org/latest/customize.html

感谢 tknickman 我想通了:使用其中之一

coverage run --source <path to project dir> test.py

或者创建一个配置文件 .coveragerc,它位于您 运行 覆盖的目录中,内容如下:

[run]
source =
    <path to project dir>

这前提是你没有在项目目录下安装你的虚拟环境。 如果你在项目目录下安装了虚拟环境,你可以使用

coverage run --source <project path> --omit <pattern> test.py

注意 omit 想要一个像

这样的文件模式
~/projectdir/venv/*

而不是路径。

对应的 .coveragerc 看起来像这样:

[run]
source=
    <path to project dir>
omit=
    <path to project dir>/<name of virtual env>/*

我仍然认为像标准库的包一样,任何安装在 site-packages 下的包都不应该被默认覆盖。

如果使用pytest,可以在setup.cfg(see docs)中指定独占路径或文件进行测试:

[pytest]
# a directory
testpaths = tests

# exact file(s)
python_files = tests/test1.py tests/test2.py

看起来如果您包含 python_filestestpaths 参数,那么将只使用 python_files

在您的 setup.cfg 文件中包括:

[coverage:run]
omit=*/site-packages/*,*/tests/*,*/.eggs/*

或显示在您的结果中但您想隐藏的任何其他文件夹。