Why are most of my project's Django files missing from the PyTest Coverage report?

霸气de小男生 提交于 2020-01-16 16:47:40

问题


I'm running pytest-cov and pytest-django using tox. I have a very simple tox.ini file with limited omit files. The problem is when I run pytest using tox -e unit, I get a limited Coverage report:

---------- 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%

All of my Django apps under components have many files that should be covered in the report, including apps, serializers, signals, urls, and views (the standard Django structure). Anyone have any idea what I'm missing? Everything I've got in the tox.ini seems to follow pretty much exactly what I've read in the various documentation for pytest, pytest-django, pytest-cov, and coverage, but I must be missing something important!

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/*

回答1:


The answer seems quite trivial and obvious, but amazingly, it was quite difficult to come by. coverage will only report on code that is actually run. So if your tests don't call a bit of code and it doesn't get run during normal loading of the application, coverage will not show a report for that code. Code that doesn't run does not cause bugs :)



来源:https://stackoverflow.com/questions/58477931/why-are-most-of-my-projects-django-files-missing-from-the-pytest-coverage-repor

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