65

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?

1010101
  • 843
  • 1
  • 6
  • 11
  • 3
    Perhaps you are not running code from the src folder? Add --debug=trace to the coverage run line. It will print information about each file executed, whether it is traced, and if not, why not. – Ned Batchelder Nov 15 '17 at 00:59
  • 1
    try to replace src with an actual path. It should point to a folder and not a file – Philippe C Jun 20 '18 at 17:02

10 Answers10

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
  • 5,105
  • 1
  • 31
  • 33
  • 14
    Judging by the upvotes this seems to be a solution for some people, but in my case pytest-cov works just fine with no `__init__.py` files in my `tests` folder. – Nick Crews Oct 11 '21 at 18:13
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
  • 574
  • 10
  • 24
11

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

  • I find this comment useful on my end, as I am searching for ways to resolve my pytest-cov usage on my unit tests. Thanks for this! – Lëmön Jul 30 '19 at 07:00
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
  • 1,553
  • 1
  • 14
  • 18
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
betontalpfa
  • 3,454
  • 1
  • 33
  • 65
Ciel Li
  • 31
  • 1
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
  • 1,242
  • 2
  • 16
  • 16
  • I had to unset my `source` setting under `[tool.coverage.run]` in `pyproject.toml`. Trying to set a bunch of explicit settings isn't always helpful, it seems... – NostraDavid Feb 07 '23 at 22:06
2

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

nivpeled
  • 1,810
  • 5
  • 17
1

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.

Maarten Breddels
  • 1,344
  • 10
  • 12
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
  • 95
  • 1
  • 7