Expected URL with primary key when testing SimpleTestCase.assertRedirects()

113 views
Skip to first unread message

Antonio Alaniz

unread,
Jun 17, 2014, 10:26:27 PM6/17/14
to django...@googlegroups.com
I'm testing a redirect that would include the primary key. Can anyone tell me how I can test the redirect URL if it involves the primary key? Will the following work?:

self.assertRedirects(response, 'message/(?P<message_id>\d+)/', 200)

Kelvin Wong

unread,
Jun 18, 2014, 6:15:13 AM6/18/14
to django...@googlegroups.com
This is how I usually do redirect tests:

self.assertEqual(response.status_code, 302)
self.assertRedirects(
        response,
        'http://testserver' + reverse('pizza:deleted')
)

I recommend using named URLs, that way your message_id would be similar to this:

reverse('pizza:delete', args=[pizza.id])

Here is the complete app's tests if you want to take a look


K

Antonio Alaniz

unread,
Jun 18, 2014, 9:30:43 AM6/18/14
to django...@googlegroups.com
Thanks, K. I think one of my main issues, after looking at your tests, is that I'm not currently using the client to test the redirect. I have this test as a part of my functional_tests.py, but it sounds like I need to possibly build this into my unit tests as I see your test is in tests.py. I'm going move some of this work around between functional and unit tests and then try out your suggestion with the client. 

Tony


On Tuesday, June 17, 2014 7:26:27 PM UTC-7, Antonio Alaniz wrote:
Reply all
Reply to author
Forward
0 new messages