preventing python coverage from including virtual environment site packages

狂风中的少年 提交于 2019-12-03 22:08:07
Johannes Maria Frank

Thanks to tknickman I figured it out: Use either

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

or create a configuration file .coveragerc which resides in the directory you run coverage from, with the following content:

[run]
source =
    <path to project dir>

This provides you do not have your virtual environment installed under the project directory. If you have the virtual environment installed under the project dir you can use

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

Note that omit wants a file pattern like

~/projectdir/venv/*

instead of a path.

The corresponding .coveragerc would look like this:

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

I still think that like packages of the standard library any packages installed under site-packages should not be covered by default.

Try using py.test and then specifiying your test options in a setup.cfg file. You will need to pip install pytest first.

For example:

[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

You can read more about configuring py.test here: https://pytest.org/latest/customize.html

If using pytest, you can specify exclusive paths or files to test in setup.cfg (see docs):

[pytest]
# a directory
testpaths = tests

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

It looks like if you include the python_files and testpaths parameters, then the python_files will only be used.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!