【发布时间】:2013-06-16 10:40:34
【问题描述】:
所以,我有一个烧瓶视图,它将芹菜任务添加到队列中,并向用户返回 200。
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 实例等。我只想知道,在所有验证都正常之后,它会向用户返回 200。 celery launch_task() 将在别处进行测试。
因此,我热衷于模拟 launch_task() 调用,所以它基本上什么都不做,使我的单元测试独立于 celery 实例。
我尝试过以下各种化身:
@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 错误退出。任何帮助将不胜感激!
【问题讨论】:
标签: python unit-testing mocking flask celery