From the course: Building RESTful Web APIs with Django

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Test case for a CreateAPIView subclass

Test case for a CreateAPIView subclass

From the course: Building RESTful Web APIs with Django

Test case for a CreateAPIView subclass

- [Instructor] Testing Django REST framework API views is a bit different compared to testing Django views. Django REST framework provides test case classes. Each of the test case classes implements the same interface as Django's test cases. However, they use Django REST framework's HTTP client instead of Django's client to specifically test API views. We are going to write a unit test for the create product API. Instead of importing test case from Django we import API test case from REST framework. We import our product model, and then we begin to write our test. We keep track of the initial product count, and we're going to be creating a new product with a specific name, description, and a price. We're going to be checking the response from the client, and POSTing to the product's new endpoint. If for whatever reason we couldn't create a new product, we're going to print out whatever response we got, so that we can see…

Contents