Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Mock不';不要在芹菜里工作_Python_Django_Unit Testing_Mocking_Celery - Fatal编程技术网

Python:Mock不';不要在芹菜里工作

Python:Mock不';不要在芹菜里工作,python,django,unit-testing,mocking,celery,Python,Django,Unit Testing,Mocking,Celery,我想使用pythonmock库来测试我的Django应用程序是否发送电子邮件 测试代码: # tests.py from django.test import TestCase class MyTestCase(TestCase): @mock.patch('django.core.mail.mail_managers') def test_canceled_wo_claiming(self, mocked_mail_managers): client = C

我想使用python
mock
库来测试我的Django应用程序是否发送电子邮件

测试代码:

# tests.py
from django.test import TestCase

class MyTestCase(TestCase):

    @mock.patch('django.core.mail.mail_managers')
    def test_canceled_wo_claiming(self, mocked_mail_managers):
        client = Client()
        client.get('/')
        print(mocked_mail_managers.called)
        mocked_mail_managers.assert_called_with('Hi, managers!', 'Message Body')
第一个示例-无任务

# views.py
from django.views.generic import View
from django.core.mail import mail_managers

class MyView(View):

    def get(self, request):
        mail_managers('Hi, managers!', 'Message Body')
        return HttpResponse('Hello!')
# views.py
from django.views.generic import View
from . import tasks

class MyView(View):
    def get(self, request):
        tasks.notify.apply_async()
        return HttpResponse('Hello!')


# tasks.py
from celery import shared_task
from django.core.mail import mail_managers

@shared_task
def notify():
    mail_managers('Hi, managers!', 'Message Body')
第二个示例-带任务

# views.py
from django.views.generic import View
from django.core.mail import mail_managers

class MyView(View):

    def get(self, request):
        mail_managers('Hi, managers!', 'Message Body')
        return HttpResponse('Hello!')
# views.py
from django.views.generic import View
from . import tasks

class MyView(View):
    def get(self, request):
        tasks.notify.apply_async()
        return HttpResponse('Hello!')


# tasks.py
from celery import shared_task
from django.core.mail import mail_managers

@shared_task
def notify():
    mail_managers('Hi, managers!', 'Message Body')
第一个示例正常工作,第二个示例失败,未调用
异常

我的设置:

# Celery
BROKEN_URL = 'memory://'
BROKER_BACKEND = 'memory'

CELERY_ALWAYS_EAGER = True
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'

是否有可能执行这样的集成测试,或者解决此问题的唯一方法是将测试一分为二?

直接从使用异步任务的代码测试异步任务的行为可能很棘手。一个原因是测试可能在任务实际运行之前就执行断言,这可能会给您带来误报。在这种情况下,我要做的是将测试分为两个步骤:

  • 模拟任务,并测试在必须调用任务时是否调用该任务,以及是否使用预期参数
  • 将任务作为独立函数进行测试,并将其作为正常函数执行,即不需要芹菜服务器
  • 举例来说,这可能类似于:

    # views.py
    from path.to.tasks import my_task
    
    
    def my_view(requtest):
        # do stuff
        my_task.delay('foo', 'bar')
        return HttpResponse('whatever')
    
    
    # test_my_task.py
    from views import my_view
    from path.to.tasks import my_task
    
    
    class MyTest(TestCase):
        @mock.patch('path.to.tasks.my_task')
        def test_my_task_is_called(self, mocked_task):
            client = Client()
            client.get('/')
            my_task.assert_called_with('foo', 'bar')
    
        def test_my_task_works(self):
            my_task('foo', 'bar')  # note I don't use .delay(...), .apply_async(...), etc
            assert 'my task did what I expected it to do'
    
    通过这种方式,您可以测试实现代码在任务方面的行为是否正确,以及任务在按预期调用后的行为是否正确


    我希望它有用!:)

    我发现了一个问题,这很愚蠢。以及:

    基本原则是,在查找对象的位置进行修补,这不一定与定义对象的位置相同

    我需要改变:

    @mock.patch('django.core.mail.mail_managers')
    


    还有一点-我用
    task\u name.delay(args)
    调用task,所以我必须修补
    task\u caller\u location.task\u name.delay
    ,而不仅仅是
    task\u caller\u location.task\u name
    。杰拉德,谢谢你的写作!无法理解官方文件,但您的方法运行良好!