pytest-cov icon indicating copy to clipboard operation
pytest-cov copied to clipboard

--cov don't include recursively folders and files

Open kekel87 opened this issue 4 years ago • 9 comments

Summary

Expected vs actual result

I want to integrate all the python files in from my src folder only, even the untested ones.

I managed to get the expected result by indicating all the folders of my project (see Config), but it is not very practical ...

I also tried with --cov or --cov . but I get the same result.

Actual

Name                              Stmts   Miss  Cover
-----------------------------------------------------
src\app.py                           10     10     0%
src\core\settings.py                 27      1    96%
src\core\sheets_utils.py             67     26    61%
src\core\stackdriver_logging.py      25     13    48%
src\core\utils.py                    24      7    71%
-----------------------------------------------------
TOTAL                               153     57    63%
Coverage HTML written to dir reports/htmlcov
Coverage XML written to file reports/coverage.xml

Expected

Name                                                          Stmts   Miss  Cover
---------------------------------------------------------------------------------
... lot of files
---------------------------------------------------------------------------------
TOTAL                                                          3414   3318     3%
Coverage HTML written to dir reports/htmlcov
Coverage XML written to file reports/coverage.xml

Reproducer

Command

python -m pytest -v

Versions

Python 3.7.9 pytest 6.2.5

Config

Expected

[tool:pytest]
norecursedirs=tests/mocks
testpaths=tests/
addopts =
    --cov src
    --cov-report term
    --cov-report html:reports/htmlcov
    --cov-report xml:reports/coverage.xml
    --junit-xml=reports/junit.xml
# https://github.com/googleapis/python-api-common-protos/issues/23#issuecomment-756495529 
filterwarnings = 
    ignore:Call to deprecated create function FieldDescriptor
    ignore:Call to deprecated create function Descriptor
    ignore:Call to deprecated create function EnumDescriptor
    ignore:Call to deprecated create function EnumValueDescriptor
    ignore:Call to deprecated create function FileDescriptor
    ignore:Call to deprecated create function OneofDescriptor
    ignore:Call to deprecated create function MethodDescriptor
    ignore:Call to deprecated create function ServiceDescriptor

Actual

[tool:pytest]
norecursedirs=tests/mocks
testpaths=tests/
addopts =
    --cov src
    --cov src/api
    --cov src/api/routes
    --cov src/api/dependencies
    --cov src/core
    --cov src/enums
    --cov src/schemas
    --cov src/schemas/big_query
    --cov src/schemas/datalake
    --cov src/schemas/mapping
    --cov src/schemas/object
    --cov src/schemas/referential
    --cov src/schemas/source
    --cov src/services
    --cov src/services/mapping
    --cov src/services/object
    --cov src/services/referential
    --cov src/services/source
    --cov-report term
    --cov-report html:reports/htmlcov
    --cov-report xml:reports/coverage.xml
    --junit-xml=reports/junit.xml
# https://github.com/googleapis/python-api-common-protos/issues/23#issuecomment-756495529 
filterwarnings = 
    ignore:Call to deprecated create function FieldDescriptor
    ignore:Call to deprecated create function Descriptor
    ignore:Call to deprecated create function EnumDescriptor
    ignore:Call to deprecated create function EnumValueDescriptor
    ignore:Call to deprecated create function FileDescriptor
    ignore:Call to deprecated create function OneofDescriptor
    ignore:Call to deprecated create function MethodDescriptor
    ignore:Call to deprecated create function ServiceDescriptor

kekel87 avatar Sep 30 '21 09:09 kekel87

What sort of installation method does your project have? Perhaps a reproducer in a repository would clarify this problem.

ionelmc avatar Oct 04 '21 08:10 ionelmc

Sorry I missed your message 😅

I use pipenv, FastAPI and pytest (pytest, pytest-describe, pytest-cov and pytest-mock).

My project have this kind of architecture:

📦root
 ┣ 📂src
 ┃ ┣ 📂api
 ┃ ┗ 📜app.yaml
 ┣ 📂tests
 ┃ ┣ 📂integration
 ┃ ┃ ┣ ...
 ┃ ┃ ┣📜test_app.py
 ┃ ┃ ┗📜conftest.py
 ┃ ┣ 📂mocks
 ┃ ┣ 📂unit
 ┃ ┃ ┣ 📂api
 ┃ ┃ ┃ ┣ ...
 ┃ ┃ ┃ ┗📜test_route.py
 ┃ ┃ ┗ ...
 ┃ ┗📜conftest.py
 ┣ ...
 ┣📜Pipfile
 ┗📜setup.cfg # conf as here

I launch my tests with this command: python -m pytest -v -s

kekel87 avatar Jan 17 '22 08:01 kekel87

Not sure what's going on there but you might want to copy some of the configuration here: https://github.com/pytest-dev/pytest-cov/tree/master/examples

Or post a reproducer...

ionelmc avatar Feb 02 '22 17:02 ionelmc

This is a limitation of coverage.py - as soon as it discovers a directory without an __init__.py file, it stops recursing (from your example, you do not seem to have any __init__.py files). See coverage.files.find_python_files (https://github.com/nedbat/coveragepy/blob/e1f720afb9ad31c6366f26b6fa71a5943ffa97c1/coverage/files.py#L398) for the implementation. Running this method with your directory will give you the files the collection of uncovered files will consider.

The module structure is still valid, as recent Python versions do not require you to provide (empty) __init__.py files any more for directories to be importable (implicit namespace packages/PEP 420, introduced in Python 3.3).

FriedrichFroebel avatar Mar 16 '22 20:03 FriedrichFroebel

Thanks for your answer, indeed I have no __init__.py in my project.

I found a similar issue https://github.com/nedbat/coveragepy/issues/1024 on the nedbat/coveragepy repo.

I will continue to indicate my folder manually.

kekel87 avatar Mar 31 '22 16:03 kekel87

@ionelmc I think this could be closed

beliaev-maksim avatar Jul 04 '22 10:07 beliaev-maksim

Do I understand this thread correctly that the only way to make it work is to add __init__.py files to all subfolders? There is no way to add **/*.py to any include parameter instead, right?

Alexander-Serov avatar Jul 28 '23 08:07 Alexander-Serov

There seems to be an option for this now: https://coverage.readthedocs.io/en/7.0.0/config.html#report-include-namespace-packages

FriedrichFroebel avatar Jul 29 '23 18:07 FriedrichFroebel

There seems to be an option for this now: https://coverage.readthedocs.io/en/7.0.0/config.html#report-include-namespace-packages

Amazing, this does indeed what I wanted, thanks!

Alexander-Serov avatar Jul 30 '23 09:07 Alexander-Serov