63

我是新手,遇到了一个奇怪的问题。我的报道将我的虚拟环境站点包考虑在内。这是覆盖运行的输出:

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]

这是我的项目目录的结构,它位于主目录下:

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 导入应用程序。我也试图找到省略虚拟环境目录的方法,但谷歌没有给出答案(令人惊讶)。我不认为覆盖的目的是测试已经经过良好测试的站点包。所以我会把他们排除在跑步之外。

我如何才能避免覆盖测试我的站点包?

4

4 回答 4

79

感谢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

请注意,省略需要一个文件模式,如

~/projectdir/venv/*

而不是路径。

相应的 .coveragerc 如下所示:

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

我仍然认为,与标准库的软件包一样,默认情况下不应涵盖安装在 site-packages 下的任何软件包。

于 2015-08-23T12:01:34.490 回答
7

尝试使用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

于 2015-08-22T20:44:01.573 回答
3

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

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

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

于 2021-07-07T16:23:44.557 回答
1

如果使用 pytest,您可以指定要测试的专有路径或文件setup.cfg请参阅文档):

[pytest]
# a directory
testpaths = tests

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

看起来如果你包含python_filesandtestpaths参数,那么python_files只会使用 。

于 2016-06-29T05:42:07.977 回答