python - pytest-cov无法使用新的Coverage版本5.X生成报告,但适用于Coverage版本4.4.2

标签 python pytest tox coverage.py pytest-cov

我有以下项目tox.ini

[tox]
envlist = unittest,functional,lint
skipsdist = True
sitepackages = False

[testenv]
setenv =
    TESTING = True
deps=
    -rrequirements.txt
    -rtox-requirements.txt
install_command = pip install {opts} {packages}

passenv=TOKEN BASE_URL

basepython=python3.6

[testenv:unittest]
commands=
    flake8 app
    flake8 tests
    py.test -v -n auto -l --cov=app --cov-report term-missing --no-cov-on-fail --max-slave-restart=0 tests/unit/
    coverage-badge -o coverage.svg -f

[testenv:functional]
commands=
    py.test -v -n auto -l --cov=app --cov-report term-missing --no-cov-on-fail --max-slave-restart=0 tests/functional/
    coverage-badge -o coverage.svg -f

[testenv:lint]
commands=pylint app

[flake8]
exclude =
    .tox,
    .git,
    __pycache__,
    *.pyc,
    *.egg-info,
    .cache,
    .eggs
max-line-length = 80


默认情况下,我的tox-requirements.txt中没有版本控制,在安装时,它总是安装最新的软件包。但是它在coverage-badge -o coverage.svg -f命令上失败。

$ tox -v -r -e functional
using tox.ini: /var/lib/jenkins/workspace/cicd-myprg-func-test/tox.ini (pid 15546)
using tox-3.14.0 from /var/lib/jenkins/.pyenv/versions/3.7.3/lib/python3.7/site-packages/tox/__init__.py (pid 15546)
functional cannot reuse: -r flag
functional recreate: /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional
[15605] /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox$ /var/lib/jenkins/.pyenv/versions/3.7.3/bin/python3.7 -m virtualenv --no-download --python /bin/python3.6 functional >functional/log/functional-0.log
functional installdeps: -rrequirements.txt, -rtox-requirements.txt
[15733] /var/lib/jenkins/workspace/cicd-myprg-func-test$ /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/bin/pip install --extra-index-url https://pypi.storage.rackspace.com --trusted-host pypi.storage.rackspace.com -rrequirements.txt -rtox-requirements.txt >.tox/functional/log/functional-1.log
write config to /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/.tox-config1 as '66a61f41299ac9c1daf8e4a695b217df37c78ff9bb18610d56471b4e33d5e65f /bin/python3.6\n3.14.0 0 0 0\n00000000000000000000000000000000 -rrequirements.txt\n00000000000000000000000000000000 -rtox-requirements.txt'
[16449] /var/lib/jenkins/workspace/cicd-myprg-func-test$ /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/bin/python -m pip freeze >.tox/functional/log/functional-2.log
functional installed: amqp==2.5.2,apipkg==1.5,astroid==2.3.3,atomicwrites==1.3.0,attrs==19.3.0,bcrypt==3.1.7,beautifulsoup4==4.7.1,billiard==3.6.1.0,bleach==3.1.0,blinker==1.4,celery==4.4.0,certifi==2019.11.28,cffi==1.13.2,chardet==3.0.4,Click==7.0,coverage==5.0.1,coverage-badge==1.0.1,cryptography==2.8,==3.0.60,decorator==4.4.1,docker==4.1.0,entrypoints==0.3,execnet==1.7.1,flake8==3.7.9,Flask==1.1.1,Flask-APIBlueprint==1.0.0,Flask-Limiter==1.0.1,Flask-SQLAlchemy==2.3.2,flask-swagger==0.2.13,glob2==0.7,html5lib==1.0.1,idna==2.8,importlib-metadata==1.3.0,isort==4.3.21,itsdangerous==1.1.0,Jinja2==2.10.3,kombu==4.6.7,lazy-object-proxy==1.4.3,limits==1.4.1,Mako==1.1.0,MarkupSafe==1.1.1,marshmallow==3.0.0b2,mccabe==0.6.1,more-itertools==8.0.2,mysqlclient==1.4.1,paramiko==2.4.1,parse==1.14.0,parse-type==0.5.2,pbr==5.4.4,pluggy==0.13.1,py==1.8.1,pyasn1==0.4.8,pycodestyle==2.5.0,pycparser==2.19,pyflakes==2.1.1,pylint==2.4.4,pymssql==2.1.4,PyNaCl==1.3.0,pytest==4.4.0,pytest-bdd==3.2.1,pytest-cov==2.6.1,pytest-flask==0.15.0,pytest-forked==1.1.3,pytest-mock==1.13.0,pytest-xdist==1.31.0,python-dateutil==2.8.0,pytz==2017.2,PyYAML==5.2,requests==2.21.0,requests-toolbelt==0.9.1,retry==0.9.2,six==1.13.0,soupsieve==1.9.5,SQLAlchemy==1.2.18,sshtunnel==0.1.5,typed-ast==1.4.0,urllib3==1.24.3,uWSGI==2.0.18,vine==1.3.0,webencodings==0.5.1,websocket-client==0.57.0,Werkzeug==0.16.0,wrapt==1.11.2,zipp==0.6.0
functional run-test-pre: PYTHONHASHSEED='1444619988'
functional run-test: commands[0] | py.test -v -n auto -l --cov=app --cov-report term-missing --no-cov-on-fail --max-slave-restart=0 tests/functional/
[16450] /var/lib/jenkins/workspace/cicd-myprg-func-test$ /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/bin/py.test -v -n auto -l --cov=app --cov-report term-missing --no-cov-on-fail --max-slave-restart=0 tests/functional/
================================================= test session starts =================================================
platform linux -- Python 3.6.8, pytest-4.4.0, py-1.8.1, pluggy-0.13.1 -- /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/bin/python3.6
cachedir: .tox/functional/.pytest_cache
rootdir: /var/lib/jenkins/workspace/cicd-myprg-func-test
plugins: flask-0.15.0, forked-1.1.3, celery-4.4.0, bdd-3.2.1, xdist-1.31.0, cov-2.6.1, mock-1.13.0
[gw0] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprg-func-test
[gw1] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprg-func-test
[gw2] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprg-func-test
[gw3] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprg-func-test
[gw1] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw2] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw3] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw0] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
gw0 [1] / gw1 [1] / gw2 [1] / gw3 [1]
scheduling tests via LoadScheduling
Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)

