Writing Tests for Django Models
Write tests for the User, Post, and Comment models using Pytest.
We'll cover the following
When applying testing to a Django project, it’s always a good idea to start with writing tests for the models. But why test the models?
Well, it gives us better confidence in our code and the connections to the database. It’ll make sure that methods or attributes on the model are well represented in the database, but it can also help us with code structure, resolving bugs, and building documentation.
Without further ado, let’s start by writing tests for the User
model.
Writing tests for the User
model
Inside the core/user
directory, let’s create a new file called tests.py
. We’ll write tests to create a user and a simple user:
Get hands-on with 1400+ tech skills courses.