यह पुराना है, लेकिन यूनिट परीक्षण करते समय मैं एक समान मुद्दे में भाग गया। यहां बताया गया है कि मैंने समस्या का हल कैसे किया।
आप रीडायरेक्ट यूआरएल को पकड़ने के लिए response.redirect_chain
और/या response.request['PATH_INFO']
का उपयोग कर सकते हैं।
दस्तावेज़ीकरण भी देखें! Django Testing Tools: assertRedirects
from django.core.urlresolvers import reverse
from django.test import TestCase
class MyTest(TestCase)
def test_foo(self):
foo_path = reverse('foo')
bar_path = reverse('bar')
data = {'bar': 'baz'}
response = self.client.post(foo_path, data, follow=True)
# Get last redirect
self.assertGreater(len(response.redirect_chain), 0)
# last_url will be something like 'http://testserver/.../'
last_url, status_code = response.redirect_chain[-1]
self.assertIn(bar_path, last_url)
self.assertEqual(status_code, 302)
# Get the exact final path from the response,
# excluding server and get params.
last_path = response.request['PATH_INFO']
self.assertEqual(bar_path, last_path)
# Note that you can also assert for redirects directly.
self.assertRedirects(response, bar_path)
आप कुछ गलत कर रहे हैं। –