Testing Django REST Framework

Satria Bagus
Sulang
Published in
3 min readMar 8, 2018

Recently i have to use Django REST Framework on my college project. Since the project requires me to follow Test-driven-development (TDD) paradigm, i have to write the test for my Django REST apps. However, i am still unfamiliar with the Django and Django REST itself. It took a lot of effort for me to learn and write the test for Django REST. I am sorry if there is any mistake in my code snippet since it was my first time wrote the test for Django REST.

Photo by Fabian Grohs on Unsplash

Now i will show how i implemented testing on my django rest application.

What my application do is to display the all jobs available on the application and show details of each job. So in my testing i will create a list of job and company correspond to each job.

The first things you need to do is to create tests.py file, after that add this following code:

Since the structure of Django and Django REST are different from each other, we use testing library provided by Django REST instead of Django. So instead of extending the TestCase class we have to extend APITestCase provided by Django REST.

The purpose of setUp function is to create temporary models object and defines the required variables.

Once you done, you will need to copy the following code to test views.py file in your Django REST apps.

The purpose of test_job_object_bundle is to verify the response you get from the API is match with the object within your models. It also test that all the job available is returned by the API call.

After that we need to create negative test to make sure unauthorized user cannot access the API and all jobs is displayed only to login users.

The next step is writing the test to ensure that the API can be accessed only for the authenticated user, you will need to copy the following code to test views.py file in your Django REST apps.

We mock the user object with wrong username and password and ensure the API returned 403 http status code.

Now after creating the test for job lists API, we will create the test for job detail API.

The first step is you will need to copy the setup function in the LowonganListAPIViewTestCase class. After you copy the function you will need create the negative test to make sure only authorized user can access the details API.

Now copy the following code:

After you finished, we create the test to make sure the details match with the corresponding job. You need to copy the following code to your tests.py file:

Now you have finished writing test for your Django REST apps.

Now we look the test we have made in action

Unit test is a vital part of your backend application. You can detect the anomaly earlier and you can decide how your API should behave with the certain input. With the unit test maintaining the API become so much easier and if you want to refactor the code later you will code more easily and faster because you know what you will write and how the function should behave.

P.S This tutorial is taken from https://github.com/erdem/DRF-TDD-example with little modification in the code.

--

--