Skip to content

Commit 683f1f4

Browse files
author
Ryan P Kilby
committed
Add docs for URLPatternsTestCase
1 parent c30a336 commit 683f1f4

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

docs/api-guide/testing.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ similar way as with `RequestsClient`.
292292

293293
---
294294

295-
# Test cases
295+
# API Test cases
296296

297297
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`.
298298

@@ -324,6 +324,32 @@ You can use any of REST framework's test case classes as you would for the regul
324324

325325
---
326326

327+
# URLPatternsTestCase
328+
329+
REST framework also provides a test case class for isolating `urlpatterns` on a per-class basis. Note that this inherits from Django's `SimpleTestCase`, and will most likely need to be mixed with another test case class.
330+
331+
## Example
332+
333+
from django.urls import include, path, reverse
334+
from rest_framework.test import APITestCase, URLPatternsTestCase
335+
336+
337+
class AccountTests(APITestCase, URLPatternsTestCase):
338+
urlpatterns = [
339+
path('api/', include('api.urls')),
340+
]
341+
342+
def test_create_account(self):
343+
"""
344+
Ensure we can create a new account object.
345+
"""
346+
url = reverse('account-list')
347+
response = self.client.get(url, format='json')
348+
self.assertEqual(response.status_code, status.HTTP_200_OK)
349+
self.assertEqual(len(response.data), 1)
350+
351+
---
352+
327353
# Testing responses
328354

329355
## Checking the response data

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy