关于python:单元测试Flask视图模拟出芹菜任务

Unit test Flask view mocking out celery tasks

因此,我有一个烧瓶视图,该视图将芹菜任务添加到队列中,并向用户返回200。

1
2
3
4
5
6
7
8
from flask.views import MethodView
from app.tasks import launch_task

class ExampleView(MethodView):
    def post(self):
        # Does some verification of the incoming request, if all good:
        launch_task(task, arguments)
        return 'Accepted', 200

问题在于测试以下内容,我不想拥有一个celery实例等。我只想知道,在所有验证都OK之后,它会向用户返回200。 芹菜launch_task()将在其他地方进行测试。

因此,我很想模拟launch_task()调用,因此本质上它什么都不做,使我的单元测试独立于celery实例。

我尝试过以下各种化身:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@mock.patch('app.views.launch_task.delay'):
def test_launch_view(self, mock_launch_task):
    mock_launch_task.return_value = None
    # post a correct dictionary to the view
    correct_data = {'correct': 'params'}
    rs.self.app.post('/launch/', data=correct_data)
    self.assertEqual(rs.status_code, 200)

@mock.patch('app.views.launch_task'):
def test_launch_view(self, mock_launch_task):
    mock_launch_task.return_value = None
    # post a correct dictionary to the view
    correct_data = {'correct': 'params'}
    rs.self.app.post('/launch/', data=correct_data)
    self.assertEqual(rs.status_code, 200)

但是似乎无法正常工作,我的视图仅以500错误退出。 任何援助将不胜感激!


我也尝试了任何@patch装饰器,但没有用
我在setUp中发现了模拟:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import unittest
from mock import patch
from mock import MagicMock

class TestLaunchTask(unittest.TestCase):
    def setUp(self):
        self.patcher_1 = patch('app.views.launch_task')
        mock_1 = self.patcher_1.start()

        launch_task = MagicMock()
        launch_task.as_string = MagicMock(return_value = 'test')
        mock_1.return_value = launch_task

    def tearDown(self):
        self.patcher_1.stop()

@task装饰器将功能替换为Task对象(请参见文档)。 如果您模拟任务本身,则将Task对象(有点神奇)替换为MagicMock,并且根本不会安排任务。 而是模拟Task对象的run()方法,如下所示:

1
2
3
4
5
6
7
8
9
10
# With CELERY_ALWAYS_EAGER=True
@patch('monitor.tasks.monitor_user.run')
def test_monitor_all(self, monitor_user):
   """
    Test monitor.all task
   """


    user = ApiUserFactory()
    tasks.monitor_all.delay()
    monitor_user.assert_called_once_with(user.key)