Testing Django REST API endpoints
This section will introduce writing test cases that test Django REST framework endpoints. When testing any REST API endpoints created using the Django REST framework, we need to use the APITestCase
class provided by the rest_framework.test
library. We also should use the APIClient()
class provided by that same library when requiring authentication, instead of using the Client()
class as we did before.
In the following exercises, we will create one test class that performs two tests: the first will create an engine object and the other will update an object.
Creating an object test case
This test will use the POST
request method to send data to the http://localhost:8000/chapter-8/engines/ endpoint and create an engine object in the database. Since we are loading a data fixture that contains only two engine objects with the IDs 1
and 2
, we should expect the new object to be created at index 3
, but your results may vary. We will refer back to...