I am working on a Django 2.1 practice project. Last line of my test keeps failing.
Is it true that assertTemplateUsed check won't work if redirection happens?
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')
urls.py
url(r'^login/$',
auth_views.LoginView.as_view(
template_name='users/login.html',
redirect_authenticated_user=True),
name='users_login'),
settings.py
LOGOUT_REDIRECT_URL = '/accounts/login/'
Since the reploy is a 302 FOUND redirect there's no template used for the HTTP response!
If you want to actually follow the redirect chain you can pass the follow=True
parameter to self.client.get
(https://docs.djangoproject.com/en/2.1/topics/testing/tools/#django.test.Client.get). This way you'll be able to actually check which template was used to render the (redirected) response.