PyTest with Django REST Framework

This is a pytest based alternative to

Why I wrote this article

Testing is a key component of a developer’s code lifecycle, be it for whole teams or for lone wolfs, yet comprehensive testing guides have not kept up with Django REST Framework’s growth, and there’s no resource I had the chance to see out there that covers everything I think a testing standard should do for an app. That is to:

  • Have fast enough tests for both efficient code-and-test flows for developers and for CI/CD pipelines.
  • Have comprehensive enough tests to isolate what breaks in the piece of code and what breaks in external/internal code the code we are testing depends on.
  • Leave a clear set of useful tools to pinpoint relevant individual or multiple tests resulting in a rocket-fast workflow for the developer.

In this article I propose a testing approach, an structure for tests, a set of tools for testing while developing and also show how to test each common part of a Django app. This is a work in progress and I seek feedback and suggestions, son any meaningful comment will help tons.

The DRF tests are a thin layer on top of the Django test clients and classes. Although there does not appear to be a good pytest DRF integration, manually tweaking the request headers, etc.