GithubHelp home page GithubHelp logo

churchlab / django-celery-testworker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rentmethod/django-celery-testworker

0.0 17.0 0.0 166 KB

Test functions to use with Django for tests that depend on the execution of Celery tasks

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-celery-testworker's Introduction

django-celery-testworker - Testing with Django and Celery

Writing tests for Django projects that depend on Celery tasks is problematic. The test that queues the task typically operates on the test database (by virtue of Django's test framework), whereas the Celery worker, using the django-celery integration, operates on the main database. When using the database backend for Celery, tasks will not even get picked up by the worker to begin with.

A celery worker that operates on the test database is needed. This app runs such a worker automatically in the background while running the test suite.

Using django-celery-testworker

To enable django-celery-testworker for your project you need to add djcelery-testworker to INSTALLED_APPS:

INSTALLED_APPS += ("djcelery_testworker", )

To run a Celery worker that operates on your test databases, run with Django's management command:

./manage.py celerytestworker

To create a unit test that depends on a Celery task, use the CeleryWorkerTestCase class. A worker will then automatically be started for this test.

from testcase import CeleryWorkerTestCase
import time

class SomeTestCase(CeleryWorkerTestCase):
    def test_something(self):
        task = some_task.delay()
        time.sleep(1)
        self.assertEqual(task.state, "SUCCESS")

Technically, tests that depend on a celery worker running are not real unit tests, though, and should be implemented as integration tests.

To automatically launch a worker in the background while running an integration test suite, use the run_celery_test_worker function. For example, you could use it with lettuce by including it in terrain.py:

# terrain.py
from django.test.simple import DjangoTestSuiteRunner
from django.test.utils import setup_test_environment, teardown_test_environment
from lettuce import *
from splinter.browser import Browser
from djcelery_testworker import run_celery_test_worker

@before.harvest
def initial_setup(server):
    setup_test_environment()
    
    # set up the test databases using DjangoTestSuiteRunner
    world.django_suite = DjangoTestSuiteRunner()
    world.django_suite.old_config = world.django_suite.setup_databases()

    # run a celery worker
    print "Starting Celery worker..."
    world.celeryd = run_celery_test_worker()

    # run browser for selenium tests
    world.browser = Browser()

@after.harvest
def cleanup(server):
    # quit the browser
    world.browser.quit()

    # terminate the celery worker
    try:
        print "Terminating Celery worker..."
        if world.celeryd.returncode is None:
            world.celeryd.kill()
        world.celeryd.wait()
    except:
        raise
    
    # destroy the test database using the DjangoTestSuiteRunner
    world.django_suite.teardown_databases(world.django_suite.old_config)

    # teardown
    teardown_test_environment()

Installation

Clone the latest version of django-celery-testworker from GitHub:

$ git clone git://github.com/RentMethod/django-celery-testworker.git

You can install it by doing the following,:

$ tar xvfz django-celery-testworker-0.0.0.tar.gz
$ cd django-celery-testworker-0.0.0
# python setup.py install # as root

django-celery-testworker's People

Contributors

willembult avatar glebkuznetsov avatar

Watchers

Alexander (Sasha) Wait Zaranek avatar Mad Price Ball avatar  avatar Changping Chen avatar Daniel Bryan Goodman avatar James Cloos avatar Marc Lajoie avatar Adam Marblestone avatar Ben Pruitt avatar  avatar Alex Garruss avatar  avatar George Church avatar Tim avatar Surge Biswas avatar Aditya Kunjapur avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.