Learning to test
So far in this book, we haven't covered how to create tests. Now is a good time to do that, so we are going to create tests for the methods that we created in the model manager.
Why do we need tests? The short answer to this question is that tests will allow us to know that the methods or functions are doing the right thing. The other reason (and one of the most important, in my opinion) is that tests give us more confidence when it comes to performing changes in the code.
Django has great tools out of the box for creating unit and integration tests, and combined with frameworks like Selenium, it is possible to basically test all of our application.
With that said, let's create our first tests. Django creates a file called test.py
in the app
directory when creating a new Django app. You can write your tests in there, or if you prefer to keep the project more organized by separating the tests into multiple files, you can remove that file and create a directory called tests
and...