Search code examples
pythonpytestcode-coveragepytest-covpyproject.toml

Pytest cov does not read pyproject.toml


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.


Solution

  • 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.