[Python] Python code coverage not working for multiprocessing inside site-packages

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple code packages in a module/folder "src" File sample.py

    import multiprocessing

    def f(x):
    return x*x

    def big_run(n):
    with multiprocessing.Pool(5) as p:
    p.map(f, list(range(n)))
    p.close()
    p.join()



    File __init__.py

    from .sample import big_run



    And I test it with a file test.py

    from src import big_run

    def test_main():
    big_run(10)



    Just copy the folder to /mnt/data/global_envs/seb_py310/lib/python3.10/site-packages I use pytest with pytest.ini

    [pytest]
    python_files = test*.py
    required_plugins = pytest-cov pytest-xdist pytest-env

    [coverage:run]
    concurrency = multiprocessing
    branch = true
    parallel = true
    sigterm = true



    And the command line python -m pytest . --cov=src --cov-branch --cov-report=term --cov-config=../../../pytest.ini

    The code coverage report is

    ---------- coverage: platform linux, python 3.10.6-final-0 -----------
    Name Stmts Miss Branch BrPart Cover
    ----------------------------------------------------------------------------------------------------------------
    /mnt/data/global_envs/seb_py310/lib/python3.10/site-packages/src/__init__.py 1 0 0 0 100%
    /mnt/data/global_envs/seb_py310/lib/python3.10/site-packages/src/sample.py 8 1 2 0 90%
    ----------------------------------------------------------------------------------------------------------------
    TOTAL 9 1 2 0 91%


    And if I just put the folder next to the test.py (not inside site-packages) I get 100% code coverage. The code that is not covered is the line return x*x. If I don't use multiprocessing, obviously, there is no issue.

    Any idea why I can't get 100% code coverage when it is inside site-packages? I care about this because I am building a python library that uses multiprocessing.pool and I can't get the right coverage level.

    I tried moving the code in many other folders, the issue is only with site-packages. I also tried copying the whole content of site-packages to another folder such as /mnt/data/global_envs/seb_py310/lib/python3.10/sites and I still get 100% code coverage. I also looked into other settings for pytest.ini and using plain coverage but nothing worked. I also tried this https://stackoverflow.com/a/41365659/11598039

    Continue reading...

Compartilhe esta Página