Setting up tests for Django and DRF
In this chapter, we shall primarily focus on how to write unit tests and integration tests for Django and DRF projects. In the Python and Django community, there is a long-standing debate over which of the following packages is better for writing tests for projects:
unittest
(https://docs.djangoproject.com/en/stable/topics/testing/) comes shipped with Django and is also an extension of Python’sunittest
packagepytest-django
(https://pytest-django.readthedocs.io/en/latest/) usespytest
under the hood and provides a lot of abstraction, which takes care of all the basic nuances of writing tests
We shall not get into the debate and allow developers to decide which package is better from their own experience and preference. We shall take the simple route and follow what the Django official documentation recommends, using the default Django unittest
to set up tests and learn them.
Let us now get started with writing test cases...