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

PythonDjangoPytestcoverage.pyTest Coverage

Python Problem Overview


I am trying to find the coverage using coverage module for a django project but gets

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

My project folder has src and tests folders.

When I run

coverage run -m pytest && coverage report

It produces a report with 100% coverage with the list of files inside the tests folder. Whereas when I run

coverage run --source=src -m pytest && coverage report

it says

Coverage.py warning: No data was collected. (no-data-collected)
No data to report.

When I try to give the source=src or include=src in the .coveragerc also the same warning occurs. The tests passes for all the above cases.

I want the coverage of the src folder. Is it because I am missing some path setting?

Python Solutions


Solution 1 - Python

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/

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

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.

Solution 5 - Python

if you need to use 'source' in your .coveragerc, you can write as below

[run]
source = src
omit = *migrations*, *tests*
plugins = django_coverage_plugin

Solution 6 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question1010101View Question on Stackoverflow
Solution 1 - Pythonlouis_guittonView Answer on Stackoverflow
Solution 2 - PythonNadavView Answer on Stackoverflow
Solution 3 - PythonLucas Mendes Mota Da FonsecaView Answer on Stackoverflow
Solution 4 - PythonRobert YiView Answer on Stackoverflow
Solution 5 - PythonCiel LiView Answer on Stackoverflow
Solution 6 - Pythonmodle13View Answer on Stackoverflow