tests/functional/views/v2/test_myapi.py::test_myapi[/v2/myapi-200]
[gw1] [100%] PASSED tests/functional/views/v2/test_myapi.py::test_myapi[/v2/myapi-200] Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)
WARNING: Failed to generate report: No data to report.

/var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/lib/python3.6/site-packages/pytest_cov/plugin.py:229: PytestWarning: Failed to generate report: No data to report.

  self.cov_controller.finish()


================================================== warnings summary ===================================================
.tox/functional/lib/python3.6/site-packages/urllib3/connectionpool.py:847
  /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/lib/python3.6/site-packages/urllib3/connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
    InsecureRequestWarning)

-- Docs: https://docs.pytest.org/en/latest/warnings.html

----------- coverage: platform linux, python 3.6.8-final-0 -----------

======================================== 1 passed, 1 warnings in 3.47 seconds =========================================
functional run-test: commands[1] | coverage-badge -o coverage.svg -f
[16491] /var/lib/jenkins/workspace/cicd-myprg-func-test$ /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/bin/coverage-badge -o coverage.svg -f
Error: No data to report. Did you run coverage first?
ERROR: InvocationError for command /var/lib/jenkins/workspace/cicd-myprg-func-test/.tox/functional/bin/coverage-badge -o coverage.svg -f (exited with code 1)
_______________________________________________________ summary _______________________________________________________
ERROR:   functional: commands failed


如果我尝试手动运行Coverage,它将起作用并生成报告

$ .tox/functional/bin/coverage run -m pytest -v -n auto -l --cov=app --cov-report term-missing --no-cov-on-fail --max-slave-restart=0 tests/functional/
================================================= test session starts =================================================
platform linux -- Python 3.6.8, pytest-4.4.0, py-1.8.1, pluggy-0.13.1 -- /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/python3.6
cachedir: .pytest_cache
rootdir: /var/lib/jenkins/workspace/cicd-myprj-func-test
plugins: flask-0.15.0, forked-1.1.3, celery-4.4.0, bdd-3.2.1, xdist-1.31.0, cov-2.6.1, mock-1.13.0
[gw0] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw1] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw2] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw3] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw0] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw1] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw3] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw2] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
gw0 [1] / gw1 [1] / gw2 [1] / gw3 [1]
scheduling tests via LoadScheduling
Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)

