Skip to main content

Pytest plugin for inspecting Celery task queues in Redis during tests

Project description

Inspect Celery Task Queues within Pytest

Pytest plugin for inspecting Celery task queues in Redis during tests. Enables assertions on the number of jobs in a queue, etc. Helpful for integration tests.

This plugin provides utilities for testing Celery tasks without running workers, allowing you to inspect queued tasks directly from Redis.

Installation

uv add pytest-celery-utils --dev

This package contains a pytest plugin which is automatically registered when the package is installed.

Requirements

  • Celery with Redis broker
  • Redis server

Usage

The plugin automatically provides a celery_utils fixture that you can use in your tests:

def test_task_queued(celery_utils):
    # Queue a task
    my_task.delay(arg1, arg2)

    # Check if task is queued
    assert celery_utils.count_jobs_of_type("myapp.tasks.my_task") == 1

    # Get all queued tasks
    tasks = celery_utils.get_all_queued_tasks()
    assert len(tasks) > 0

    # Get tasks of specific type
    my_tasks = celery_utils.jobs_of_type("myapp.tasks.my_task")
    assert len(my_tasks) == 1

Configuration

You need to provide a celery_app fixture in your test configuration:

# conftest.py
import pytest
from celery import Celery

@pytest.fixture
def celery_app():
    app = Celery("myapp", broker="redis://localhost:6379/0")
    return app

API Reference

CeleryTestUtils

The main utility class providing methods for inspecting Celery queues.

Methods

  • get_all_queued_tasks() -> list[dict[str, Any]] Returns all tasks currently in the Redis queue(s).

  • jobs_of_type(task: Union[str, Callable]) -> list[dict[str, Any]] Returns all tasks of a specific type. Can accept either a task name string or a task function.

  • count_jobs_of_type(task: Union[str, Callable]) -> int Returns the count of tasks of a specific type.

Utility Functions

  • dig(path, data, *, default=None) Navigate nested dictionaries using dot-path notation or list of keys.

    from pytest_celery_utils import dig
    
    data = {"headers": {"task": "myapp.tasks.example"}}
    task_name = dig("headers.task", data)
    # or
    task_name = dig(["headers", "task"], data)
    

Limitations

  • Only supports Redis broker (not RabbitMQ or other brokers)
  • Designed for testing without workers running
  • Not optimized for performance (intended for testing only)

License

MIT

Author

Michael Bianco (mike@mikebian.co)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_celery_utils-0.1.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_celery_utils-0.1.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_celery_utils-0.1.1.tar.gz.

File metadata

  • Download URL: pytest_celery_utils-0.1.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_celery_utils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7af94a00f7f001a5c43ced7fe65fef01628eb23ab3339f659d203b6d8080b7b2
MD5 cdf2c47de87a91f55cfc44ed0f0c7e30
BLAKE2b-256 ac40f40ed946bb7a6140cdb16b55e3ce67582fc37902589b82b2bcf8dca6571f

See more details on using hashes here.

File details

Details for the file pytest_celery_utils-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_celery_utils-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_celery_utils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa7bc77a9cfa4169dd1d7030a1e757e5fb16313b5c9dc87951607736a813ea08
MD5 c84612045d3b44c74b85a811508b5863
BLAKE2b-256 95418fde76656a9fefab689826cbe7dc16bd231ca5d6dee607498de9c1d4aa10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page