9

Pytest cov is not reading its setting from the pyproject.toml file. I am using nox, so I run the test with:

python3 -m nox

It seems I have the same issue even without nox.

In fact, after running a poetry install:

  • poetry run pytest --cov=src passes the test
  • poetry run pytest --cov does not pass the test

In particular, when failing the test I have the following output (output is cut to the most important stuff):

WARNING: Failed to generate report: No data to report.

/Users/matteo/Library/Caches/pypoetry/virtualenvs/project-Nz69kfmJ-py3.7/lib/python3.7/site-packages/pytest_cov/plugin.py:271: PytestWarning: Failed to generate report: No data to report.

  self.cov_controller.finish()


---------- coverage: platform darwin, python 3.7.7-final-0 -----------

FAIL Required test coverage of 100.0% not reached. Total coverage: 0.00%

Code with a reproducible error here. To run it you'll need to install poetry and to install nox.

user1315621
  • 3,044
  • 9
  • 42
  • 86
  • What exactly doesn't pass the test? You have added the same command twice. – hoefling Jul 10 '20 at 14:12
  • I'm so sorry. Fix it above – user1315621 Jul 10 '20 at 15:51
  • 1
    It looks to me like you're using the `src` layout wrong. The meaning of the `src` dir is to avoid accidental imports of the code from repository, ensuring the package is always installed. `poetry` supports `src` layout; in your case, you'd have to add `packages = [{ include = 'project', from = 'src' }]` to the `tool.poetry` section and `source = ['project']` to `tool.coverage.run`. – hoefling Jul 10 '20 at 16:12
  • 1
    Or you can drop the `src` dir completely if you don't need the `src` layout. Anyway, `from src.project import code` should be fixed. Your root package is `project`, not `src`. – hoefling Jul 10 '20 at 16:14
  • That's an important lesson, thank you. I see that just changing the import solves the problem. If you want to write it as an answer I will accept it, thanks! – user1315621 Jul 10 '20 at 17:21

1 Answers1

7

Turning the comment into an answer:

Check the current treatment of the src directory. Right now, it seems to be a namespace package which is not what you intend. Either switch to the src layout:

# pyproject.toml

[tool.poetry]
...
packages = [
    { include = 'project', from = 'src' }
]

[tool.coverage.run]
...
source = ['project']

and fix the import in test_code.py:

from src.project import code

to

from project import code

or remove the src dir:

rootdir
├── project
│   └── __init__.py
└── tests
    └── test_code.py

and fix the import in test_code.py.

hoefling
  • 59,418
  • 12
  • 147
  • 194