Repository navigation
Closed
Description
Summary
Running a test suite involving Celery task workers, the new coverage.py patch = subprocess feature correctly measures the workers. But with the same coverage settings, using pytest-cov does not measure the workers.
Reproducer
small_celery.py:
from celery import Celery
from celery.contrib.testing import worker
from testcontainers.redis import RedisContainer
import pytest
app = Celery("tasks", broker="redis://localhost:6379/0", backend="redis://localhost:6379/0")
@app.task
def add(x, y):
return x + y
@pytest.fixture(scope="session")
def redis_container():
with RedisContainer() as container:
yield container
@pytest.fixture
def celery_app(redis_container):
host = redis_container.get_container_host_ip()
port = redis_container.get_exposed_port(6379)
redis_url = f"redis://{host}:{port}/0"
app.conf.update(broker_url=redis_url, result_backend=redis_url)
return app
@pytest.fixture
def celery_worker(celery_app):
with worker.start_worker(
celery_app,
pool="prefork",
perform_ping_check=False,
):
yield
def test_add_task(celery_worker):
result = add.delay(4, 4)
assert result.get() == 8requirements.txt:
celery[redis]
pytest
testcontainers[redis]
pyproject.toml:
[tool.coverage.run]
patch = ["subprocess", "_exit"]Run with coverage:
% coverage run -m pytest small_celery.py; coverage combine; coverage report -m
============================= test session starts ==============================
platform darwin -- Python 3.11.13, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/nbatchelder/lab/celery/5
configfile: pyproject.toml
plugins: cov-6.2.1
collected 1 item
small_celery.py . [100%]
============================== 1 passed in 4.29s ===============================
Combined data file .coverage.MacBook-RX5KW.42996.XaDnQVWx
Combined data file .coverage.MacBook-RX5KW.42999.XxepszGx
Name Stmts Miss Cover Missing
-----------------------------------------------
small_celery.py 26 0 100%
-----------------------------------------------
TOTAL 26 0 100%
Run with pytest-cov:
% pytest --cov=. --cov-report=term-missing small_celery.py
============================= test session starts ==============================
platform darwin -- Python 3.11.13, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/nbatchelder/lab/celery/5
configfile: pyproject.toml
plugins: cov-6.2.1
collected 1 item
small_celery.py . [100%]
================================ tests coverage ================================
______________ coverage: platform darwin, python 3.11.13-final-0 _______________
Name Stmts Miss Cover Missing
-----------------------------------------------
small_celery.py 26 1 96% 10
-----------------------------------------------
TOTAL 26 1 96%
============================== 1 passed in 4.36s ===============================
Something about pytest-cov is interfering with coverage.py's new subprocess support. What can we do about that?
Versions
% uv pip freeze
Using Python 3.11.13 environment at: /Users/nbatchelder/lab/celery/.venv
amqp==5.3.1
billiard==4.2.1
celery==5.5.3
certifi==2025.8.3
charset-normalizer==3.4.3
click==8.2.1
click-didyoumean==0.3.1
click-plugins==1.1.1.2
click-repl==0.3.0
coverage==7.10.3
docker==7.1.0
idna==3.10
iniconfig==2.1.0
kombu==5.5.4
packaging==25.0
pluggy==1.6.0
prompt-toolkit==3.0.51
pygments==2.19.2
pytest==8.4.1
pytest-cov==6.2.1
python-dateutil==2.9.0.post0
python-dotenv==1.1.1
redis==5.2.1
requests==2.32.4
six==1.17.0
testcontainers==4.12.0
typing-extensions==4.14.1
tzdata==2025.2
urllib3==2.5.0
vine==5.1.0
wcwidth==0.2.13
wrapt==1.17.2
Metadata
Metadata
Assignees
Labels
No labels
Activity
nedbat commentedon Aug 15, 2025
I've added an experimental new patch to coverage:
patch = fork, in this branch:I don't understand how it can be helping, but it does. If you use
patch = subprocess,fork,_exit, then pytest-cov gets all of the code. Anyone want to help me understand why? It seems to involve billiard (celery's multiprocessing tool).nedbat commentedon Aug 17, 2025
I've shipped
patch = forkin coverage.py 7.10.4. I don't know if there's any interest in figuring this out. If not, feel free to close this issue.ionelmc commentedon Aug 18, 2025
There is, it's on my list. I suspect there's some bad interaction with how pytest-cov setup the coverage. I am thinking that might be simplified considering the variety of patches that coverage has now builtin.
ionelmc commentedon Aug 30, 2025
@nedbat I've looked at this and the problem is that we make Coverage instances with
data_suffix=get_some_random_string(). Turns out this suffix will be used in the subprocess too and will make for some race conditions. I guess I'll replace that withdata_suffix=True(that will make coverage make the suffix on demand right?).nedbat commentedon Aug 31, 2025
TBH, I don't understand what the difference is, because I don't understand how pytest-cov is coordinating its Coverage instances.
ionelmc commentedon Sep 7, 2025
Well if you set the data suffix it will be used for all the subprocesses, so each new subprocess will mess with the same data file. I think I figured out a way to simplify pytest-cov. My plan is to get a new pytest-cov out that requires coverage>=7.10.6, since all those patch releases made significant changes to the subprocess support in coverage.