为什么PyTest覆盖率报告中缺少我项目的大多数Django文件? [英] Why are most of my project's Django files missing from the PyTest Coverage report?
问题描述
我正在使用tox
运行pytest-cov
和pytest-django
.我有一个非常简单的tox.ini
文件,有限的omit
文件.问题是,当我使用tox -e unit
运行pytest
时,得到的覆盖率报告有限:
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%
我在components
下的所有Django应用程序都有许多应在报告中涵盖的文件,包括apps
,serializers
,signals
,urls
和views
(标准Django结构). .有人知道我想念什么吗?我在tox.ini
中拥有的所有内容似乎都完全按照我在pytest
,pytest-django
,pytest-cov
和coverage
的各种文档中阅读的内容进行,但是我必须缺少一些重要的内容!
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/*
推荐答案
答案似乎很简单,也很明显,但是令人惊讶的是,很难找到答案. coverage
将仅报告实际运行的代码.因此,如果您的测试未调用任何代码,并且在正常加载应用程序期间未运行该代码,则coverage
将不会显示该代码的报告.不运行的代码不会导致错误:)
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 :)
这篇关于为什么PyTest覆盖率报告中缺少我项目的大多数Django文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!