Using django-nose and django-celery together -- unit testing
Asked Answered
L

3

9

I have a django project that used django-nose. I'd like to add django-celery to the project. I use unit tests. Both django-nose and django-celery need a TEST_RUNNER setting in my settings.py file. Specifically:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

for django-nose and:

TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'

for django-celery.

How should I handle this so that I can use both packages?

Lipetsk answered 23/3, 2013 at 2:38 Comment(1)
+1 idk, maybe have to subclass one and add the functionality of the other to your own class.Barberabarberry
L
6

I found that the best way to handle this is to skip the Celery test runner. I just use decorate my celery-using tests with a @override_settings(CELERY_ALWAYS_EAGER=True) and everything gets tested nicely.

Lipetsk answered 6/5, 2013 at 18:42 Comment(0)
H
4

If you're able to isolate your tests into celery and non-celery dependent functionality, you could try overriding settings on the test classes that invoke celery tasks:

from django.test.utils import override_settings

@override_settings(TEST_RUNNER='djcelery.contrib.test_runner.CeleryTestSuiteRunner')
class AsyncTestCase(TestCase):
     def test_async(self):
         self.assertEquals(add.delay(4,4), 8)

while the NoseTestRunner would be set as the default in settings.py

Hose answered 15/4, 2013 at 1:48 Comment(0)
E
2

You can subclass Celery runner and Nose runner, then you get good sides for all of them.

from django_nose import NoseTestSuiteRunner
from djcelery.contrib.test_runner import CeleryTestSuiteRunner

class TestRunner(CeleryTestSuiteRunner, NoseTestSuiteRunner):
    pass

Then in your settings:

TEST_RUNNER = '<package to>.TestRunner'

Why it works:

help(TestRunner)

You get:

|  Method resolution order:
|      TestRunner
|      djcelery.contrib.test_runner.CeleryTestSuiteRunner
|      django_nose.runner.NoseTestSuiteRunner
|      django_nose.runner.BasicNoseRunner
|      django_nose.runner.BaseRunner
|      django.test.runner.DiscoverRunner
|      builtins.object

The test CeleryTestRunner did nothing just setting some config and then call super().setup_test_environment(). Then the super will looking for setup_test_environment() in that chain.

Watch the video from Raymond Hettinger: super considered super(here).

Electrocautery answered 15/12, 2015 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.