Skip to content

Commit

Permalink
Added context and response helpers
Browse files Browse the repository at this point in the history
    - Store last response in self.last_response
    - Store last response's context in self.context
    - Added assertInContext() method
  • Loading branch information
frankwiles committed May 23, 2015
1 parent 5215720 commit eb1e80b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ Another thing you do often is HTTP get urls, our ```get()``` method assumes you
def test_get_named_url(self):
response = self.get('my-url-name')

When using this get method two other things happen for you, we store the last
response in ``self.last_response``` and the response's Context in ```self.context```. So instead of:

def test_default_django(self):
response = self.client.get(reverse('my-url-name'))
self.assertTrue('foo' in response.context)
self.assertEqual(response.context['foo'], 12)

You can instead write:

def test_testplus_get(self):
self.get('my-url-name')
self.assertInContext('foo')
self.assertEqual(self.context['foo'], 12)

### post(url_name, data, **args, ***kwargs)

Our ```post()``` method takes a named URL, the dictionary of data you wish to post and any args or kwargs necessary to reverse the url_name.
Expand All @@ -68,6 +83,14 @@ Our ```post()``` method takes a named URL, the dictionary of data you wish to po
def test_post_named_url(self):
response = self.post('my-url-name', data={'coolness-factor': 11.0})

### assertInContext(key)

You can ensure a specific key exists in the last response's context by using:

def test_in_context(self):
self.get('my-view-with-some-context')
self.assertInContext('some-key')

### response_XXX(response) - status code checking

Another test you often need to do is check that a response has a certain HTTP status code. With Django's default TestCase you would write:
Expand Down
15 changes: 13 additions & 2 deletions test_plus/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,24 @@ class TestCase(TestCase):
"""
user_factory = None

def __init__(self, *args, **kwargs):
self.last_response = None
super(TestCase, self).__init__(*args, **kwargs)

def tearDown(self):
self.client.logout()

def get(self, url_name, *args, **kwargs):
""" GET url by name using reverse() """
return self.client.get(reverse(url_name, args=args, kwargs=kwargs))
self.last_response = self.client.get(reverse(url_name, args=args, kwargs=kwargs))
self.context = self.last_response.context
return self.last_response

def post(self, url_name, *args, **kwargs):
""" POST to url by name using reverse() """
data = kwargs.pop("data", None)
return self.client.post(reverse(url_name, args=args, kwargs=kwargs), data)
self.last_response = self.client.post(reverse(url_name, args=args, kwargs=kwargs), data)
return self.last_response

def response_200(self, response):
""" Given response has status_code 200 """
Expand Down Expand Up @@ -183,3 +190,7 @@ def assertGoodView(self, url_name, *args, **kwargs):
self.response_200(response)

return response

def assertInContext(self, key):
if self.last_response:
self.assertTrue(key in self.last_response.context)
12 changes: 12 additions & 0 deletions test_project/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ def test_assertnumqueries_warning(self):
self.assertTrue('skipped' in str(w[-1].message))
else:
return unittest.skip("Only useful for Django 1.6 and before")

def test_assertincontext(self):
response = self.get('view-context-with')
self.assertTrue('testvalue' in response.context)

self.assertInContext('testvalue')
self.assertTrue(self.context['testvalue'], response.context['testvalue'])

@unittest.expectedFailure
def test_assertnotincontext(self):
self.get('view-context-without')
self.assertInContext('testvalue')
4 changes: 3 additions & 1 deletion test_project/test_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .views import (
view_200, view_201, view_302, view_404, needs_login, data_1,
data_5,
data_5, view_context_with, view_context_without
)


Expand All @@ -18,4 +18,6 @@
url(r'^view/needs-login/$', needs_login, name='view-needs-login'),
url(r'^view/data1/$', data_1, name='view-data-1'),
url(r'^view/data5/$', data_5, name='view-data-5'),
url(r'^view/context/with/$', view_context_with, name='view-context-with'),
url(r'^view/context/without/$', view_context_without, name='view-context-without'),
)
12 changes: 11 additions & 1 deletion test_project/test_app/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render

from .models import Data

Expand Down Expand Up @@ -29,10 +30,19 @@ def data_1(request):
list(Data.objects.all())
return HttpResponse('', status=200)


def data_5(request):
list(Data.objects.all())
list(Data.objects.all())
list(Data.objects.all())
list(Data.objects.all())
list(Data.objects.all())
return HttpResponse('', status=200)


def view_context_with(request):
return render(request, 'base.html', {'testvalue': True})


def view_context_without(request):
return render(request, 'base.html', {})
Empty file.

0 comments on commit eb1e80b

Please sign in to comment.