Django assertTemplateUsed在redi之后失败

2024-04-26 00:46:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个django2.1实践项目。我测试的最后一行总是不及格。
如果发生重定向,assertTemplateUsed检查是否确实不起作用?

Traceback (most recent call last):
File "test_views.py", line 24, in test_home_page_not_login_redirect self.assertTemplateUsed(resp, 'users/home.html')
File "testcases.py", line 554, in assertTemplateUsed self.fail(msg_prefix + "No templates used to render the response")
AssertionError: No templates used to render the response

test_views.py
def test_home_page_not_login_redirect(self):
    resp = self.client.get('/')
    self.assertEqual(resp.status_code, 302)
    self.assertRedirects(resp, '/accounts/login/?next=/')
    self.assertTemplateUsed(resp, 'users/login.html')


^{pr2}$


settings.py
LOGOUT_REDIRECT_URL = '/accounts/login/'

Tags: inpytestselfhomelinepagenot
1条回答
网友
1楼 · 发布于 2024-04-26 00:46:30

因为repoy是302找到的重定向,所以没有用于HTTP响应的模板!在

如果您想真正遵循重定向链,可以将follow=True参数传递给self.client.gethttps://docs.djangoproject.com/en/2.1/topics/testing/tools/#django.test.Client.get)。通过这种方式,您将能够实际检查用于呈现(重定向)响应的模板。在

相关问题 更多 >