Skip to content

Commit ae63c49

Browse files
committed
Added test case classes
1 parent 6de9b7c commit ae63c49

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

docs/api-guide/testing.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,36 @@ As usual CSRF validation will only apply to any session authenticated views. Th
167167

168168
---
169169

170+
# Test cases
171+
172+
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`.
173+
174+
* `APISimpleTestCase`
175+
* `APITransactionTestCase`
176+
* `APITestCase`
177+
* `APILiveServerTestCase`
178+
179+
## Example
180+
181+
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.
182+
183+
from django.core.urlresolvers import reverse
184+
from rest_framework import status
185+
from rest_framework.test import APITestCase
186+
187+
class AccountTests(APITestCase):
188+
def test_create_account(self):
189+
"""
190+
Ensure we can create a new account object.
191+
"""
192+
url = reverse('account-list')
193+
data = {'name': 'DabApps'}
194+
response = self.client.post(url, data, format='json')
195+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
196+
self.assertEqual(response.data, data)
197+
198+
---
199+
170200
# Testing responses
171201

172202
## Checking the response data

rest_framework/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.conf import settings
77
from django.test.client import Client as DjangoClient
88
from django.test.client import ClientHandler
9+
from django.test import testcases
910
from rest_framework.settings import api_settings
1011
from rest_framework.compat import RequestFactory as DjangoRequestFactory
1112
from rest_framework.compat import force_bytes_or_smart_bytes, six
@@ -137,3 +138,19 @@ def request(self, **kwargs):
137138
# Ensure that any credentials set get added to every request.
138139
kwargs.update(self._credentials)
139140
return super(APIClient, self).request(**kwargs)
141+
142+
143+
class APISimpleTestCase(testcases.SimpleTestCase):
144+
client_class = APIClient
145+
146+
147+
class APITransactionTestCase(testcases.TransactionTestCase):
148+
client_class = APIClient
149+
150+
151+
class APITestCase(testcases.TestCase):
152+
client_class = APIClient
153+
154+
155+
class APILiveServerTestCase(testcases.LiveServerTestCase):
156+
client_class = APIClient

0 commit comments

Comments
 (0)