38π
coverage
(used by pytest-cov
) needs the tests folder to contain an __init__.py
before it will collect any data.
I added __init__.py
to the tests folder and then coverage collected the data as expected.
Refer to http://thomas-cokelaer.info/blog/2017/01/pytest-cov-collects-no-data-on-travis/
22π
I had the same issue and the problem was with the path I was running the tests.
What is working now:
Structure
~/Projects/ProjectName
βββ manage.py
βββ tests
βββ src
β βββ app_one
βββ .coveragerc
Command:
~/Projects/ProjectName$ coverage run manage.py test
and my .coveragerc:
[run]
include = */src/*
omit = *migrations*, *tests*
plugins = django_coverage_plugin
- [Django]-How to do SELECT MAX in Django?
- [Django]-Django filter on the basis of text length
- [Django]-Favorite Django Tips & Features?
12π
The problem is that youβre not specifying which dir to get coverage from.
You can specify that in the .coveragerc
file or on the command line:
pytest tests -v --cov-report term --cov-report html:htmlcov --cov-report xml --cov-fail-under=90 --cov=<the-dir-to-colect-coverage-from>
If you desire you can only execute pytest tests
and add pytest
args on pytest.ini
at your project root:
[pytest]
addopts = -v --cov-report term --cov-report html:htmlcov --cov-report xml --cov-fail-under=<coverage-percentage-desired> --cov=<the-dir-to-colect-coverage-from>
Bonus:
If you want to omit files from the coverage you can add a .coveragerc
file on your project root:
[run]
omit =
# omit everything in the folder
proj-ab/api/confs/
# omit file
proj-ab/models/file-not-covered.py
Requirements:
On these examples Iβm using requirements: pytest==4.6.2
and pytest-cov==2.7.1
- [Django]-Proper way to handle multiple forms on one page in Django
- [Django]-Authorization Credentials Stripped β django, elastic beanstalk, oauth
- [Django]-How to annotate Count with a condition in a Django queryset
9π
I had the same issue and the above answers did not fully solve it. It turns out you need to have __init__.py
was in every subdirectory that has a test.
- [Django]-@csrf_exempt does not work on generic view based class
- [Django]-Django β Render the <label> of a single form field
- [Django]-Can we append to a {% block %} rather than overwrite?
3π
if you need to use βsourceβ in your .coveragerc
, you can write as below
[run]
source = src
omit = *migrations*, *tests*
plugins = django_coverage_plugin
- [Django]-Pass extra arguments to Serializer Class in Django Rest Framework
- [Django]-Django test app error β Got an error creating the test database: permission denied to create database
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
2π
I encountered this error with tox:
Coverage.py warning: No data was collected. (no-data-collected)
My configuration performs an install of the module and tests that rather than the source code. However, test discovery was finding a module that I had named test_*.py
in my app package, causing PYTHONPATH
confusion and resulting in failure to collect coverage details. Renaming the module to not start with test_
resolved the issue.
- [Django]-How to implement FirebaseDB with a Django Web Application
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
- [Django]-Trying to migrate in Django 1.9 β strange SQL error "django.db.utils.OperationalError: near ")": syntax error"
2π
I had the same issue because I ran pip install .
. Because the package name and the code directory had the same name and are in the current directory, coverage probably picked up the directory, while the code that was run was from site-package
(or visa versa). pip install -e .
made sure there was no confusion.
- [Django]-Is there a naming convention for Django apps
- [Django]-How to set-up a Django project with django-storages and Amazon S3, but with different folders for static files and media files?
- [Django]-Django Static files 404
1π
The solution that worked for me: remove "execute" permission from all the *.py files in the scope of the run.
- [Django]-Django rest framework, use different serializers in the same ModelViewSet
- [Django]-Django Model() vs Model.objects.create()
- [Django]-Serving Media files during deployment in django 1.8
1π
Check your setup.cfg
or other ways to implicitely pass flags to pytest
coverage run -m pytest tests
wonβt work if pytest
receives --cov-*
flags which basically tell it to generate report itself. Data will be intercepted and coverage
will be left with nothing.
Weβve got this error when we changed tooling and CI/CD scripts. So it might be worth checking if youβve recently done the same.
- [Django]-Django-allauth social account connect to existing account on login
- [Django]-How do you know if memcached is doing anything?
- [Django]-How to set up custom middleware in Django?
0π
I already had __init__.py
where necessary .
I am not sure why it worked but i simply did:
coverage run --omit='*/venv/*' manage.py test
and then:
coverage html
again and I got my intended result
- [Django]-Django Password Generator
- [Django]-Django β Circular model import issue
- [Django]-Writing a __init__ function to be used in django model
0π
I experienced this error when using a standard package layout with tox
. Turns out there is a tox bug, which necessitates the tox.ini
testenv config usedevelop = true
.
Working example
layout
.
βββ mypackage
β βββ __init__.py
β βββ module.py
βββ tests
β βββ test_module.py
βββ pyproject.toml
βββ README.md
βββ pytest.ini
βββ .coveragerc
βββ tox.ini
tox.ini
[tox]
verbose = true
min_version = 4.0.0
envlist = py38
isolated_build = true
skip_missing_interpreters = true
[testenv:py38]
extras = dev
usedevelop = true
commands =
pytest -vvv {posargs}
pytest.ini
[pytest]
testpaths =
tests
addopts =
--cov=mypackage
--cov-report=xml
--log-cli-level=ERROR
filterwarnings =
ignore::DeprecationWarning
ignore::PendingDeprecationWarning
.coveragerc
[run]
source = mypackage
omit =
venv/*
[report]
exclude_lines =
pragma: no cover
def __repr__
raise NotImplementedError
if __name__ == .__main__.:
- [Django]-Django Password Generator
- [Django]-Django: Safely Remove Old Migrations?
- [Django]-"Too many values to unpack" Exception