Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django testing named URLs with additional GET parameters

I am trying to write some tests for a Django application I'm working on but I haven't yet decided on the exact urls I want to use for each view. Therefore, I'm using named urls in the tests.

For example, I have a url named dashboard:

c = Client()
resp = c.get(reverse('dashboard'))

This view should only be available to logged in users. If the current user is anonymous, it should redirect them to the login page, which is also a named url. However, when it does this, it uses an additional GET parameter to keep track of the url it just came from, which results in the following:

/login?next=dashboard

When I then try to test this redirect, it fails because of these additional parameters:

# It's expecting '/login' but gets '/login?next=dashboard'
self.assertRedirects(resp, reverse('login'))

Obviously, it works if I hard code them into the test:

self.assertRedirects(resp, '/login?next=dashboard')

But then, if I ever decide to change the URL for my dashboard view, I'd have to update every test that uses it.

Is there something I can do to make it easier to handle these extra parameters?

Any advice appreciated.

Thanks.

like image 626
Dan Avatar asked Feb 23 '12 10:02

Dan


1 Answers

As you can see, reverse(...) returns a string. You can use it as:

self.assertRedirects(resp, '%s?next=dashboard' % reverse('login'))
like image 50
greg Avatar answered Sep 20 '22 23:09

greg