How to write unit test to view django?

I have problems understanding how unit tests should be designed for django.

In my opinion, testing the whole look in one go seems impossible. We need to distinguish between preliminary and post-request states. But I have no idea how to do this. Is there a real life example?

When looking at the documentation, the examples are too simplistic and focused only on the model.

@login_required def call_view(request, contact_id): profile = request.user.get_profile() if request.POST: form = CallsForm(profile.company, request.POST) if form.is_valid() return HttpResponseRedirect('/contact/' + contact_id + '/calls/') else: form = CallsForm(profile.company, instance=call) variables = RequestContext(request, {'form':form} return render_to_response('conversation.html', variables) 

update:

trying to do a successful test job, but still failing:

 def test_contact_view_success(self): # same again, but with valid data, then self.client.login(username='username1', password='password1') response = self.client.post('/contact/add/', {u'last_name': [u'Johnson'], }) self.assertRedirects(response, '/') 

error message:

 AssertionError: Response didn't redirect as expected: Response code was 200 (expected 302) 

I think this is because form.is_valid () fails and does not redirect, fix it?

+46
django unit-testing
Aug 09 2018-12-12T00:
source share
2 answers

NB NB! What I describe below is not strictly a "unit test"; it's almost impossible to write an independent unit test for Django view code. It is rather an integration test ...

You are correct that there are several ways in your view:

  • GET or POST an anonymous user (must be redirected to the login page)
  • GET or POST registered user without a profile (should raise a UserProfile.DoesNotExist exception)
  • GET registered user (must show form)
  • POST registered user with empty data (form errors should be displayed)
  • POST registered user with invalid data (form errors should be displayed)
  • POST registered user with valid data (must be redirected)

Testing 1. really just testing @login_required , so you can skip it. I still test it (just in case, I or someone else forgot to use this decorator).

I'm not sure if the failure case (500 error pages) in 2. is what you really want. I would decide what you want instead (perhaps use get_or_create() or catch the DoesNotExist exception and create a new profile this way).

Depending on how many user checks you have, 4. really don't need to be tested.

In any case, given all of the above, I would do something like:

 from django.test import TestCase class TestCalls(TestCase): def test_call_view_denies_anonymous(self): response = self.client.get('/url/to/view', follow=True) self.assertRedirects(response, '/login/') response = self.client.post('/url/to/view', follow=True) self.assertRedirects(response, '/login/') def test_call_view_loads(self): self.client.login(username='user', password='test') # defined in fixture or with factory in setUp() response = self.client.get('/url/to/view') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'conversation.html') def test_call_view_fails_blank(self): self.client.login(username='user', password='test') response = self.client.post('/url/to/view', {}) # blank data dictionary self.assertFormError(response, 'form', 'some_field', 'This field is required.') # etc. ... def test_call_view_fails_invalid(self): # as above, but with invalid rather than blank data in dictionary def test_call_view_fails_invalid(self): # same again, but with valid data, then self.assertRedirects(response, '/contact/1/calls/') 

Obviously, the disadvantage here is hard-coded URLs. You can use reverse() in your tests or create queries using RequestFactory and call your views as methods (and not at the URL). However, with the latter method, you still need to use hard-coded values ​​or reverse() to check redirection targets.

Hope this helps.

+76
Aug 09 '12 at 15:57
source share

Django comes with a test client that can be used to check the complete request / response cycle: Documents contain an example of receiving a request to a given URL and approving a status code, as well as the context of the template. You will also need a test that performs POST and claims successful redirects as expected.

+4
Aug 09 2018-12-18T00:
source share



All Articles