pytest-celery is a pytest plugin that provides fixtures and utilities for testing Celery applications. It streamlines setting up and tearing down Celery workers and brokers (like Redis, RabbitMQ, SQS) within your pytest test suite. Currently at version 1.3.0, it maintains an active development pace with frequent updates to support new Python and Celery versions, and address compatibility issues.
pip install "pytest-celery[all]"Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a basic test for a Celery task using pytest-celery's fixtures. It configures Celery to use Redis, starts a worker, and executes a task. Ensure your test environment has a running Redis instance or adjust the `celery_config` fixture accordingly. Remember to replace `my_app.tasks` with the actual path to your Celery tasks.
Upgrade Python to 3.9 or higher, or pin `pytest-celery<1.3.0` in your project dependencies.
If experiencing SQS transport issues, check your installed `celery` and `kombu` versions against `pytest-celery`'s dependency requirements. You may need to manually install `pycurl` (e.g., `pip install 'pytest-celery[sqs]'`) or ensure `urllib3` is correctly used, depending on the `pytest-celery` version.
Use `celery_session_app` and `celery_session_worker` fixtures for session-scoped Celery instances, or define your own fixtures with `scope='session'` or `scope='module'`.
Ensure `setuptools` is explicitly listed as a dependency if your project's build process requires it, rather than relying on transitive installation.
Ensure `pytest-celery` is correctly installed (`pip install pytest-celery`) and that your pytest environment can discover the plugin. If running in a virtual environment, confirm it is activated.
Install or upgrade `pytest-celery` to the latest version (`pip install -U pytest-celery`) and ensure the command is run in an environment where the plugin is active.
Increase the timeout using `--celery-worker-timeout=<seconds>` in your pytest command, verify your Celery app configuration, ensure the broker (e.g., Redis, RabbitMQ) is running and accessible, and check the worker's startup logs for specific errors.
Adjust your `PYTHONPATH` environment variable or ensure you are running `pytest` from a directory where `your_project.celery_app` is importable, for example, `pytest --celery-app=my_app.celery` when `my_app/celery.py` exists.