Skip to main content

Fork of celerytest with minor bug fixes

celerytest - Integration testing with Celery

Writing (integration) tests that depend on Celery tasks is problematic. When you manually run a Celery worker together with your tests, it runs in a separate process and there’s no clean way to address objects targeted by Celery from your tests. When you use a separate test database (as with Django for example), you’ll have to duplicate configuration code so your Celery worker accesses the same database.

celerytest provides the ability to run a Celery worker in the background from your tests. It also allows your tests to monitor the worker and pause until Celery tasks are completed.

Using celerytest

To start a Celery worker in a separate thread, use:

app = Celery() # your Celery app
worker = start_celery_worker(app) # configure the app for our celery worker

To wait for the worker to finish executing tasks, use:

result = some_celery_task.delay()
worker.idle.wait() # optionally specify time-out

Django

To use this with your django app through django-celery, get your app as such:

from djcelery.app import app
worker = start_celery_worker(app)

TestCase

If you want to use this in a unittest TestCase, you can use CeleryTestCaseMixin. If you’re writing unit tests that depend on a celery worker, though, you’re doing it wrong. For unit tests, you’ll want to mock your Celery methods and test them separately. You could use CeleryTestCaseMixin to write integration tests with Celery tasks, though.

from unittest import TestCase
from celerytest.testcase import CeleryTestCaseMixin, setup_celery_worker
import time

app = Celery()
setup_celery_worker(app) # need to setup worker outside

class SomeTestCase(CeleryTestCaseMixin, TestCase):
    celery_app = app
    celery_concurrency = 4

    def test_something(self):
        result = multiply.delay(2,3)
        self.worker.idle.wait()
        self.assertEqual(result.get(), 6)

Lettuce

To automatically launch a worker in the background while running a Lettuce integration test suite, add to terrain.py:

# my_celery_app.py
app = Celery('my_celery_app', broker='amqp://')

# terrain.py
from lettuce import *
from celerytest import start_celery_worker

# replace this with an import of your actual app
from my_celery_app import app

@before.harvest
def initial_setup(server):
    # memory transport may not work here
    world.celery = start_celery_worker(app, config="amqp")

@after.harvest
def cleanup(server):
    world.celery.stop()

@after.each_step
def after_step(step):
    # make sure we've received any scheduled tasks
    world.celery.active.wait(.05)
    # allow tasks to complete
    world.celery.idle.wait(5)

Installation

Install the latest version of celerytest from PyPI:

$ pip install celerytest

Or, clone the latest version of celerytest from GitHub and run setup:

$ git clone git://github.com/RentMethod/celerytest.git
$ cd celerytest
$ ./setup.py install # as root

Release files for celery-testutils 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for celery-testutils 0.1.2
File Size Uploaded
celery_testutils-0.1.2.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for celery-testutils 0.1.2
File Interpreter ABI Platform
celery_testutils-0.1.2-py2-none-any.whl Python 2 none any Details

Total release size: 15.5 kB

Release files / celery_testutils-0.1.2.tar.gz

Download URL celery_testutils-0.1.2.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fa7fe7909398fc7f7b87a7745f5da9d943a31ab5cdd3d3d36c279ab9b43b2690
BLAKE2b-256 checksum
How to use checksums
f61167546e307a2f42afff0ee456b99a1f805e6456b1be989036a84291fd460f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / celery_testutils-0.1.2-py2-none-any.whl

Download URL celery_testutils-0.1.2-py2-none-any.whl
Size 8.8 kB
Tags Python 2
SHA-256 checksum
How to use checksums
05f607f64346754a7a05941498105abfa5bcfb29f50a15c566f1cf07766d9d26
BLAKE2b-256 checksum
How to use checksums
d7c1b9d59b7f784d5c58d8510a2891d5af93e715c8404c8f6150aed0017db839
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page