Dandelion
Dandelion

Reputation: 756

How to run celery in django tests using PyCharm

I'm trying to test my Django application functionalities.
Some of my tasks are using Celery.
How can I run celery in my test environment using PyCharm?

Upvotes: 1

Views: 447

Answers (3)

Daniel Lee
Daniel Lee

Reputation: 8021

I assume that you're using the shared_task decorator and running the tasks as function_name.delay()

This could be tested by adding a conditional that runs the task if running locally (or via a test) and uses celery in production. This also allows one to run the server locally without using celery, and that can be easier to debug and maintain.

PRODUCTION = False

if PRODUCTION:
    some_task.delay()
else:
    some_task()

One could also create a decorator that does the same thing, which is prettier, but perhaps more complicated to maintain?

Upvotes: 2

Greenev
Greenev

Reputation: 909

To do unit tests without testing the celery stuff you may want to mock your @shared_task decorator

from unittest import mock

@mock.patch('celery.shared_task', lambda *args, **kwargs: lambda func: func)
def test_celery_unit()
    from tasks import mytask
    result = mytask() # could be call just as a function

Upvotes: 2

Sai
Sai

Reputation: 235

using mock library will solve your problem.

Upvotes: 0

Related Questions