I want to write some tests for views in a Django profile app.
The views have some smart error-handling logic. e.g. if we try to create a profile, but the profile already exists, then just redirect to the existing profile page (or maybe to the edit profile page).
How do I test that this error-handling works as desired?
What are best-practices?
One idea would be to do BDD using Zombie.js, and test that I see a page whose title isn’t “Create profile” (or maybe check that I see a page whose title is “Edit profile”). But the Django testing docs say:
- Use Django’s test client to establish that the correct view is being
called and that the view is collecting the correct context data. - Use in-browser frameworks such as Twill and Selenium to test rendered
HTML and the behavior of Web pages, namely JavaScript functionality.
However, if I want to use the Django test client, it can do the following:
- Simulate GET and POST requests on a URL and observe the response — everything from low-level HTTP (result headers and status codes) to page content.
- Test that the correct view is executed for a given URL.
- Test that a given request is rendered by a given Django template, with a template context that contains certain values.
Should I use the test client and then look at the page content? Should I see what template got rendered? What is the appropriate way to test this view?
Redirects and profile creation are very django so while you might use other frameworks to test JS functionality, I’d be using the django test client for this instance.
You have to decide on a case by case basis how far to go with your tests. Time is limited, and some aspects are more prone to breakage than others. For example, I wouldn’t be worried about template resolution – a 200 status code and a quick check for content is plenty.
Anyhoo, I’d start by identifying the different states the view has (I’m guessing here):
This would confirm that the correct profile is being redirected to given whatever data you’ve decided should trigger this action.