1

我正在运行pytest-covpytest-django使用tox. 我有一个非常简单的tox.ini文件,文件有限omit。问题是当我运行pytestusing时tox -e unit,我得到一个有限的覆盖率报告:

---------- coverage: platform darwin, python 3.7.4-final-0 -----------
Name                                           Stmts   Miss  Cover   Missing
----------------------------------------------------------------------------
components/__init__.py                             0      0   100%
components/client/__init__.py                      0      0   100%
components/client/admin.py                        27      0   100%
components/client/factories.py                    57      0   100%
components/client/models.py                       62      0   100%
components/controller/__init__.py                  0      0   100%
components/controller/admin.py                    62      6    90%   96-97, 109-110, 122-123
components/controller/management/__init__.py       0      0   100%
components/controller/models.py                  107      6    94%   19, 31, 54, 92, 105, 132
components/personal/__init__.py                    0      0   100%
components/personal/admin.py                      24      0   100%
components/personal/factories.py                  39      0   100%
components/personal/models.py                     81     16    80%   23, 36, 49, 62, 72, 75-76, 92-104
server/__init__.py                                 3      0   100%
server/celery.py                                  10      1    90%   30
server/config/__init__.py                          1      0   100%
server/settings.py                                52      0   100%
server/test.py                                    56     33    41%   16-19, 43, 48-49, 63-88, 94-105, 112-115
----------------------------------------------------------------------------
TOTAL                                            589     62    89%

我所有的 Django 应用程序components都有许多文件应该包含在报告中,包括appsserializerssignalsurlsviews(标准 Django 结构)。有人知道我缺少什么吗?我在 , , 和 的各种文档中所读到的tox.ini内容似乎与我在 、 、 和 的各种文档中所读到的完全一致pytestpytest-djangopytest-covcoverage一定遗漏了一些重要的东西!

tox.ini

[tox]
; The only reason we'd want to `sdist` is if we distributed this as a Python package in PyPI, so let's skip it:
skipsdist = True
envlist =
    {py37}-django{2}
    lint
skip_missing_interpreters = true

[testenv]
whitelist_externals = *
passenv = *
deps = -rrequirements-test.txt
commands = {[testenv:unit]commands}

[testenv:unit]
deps = {[testenv]deps}
commands = pytest {posargs:--cov=project-name}


[pytest]
DJANGO_SETTINGS_MODULE = server.settings
python_files = tests.py test_*.py *_tests.py
addopts =
    -v -s
    --color=yes
    --cov
    --cov-append
    --cov-report=term-missing
    --cov-config=tox.ini


[coverage:report]
show_missing = True
omit =
    */usr/*
    */.tox/*py
    */.virtualenvs/*
    */migrations/*
    */tests/*

[report]
omit =
    */usr/*
    */.tox/*
    */migrations/*
    */tests/*
4

2 回答 2

0

根据 Coverage.py 文档:

只会考虑可导入的文件(位于树根目录或包含文件的目录中的__init__.py文件)。资源

因此,您要么必须确保代码由您的测试执行,要么确保代码所在的目录具有__init__.py.

于 2020-11-25T08:57:43.040 回答
0

答案似乎很简单明了,但令人惊讶的是,它很难得到。coverage只会报告实际运行的代码。因此,如果您的测试没有调用一些代码并且在应用程序正常加载期间没有运行,coverage则不会显示该代码的报告。不运行的代码不会导致错误:)

于 2019-10-25T06:38:22.200 回答