pytest-dev/pytest-cov

Uncovered files from dirs without __init__.py are not reported

stefaneidelloth opened this issue · 2 comments

This feature is built into coverage, but with a caveat: you have to use paths for the source.
Eg: if you use py.test --cov=somepath then you'll get all files in somepath in the report.

Originally posted by @ionelmc in #88 (comment)

If I have a file src/utils/foo.py an run

pytest --cov=src

I would expext that foo.py is reported as uncovered. However, it is not.

If I run

pytest --cov=src/utils

the file is reported as uncovered.

=> How can I tell pytest-cov to look for uncovered files recursively? Is there some flag I can set in pyproject.toml or do I need to use some regular expression for some file path property?

(Adding empty init.py files would also help to identify uncovered files. However, adding init.py files only for determining coverage seems to be weird. Especially as the need for those files vanishes once the tests are written.)

I use pytest 7.2.0, pytest-cov 4.0.0 and python 3.10.5

My pyproject.toml file contains following settings:

[tool.pytest.ini_options]
# Also see
# https://docs.pytest.org/en/7.1.x/customize.html#pyproject-toml
# https://pytest-cov.readthedocs.io/en/latest/config.html
# If you want to see console output from tests, include -s flag
addopts = [
    # !! do not include --cov option here because it would destroy PyCharm debug feature !!
    # Also see https://stackoverflow.com/questions/40718760/unable-to-debug-in-pycharm-with-pytest
    '--junitxml=report.xml',
    '--import-mode=importlib',
    '-s'  # enables logging output in console while debugging tests
]
pythonpath = ['src', 'test']
testpaths = 'test'

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

[tool.coverage.report]
# Also see
# https://coverage.readthedocs.io/en/6.4.4/config.html#config
fail_under = 90
show_missing = true
exclude_lines = ['if __name__ == .__main__.:']

[tool.coverage.html]
directory='.coverage'

New in Coverage 7.0 is the include_namespace_packages setting which I think will do what you want: https://coverage.readthedocs.io/en/7.0.0/config.html#report-include-namespace-packages

Yes, that did the trick. Thanks a lot!

[tool.coverage.report]
include_namespace_packages = true