2

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

3 Answers 3

5

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 :)

0

As per the Coverage.py documentation:

Only importable files (ones at the root of the tree, or in directories with a __init__.py file) will be considered. Source

So you either have to make sure that the code is executed by your tests OR ensure that the directories where your code resides have __init__.py.

0

I found the problem too,and i changed the tox.ini like below and it works!

[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

--cov=. means the entire project. Remember to put tox.ini in the root directory of the project.

I don't know how did you run the Django app and the pytest command, here is mine

coverage run manage.py runserver --noreload 0.0.0.0:8000 --settings=djangoproject.settings.local &

pytest
 
pgrep -f 9000 | xargs kill -2

coverage report

coverage html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.