Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--cov don't include recursively folders and files #499

Open
kekel87 opened this issue Sep 30, 2021 · 9 comments
Open

--cov don't include recursively folders and files #499

kekel87 opened this issue Sep 30, 2021 · 9 comments

Comments

@kekel87
Copy link

kekel87 commented Sep 30, 2021

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
@ionelmc
Copy link
Member

ionelmc commented Oct 4, 2021

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

@kekel87
Copy link
Author

kekel87 commented Jan 17, 2022

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

@ionelmc
Copy link
Member

ionelmc commented Feb 2, 2022

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

@FriedrichFroebel
Copy link

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

@kekel87
Copy link
Author

kekel87 commented Mar 31, 2022

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

I found a similar issue nedbat/coveragepy#1024 on the nedbat/coveragepy repo.

I will continue to indicate my folder manually.

@beliaev-maksim
Copy link

@ionelmc
I think this could be closed

@Alexander-Serov
Copy link

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?

@FriedrichFroebel
Copy link

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

@Alexander-Serov
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants