pytest-dev / pytest-cov

Coverage plugin for pytest.
MIT License
1.72k stars 211 forks source link

Regression in 4.0.0 vs 3.0.0 in coverage with multiprocessing #554

Closed segasai closed 1 year ago

segasai commented 1 year ago

Summary

When using multiprocessing, coverage is lost with pytest-cov 4.0.0 comparing to 3.0.0

Expected vs actual result

Expected coverage 100%, actual 33%

Reproducer

We need two files

$ cat /tmp/test/test_x.py 
import multiprocessing as mp
import xtest

def test_x():
    pool = mp.Pool(2, xtest.initer)
    pool.map(xtest.doer, range(10))
    pool.close()
    pool.join()
$ cat /tmp/test/py/xtest.py 
def initer():
    x = 2
    y = 3
    z = 4

def doer(x):
    return x

When I run this with pytest-cov-4.0.0 I get this

$ env PYTHONPATH=py:$PYTHONPATH pytest --cov=xtest test_x.py 
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.1.3, pluggy-1.0.0
rootdir: /tmp/test
plugins: profiling-1.7.0, forked-1.4.0, parallel-0.1.1, cov-4.0.0, anyio-3.6.1, xdist-2.5.0
collected 1 item                                                               

test_x.py .                                                              [100%]

---------- coverage: platform linux, python 3.8.10-final-0 -----------
Name          Stmts   Miss  Cover
---------------------------------
py/xtest.py       6      4    33%
---------------------------------
TOTAL             6      4    33%

============================== 1 passed in 0.04s ===============================

Now with the 3.0.0 version of pytest-cov

/tmp/test$ env PYTHONPATH=py:$PYTHONPATH pytest --cov=xtest test_x.py 
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.1.3, pluggy-1.0.0
rootdir: /tmp/test
plugins: cov-3.0.0, profiling-1.7.0, forked-1.4.0, parallel-0.1.1, anyio-3.6.1, xdist-2.5.0
collected 1 item                                                               

test_x.py .                                                              [100%]

---------- coverage: platform linux, python 3.8.10-final-0 -----------
Name          Stmts   Miss  Cover
---------------------------------
py/xtest.py       6      0   100%
---------------------------------
TOTAL             6      0   100%

============================== 1 passed in 0.06s ===============================

Versions

$ python --version
Python 3.8.10
$ pytest --version
pytest 7.1.3
$ pip list| grep pytest
pytest                            7.1.3
pytest-cov                        3.0.0
pytest-forked                     1.4.0
pytest-parallel                   0.1.1
pytest-profiling                  1.7.0
pytest-xdist                      2.5.0
$ pip list| grep cover
coverage                          6.5.0
coveralls                         3.3.1

Config

default config

Code

given above

segasai commented 1 year ago

I see that multiprocessing support is dropped in 4.0.0. Sorry for the noise.

azuline commented 10 months ago

I see that multiprocessing support is dropped in 4.0.0. Sorry for the noise.

Where did you see this? I've been unsuccessfully trying to get multiprocessing to work.

segasai commented 10 months ago

I see that multiprocessing support is dropped in 4.0.0. Sorry for the noise.

Where did you see this? I've been unsuccessfully trying to get multiprocessing to work.

See here: https://github.com/pytest-dev/pytest-cov/blob/master/CHANGELOG.rst

azuline commented 10 months ago

Thanks so muchhhh