Skip to content

Celery task workers measured by coverage, but not by pytest-cov #708

@nedbat

Description

@nedbat

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() == 8

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions