Pytest cov does not read pyproject.toml
Asked Answered
P

1

12

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.

Purington answered 9/7, 2020 at 22:39 Comment(5)
What exactly doesn't pass the test? You have added the same command twice.Fieldpiece
I'm so sorry. Fix it abovePurington
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.Fieldpiece
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.Fieldpiece
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!Purington
F
8

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.

Fieldpiece answered 10/7, 2020 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.