[Django]-Coverage.py warning: No data was collected. (no-data-collected)

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/

πŸ‘€louis_guitton

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
πŸ‘€Nadav

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

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.

πŸ‘€Robert Yi

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
πŸ‘€Ciel Li

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.

πŸ‘€modle13

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.

1πŸ‘

The solution that worked for me: remove "execute" permission from all the *.py files in the scope of the run.

πŸ‘€nivpeled

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.

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

πŸ‘€fulo

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__.:
πŸ‘€ryanjdillon

Leave a comment