Commit ae63c497 by Tom Christie

Added test case classes

parent 6de9b7c8
...@@ -167,6 +167,36 @@ As usual CSRF validation will only apply to any session authenticated views. Th ...@@ -167,6 +167,36 @@ As usual CSRF validation will only apply to any session authenticated views. Th
--- ---
# Test cases
REST framework includes the following test case classes, that mirror the existing Django test case classes, but use `APIClient` instead of Django's default `Client`.
* `APISimpleTestCase`
* `APITransactionTestCase`
* `APITestCase`
* `APILiveServerTestCase`
## Example
You can use any of REST framework's test case classes as you would for the regular Django test case classes. The `self.client` attribute will be an `APIClient` instance.
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
class AccountTests(APITestCase):
def test_create_account(self):
"""
Ensure we can create a new account object.
"""
url = reverse('account-list')
data = {'name': 'DabApps'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data, data)
---
# Testing responses # Testing responses
## Checking the response data ## Checking the response data
......
...@@ -6,6 +6,7 @@ from __future__ import unicode_literals ...@@ -6,6 +6,7 @@ from __future__ import unicode_literals
from django.conf import settings from django.conf import settings
from django.test.client import Client as DjangoClient from django.test.client import Client as DjangoClient
from django.test.client import ClientHandler from django.test.client import ClientHandler
from django.test import testcases
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.compat import RequestFactory as DjangoRequestFactory from rest_framework.compat import RequestFactory as DjangoRequestFactory
from rest_framework.compat import force_bytes_or_smart_bytes, six from rest_framework.compat import force_bytes_or_smart_bytes, six
...@@ -137,3 +138,19 @@ class APIClient(APIRequestFactory, DjangoClient): ...@@ -137,3 +138,19 @@ class APIClient(APIRequestFactory, DjangoClient):
# Ensure that any credentials set get added to every request. # Ensure that any credentials set get added to every request.
kwargs.update(self._credentials) kwargs.update(self._credentials)
return super(APIClient, self).request(**kwargs) return super(APIClient, self).request(**kwargs)
class APISimpleTestCase(testcases.SimpleTestCase):
client_class = APIClient
class APITransactionTestCase(testcases.TransactionTestCase):
client_class = APIClient
class APITestCase(testcases.TestCase):
client_class = APIClient
class APILiveServerTestCase(testcases.LiveServerTestCase):
client_class = APIClient
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment