How can I auto-specialize the mocking function of Celery

Given a celery job, what is the easiest way to mock a function using autospec?

For example, in python Python 2.7.5 this code will go fine:

from mock import create_autospec
from celery import task

@task
def function(a, b, c):
    pass

mock_function = create_autospec(function)
mock_function.delay('wrong arguments')

When it should throw an exception because the celery "delay" method will accept any parameter.

+4
source share
1 answer

In fact, you are trying to check the arguments of the Task.run () function.

See the docs below: http://celery.readthedocs.org/en/latest/userguide/tasks.html#custom-task-classes http://celery.readthedocs.org/en/latest/reference/celery.app. task.html # celery.app.task.Task.run

run() , . , , :

from mock import create_autospec
from celery import task

@task
def function(a, b, c):
    pass

mock_function = create_autospec(function)
mock_function.run('wrong arguments')

TypeError: <lambda>() takes exactly 4 arguments (2 given)
+5

Source: https://habr.com/ru/post/1540637/


All Articles