毒性 0% 覆盖率 [英] Tox 0% coverage

查看:44
本文介绍了毒性 0% 覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我使用的 python 项目:

I have a python project where I use:

  • pipenv
  • 毒物
  • pytest

还有更多.

基本上,我想将 tox 添加到我的 gitlab 管道中.几乎所有东西似乎都能正常工作,从 tox 调用 mypyflake8black 工作正常.但是当我在启用覆盖率的情况下调用 tox -e py37(所以我想运行测试)时,测试运行,我可以看到它们的结果,但覆盖率是 0%(100% 仅适用于空的 __init__ 文件等),我收到警告:Coverage.py 警告:未收集数据.(不收集数据).

Basically, I want to add tox to my gitlab pipelines. And almost everything seems to work, calling mypy, flake8 or black from tox works fine. But when I call tox -e py37 (so I want to run the tests) with coverage enabled, the tests are run, I can see their results, but the coverage is 0% (100% only on empty __init__ files etc.) and I get the warning: Coverage.py warning: No data was collected. (no-data-collected).

这是我的 tox.ini 中与 pytest 相关的部分:

This is my pytest-relevant part of tox.ini:

[tox]
envlist = flake8,mypy,black,py37

[testenv]
extras = tests
commands=
    pytest --cov -ra --tb=short {posargs}

这是我的.coveragerc:

[run]
branch = True
source =
    foo

omit = 
    foo/__init__.py
    foo/__main__.py
    foo/bar/__init__.py
    foo/baz/__init__.py

[report]
exclude_lines =
    pragma: no cover
    if __name__ == .__main__.
show_missing = True

我有一个可用的 setup.py,其中包括所有需要的包:pytestpytest-cov 以及更多用于样式等的包(哪个有效).我的 tests 文件夹中也有 __init__.py.

I have a working setup.py which includes all needed packages: pytest, pytest-cov and many more for style etc. (which works). I also have __init__.py in my tests folder.

有趣的是,如果我从命令行(在 pipenv 中)调用 tox.ini 中的相同命令: pytest --cov -ra --tb=short,我得到了相同的结果,但覆盖率正常(在我的例子中是 100%).

Funny thing is, if I call the same command I have in tox.ini from my command line (in pipenv): pytest --cov -ra --tb=short, I get the same results but with normal coverage (in my case it's 100%).

有办法解决吗?我不希望我的管道显示错误的覆盖范围 + 即使它们以某种方式神奇地得到正确的覆盖,我仍然希望在本地看到它.

Is there a way to fix it? I don't want my pipelines to show wrong coverage + even if they somehow magically would actually get the right one, I still would want to see it locally.

PS 当我试图解决这个问题时,我用 tox --sitepackages -e py37 调用了 tox 并且我得到了一些错误,比如 test 命令找到但在 testenv 中没有.除了这些错误之外,我当时的报道还不错.但是后来我从我的全局 pip 寄存器中卸载了 toxpytestpytest-cov,现在有或没有 --sitepackages flag 我仍然得到 0% 覆盖率

P.S When I was trying to resolve the issue I called tox with tox --sitepackages -e py37 and I got some errors like test command found but not in testenv. And besides those errors, my coverage was fine then. But then I uninstalled tox, pytest and pytest-cov from my global pip register and now with or without --sitepackages flag I still get 0% coverage

推荐答案

根本原因pytest 在 runnung tox 期间使用了已安装的源(在站点包下),而覆盖报告者计算工作目录(即本地 git 存储库)中的命中文件.

The root-cause is that pytest during runnung tox uses the installed sources (under site-packages) while the coverage reporter count the hits files in the working directory (ie. your local git repo).

解决方案

将 tox 的虚拟环境路径添加到覆盖范围:

Add the tox's virtual-env path to coverage:

[testenv]
pytest --cov={envsitepackagesdir}/foo

有关更多详细信息,请阅读 virtualenv 的替代品-相关栏目

For more detail read substitutions for virtualenv-related sections

解决办法

PYTHONPATH 设置为工作目录(即export PYTHONPATH=.).请注意,您需要通过以下方式将 PYTHONPATH 传递给 tox:

Set PYTHONPATH to working directory (ie. export PYTHONPATH=.). Note, that you need to pass the PYTHONPATH to tox by:

[testenv]
passenv =
    PYTHONPATH

这很容易,但使用这种方式,您无需测试安装,因为测试是针对工作目录中的文件运行的.

This is easy, but using this way, you don't test the installation, because the tests runs on the files in the working directory.

这篇关于毒性 0% 覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