Git Product home page Git Product logo

celerytest's Introduction

Build Status

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

celerytest's People

Contributors

alpham avatar fedalto avatar thedrow avatar willembult avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

celerytest's Issues

No module named `config`

Hello, I am trying to setup celerytest to work with Django. I have tried following all the steps but got stuck trying to setup a unit test. Specifically, when importing from celerytest import setup_celery_worker the __init__.py file at the root of the repo is not able to find the package config. When checking out the repo this makes sense to have worked locally but when packaged through pip it is trying to find config through my local files. Below is my stack trace and code file.

Traceback (most recent call last):

  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/Users/albertorodriguez/Projects/SAFA/tgen-api/api/views/tests/test_prediction_view.py", line 1, in <module>
    from tests.api_base_test import ApiBaseTest
  File "/Users/albertorodriguez/Projects/SAFA/tgen-api/api/tests/api_base_test.py", line 5, in <module>
    from celerytest import setup_celery_worker
  File "/Users/albertorodriguez/Projects/SAFA/tgen-api/venv/lib/python3.9/site-packages/celerytest/__init__.py", line 1, in <module>
    from config import CELERY_TEST_BACKEND_CONFIG, CELERY_TEST_CONFIG
ModuleNotFoundError: No module named 'config'

Code:

import json
from typing import Dict
from unittest import TestCase

from celerytest import setup_celery_worker
from celerytest.testcase import CeleryTestCaseMixin
from django.core.wsgi import get_wsgi_application
from django.test import Client
from dotenv import load_dotenv

from server.celery import app

setup_celery_worker(app)  # need to setup worker outside


class ApiBaseTest(CeleryTestCaseMixin, TestCase):
    """
    The common unit test for API layer.
    """
    celery_app = app
    celery_concurrency = 4

    def setUp(self) -> None:
        """
        Sets up the django environment.
        """
        load_dotenv()
        get_wsgi_application()

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.