APIs provide value to our users by letting them automate their interactions with our service. Naturally, DRF helps us automate testing our code as well.
DRF provides replacements for all the common Django tools we discussed in Chapter 8, Testing Answerly:
APIRequestFactory
for Django'sRequestFactory
classAPIClient
for Django'sClient
classAPITestCase
for Django'sTestCase
class
APIRequestFactory
and APIClient
make it easier to send requests formatted for our API. For example, they provide an easy way to set credentials for a request that isn't relying on session-based authentication. Otherwise, the two classes serve the same purpose as their default Django equivalents.
TheAPITestCase
class simply extends Django's TestCase
class and replaces Django's Client
with APIClient
.
Let's take a look at an example that we can add to django/mailinglist/tests.py
:
class ListMailingListsWithAPITestCase(APITestCase): def setUp(self): password = 'password' username = 'unit...