防止python覆盖包括虚拟环境站点包



我刚接触保险,遇到了一个奇怪的问题。我的报道将我的虚拟环境站点包考虑在内。下面是覆盖运行的输出:

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中导入app。我还试图找到省略虚拟环境目录的方法,但google没有给出答案(令人惊讶)。我不认为覆盖的目的是测试已经测试好的站点包。所以我将把它们从运行中排除。

我怎样才能在测试我的站点包时避免覆盖?

多亏了tknickman,我明白了:使用

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

或者创建一个配置文件.coveragerc,它驻留在运行coverage的目录中,包含以下内容:

[run]
source =
    <path to project dir>

表示您没有将虚拟环境安装在项目目录下。如果虚拟环境安装在项目目录下,可以使用

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

注意,省略需要一个像

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

而不是路径

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

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

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

尝试使用py。测试,然后在setup.cfg文件中指定测试选项。首先需要pip安装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的信息。测试在这里:https://pytest.org/latest/customize.html

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

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

如果使用pytest,您可以指定setup.cfg中要测试的独占路径或文件(参见文档):

[pytest]
# a directory
testpaths = tests
# exact file(s)
python_files = tests/test1.py tests/test2.py

看起来如果你包括python_filestestpaths参数,那么python_files将只被使用。

最新更新