【发布时间】:2015-12-28 02:43:08
【问题描述】:
我正在为 Flask 应用程序编写单元测试,但不确定为什么响应返回状态代码 200 而不是 302。我正在测试的视图使用重定向,并且我的帖子设置了 follow_redirects=False。
下面是测试和查看代码:
#test_views.py
def test_can_post_new_engagement(self):
response = self.client.post(
url_for('main.new_engagement'),
data={
'title': 'Sample Title',
'client': 1},
follow_redirects=False)
self.assertEqual(response.status_code, 302)
#views.py
@main.route('/engagement/new', methods=['GET', 'POST'])
def new_engagement():
form = EngagementForm()
form.client.choices = [(o.id, o.official_name) for o in
Organization.query.order_by('official_name')]
form.client.choices.insert(0, (0, '--- Select Organization ---'))
if form.validate_on_submit():
eng = Engagement(title=form.title.data,
client_id=form.client.data)
db.session.add(eng)
return redirect(url_for('main.new_pentest'))
return render_template('engagement_form.html', form=form)
错误:
FAIL: test_can_post_new_engagement (test_views.NewEngagementView)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ramces/dev/global_engagement_manager/tests/unit/test_views.py
", line 36, in test_can_post_new_engagement
self.assertEqual(response.status_code, 302)
AssertionError: 200 != 302
【问题讨论】:
标签: python flask python-unittest