tests/functional/views/v2/test_myapi.py::test_myapi[/v2/myapi-200]
[gw0] [100%] PASSED tests/functional/views/v2/test_myapi.py::test_myapi[/v2/myapi-200] Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)
WARNING: Failed to generate report: No data to report.

/var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/lib/python3.6/site-packages/pytest_cov/plugin.py:239: PytestWarning: Failed to generate report: No data to report.

  warnings.warn(pytest.PytestWarning(message))


================================================== warnings summary ===================================================
.tox/functional/lib/python3.6/site-packages/urllib3/connectionpool.py:847
  /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/lib/python3.6/site-packages/urllib3/connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
    InsecureRequestWarning)

-- Docs: https://docs.pytest.org/en/latest/warnings.html

----------- coverage: platform linux, python 3.6.8-final-0 -----------

======================================== 1 passed, 1 warnings in 2.67 seconds =========================================

$ .tox/functional/bin/coverage report
Name                                                                                          Stmts   Miss Branch BrPart  Cover
-------------------------------------------------------------------------------------------------------------------------------
....
....
....
-------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                         64057  43938  22950    956    26%


当我检查coverage版本,即coverage==5.0.1时,如果我安装了旧版本4.4.2,则它也可以与tox命令一起使用。

$ .tox/functional/bin/pip uninstall coverage
Uninstalling coverage-5.0.1:
  Would remove:
    /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/coverage
    /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/coverage-3.6
    /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/coverage3
    /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/lib/python3.6/site-packages/coverage-5.0.1.dist-info/*
    /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/lib/python3.6/site-packages/coverage/*
Proceed (y/n)? y
  Successfully uninstalled coverage-5.0.1

$ .tox/functional/bin/pip install coverage==4.4.2
  Collecting coverage==4.4.2
    Using cached https://files.pythonhosted.org/packages/fd/57/595ec48699ac4cbd51fd25c546859fe5b65cff0a4a9747b38e665643e062/coverage-4.4.2-cp36-cp36m-manylinux1_x86_64.whl
  Installing collected packages: coverage
  Successfully installed coverage-4.4.2
  WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
  You should consider upgrading via the 'pip install --upgrade pip' command.


之后,我尝试了tox

$ tox -v -e functional
using tox.ini: /var/lib/jenkins/workspace/cicd-myprj-func-test/tox.ini (pid 38913)
using tox-3.14.0 from /var/lib/jenkins/.pyenv/versions/3.7.3/lib/python3.7/site-packages/tox/__init__.py (pid 38913)
functional reusing: /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional
[38920] /var/lib/jenkins/workspace/cicd-myprj-func-test$ /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/python -m pip freeze >.tox/functional/log/functional-3.log
functional installed: amqp==2.5.2,apipkg==1.5,astroid==2.3.3,atomicwrites==1.3.0,attrs==19.3.0,bcrypt==3.1.7,beautifulsoup4==4.7.1,billiard==3.6.1.0,bleach==3.1.0,blinker==1.4,celery==4.4.0,certifi==2019.11.28,cffi==1.13.2,chardet==3.0.4,Click==7.0,coverage==4.4.2,coverage-badge==1.0.1,cryptography==2.8,CvLib==3.0.60,decorator==4.4.1,docker==4.1.0,entrypoints==0.3,execnet==1.7.1,flake8==3.7.9,Flask==1.1.1,Flask-APIBlueprint==1.0.0,Flask-Limiter==1.0.1,Flask-SQLAlchemy==2.3.2,flask-swagger==0.2.13,glob2==0.7,html5lib==1.0.1,idna==2.8,importlib-metadata==1.3.0,isort==4.3.21,itsdangerous==1.1.0,Jinja2==2.10.3,kombu==4.6.7,lazy-object-proxy==1.4.3,limits==1.4.1,Mako==1.1.0,MarkupSafe==1.1.1,marshmallow==3.0.0b2,mccabe==0.6.1,more-itertools==8.0.2,mysqlclient==1.4.1,paramiko==2.4.1,parse==1.14.0,parse-type==0.5.2,pbr==5.4.4,pluggy==0.13.1,py==1.8.1,pyasn1==0.4.8,pycodestyle==2.5.0,pycparser==2.19,pyflakes==2.1.1,pylint==2.4.4,pymssql==2.1.4,PyNaCl==1.3.0,pytest==4.4.0,pytest-bdd==3.2.1,pytest-cov==2.6.1,pytest-flask==0.15.0,pytest-forked==1.1.3,pytest-mock==1.13.0,pytest-xdist==1.31.0,python-dateutil==2.8.0,pytz==2017.2,PyYAML==5.2,requests==2.21.0,requests-toolbelt==0.9.1,retry==0.9.2,six==1.13.0,soupsieve==1.9.5,SQLAlchemy==1.2.18,sshtunnel==0.1.5,typed-ast==1.4.0,urllib3==1.24.3,uWSGI==2.0.18,vine==1.3.0,webencodings==0.5.1,websocket-client==0.57.0,Werkzeug==0.16.0,wrapt==1.11.2,zipp==0.6.0
functional run-test-pre: PYTHONHASHSEED='4155838352'
functional run-test: commands[0] | py.test -v -n auto -l --cov=app --cov-report term-missing --no-cov-on-fail --max-slave-restart=0 tests/functional/
[38922] /var/lib/jenkins/workspace/cicd-myprj-func-test$ /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/py.test -v -n auto -l --cov=app --cov-report term-missing --no-cov-on-fail --max-slave-restart=0 tests/functional/
================================================= test session starts =================================================
platform linux -- Python 3.6.8, pytest-4.4.0, py-1.8.1, pluggy-0.13.1 -- /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/python3.6
cachedir: .tox/functional/.pytest_cache
rootdir: /var/lib/jenkins/workspace/cicd-myprj-func-test
plugins: flask-0.15.0, forked-1.1.3, celery-4.4.0, bdd-3.2.1, xdist-1.31.0, cov-2.6.1, mock-1.13.0
[gw0] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw1] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw2] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw3] linux Python 3.6.8 cwd: /var/lib/jenkins/workspace/cicd-myprj-func-test
[gw0] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw1] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw3] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[gw2] Python 3.6.8 (default, Aug  7 2019, 17:28:10)  -- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
gw0 [1] / gw1 [1] / gw2 [1] / gw3 [1]
scheduling tests via LoadScheduling
Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)

tests/functional/views/v2/test_commcells.py::test_commcells[/v2/commcells-200]
[gw0] [100%] PASSED tests/functional/views/v2/test_commcells.py::test_commcells[/v2/commcells-200] Coverage.py warning: No data was collected. (no-data-collected)
Coverage.py warning: No data was collected. (no-data-collected)


================================================== warnings summary ===================================================
.tox/functional/lib/python3.6/site-packages/urllib3/connectionpool.py:847
  /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/lib/python3.6/site-packages/urllib3/connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
    InsecureRequestWarning)

-- Docs: https://docs.pytest.org/en/latest/warnings.html

----------- coverage: platform linux, python 3.6.8-final-0 -----------
Name                                      Stmts   Miss Branch BrPart  Cover   Missing
-------------------------------------------------------------------------------------
....
....
....
....
-------------------------------------------------------------------------------------
TOTAL                                      9520   9520   2915      0     0%

======================================== 1 passed, 1 warnings in 5.73 seconds =========================================
functional run-test: commands[1] | coverage-badge -o coverage.svg -f
[38964] /var/lib/jenkins/workspace/cicd-myprj-func-test$ /var/lib/jenkins/workspace/cicd-myprj-func-test/.tox/functional/bin/coverage-badge -o coverage.svg -f
Saved badge to /var/lib/jenkins/workspace/cicd-myprj-func-test/coverage.svg
_______________________________________________________ summary _______________________________________________________
  functional: commands succeeded
  congratulations :)


我们是否必须添加任何新标志以使其与pytest-cov一起使用?

最佳答案

安装最新版本Coverage==5.0.2可以解决此问题。

关于python - pytest-cov无法使用新的Coverage版本5.X生成报告,但适用于Coverage版本4.4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59586354/

相关文章:

python - Django 中的 HttpResponseRedirect 没有重定向到页面views.py 文件

python - python 对给定键集的 csv 进行排序

python - 将 py.test 的输出作为对象读取

python - Jira POST 和 PUT Rest 调用从 Python 返回错误 400

python - 自定义 pytest junitxml 失败报告

python - 无法使用 Pytest 断言列表的长度

python - mypy 在 tox 中找不到模块

python - 如何告诉 tox 使用 PyPI 镜像来安装包?

python - 用于测试包的基本毒理学设置

python - 在Python中从Excel工作表中查找值