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。 芹菜
因此,我很想模拟
我尝试过以下各种化身:
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错误退出。 任何援助将不胜感激!
我也尝试了任何
我在
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() |
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) |