8 Testing your project
This chapter covers
- Reasons to write automated tests
- Writing and running unit tests in Django
- Authenticating a user during a test
- Posting data and uploading files during a test
How do you know that a change in your code isn’t going to break existing functionality? Automated testing is the answer. This chapter shows you how to write tests to check that your project continues to work even while you’re making changes and adding features.
8.1 Automated testing
If you’ve been coding along as you’ve been reading, you have written more than 20 views and likely have over a thousand lines of code. It doesn’t take long before manually testing everything in your project becomes a time-consuming endeavor. This becomes more complicated with a multiuser website: you need to test some views several times, using different credentials to ensure the right people can get where they’re supposed to and the wrong people can’t.
This section’s header gives away the solution: automated testing. You write code to test your code. Learning how to write good, automated tests is a skill in itself and an important part of working as a developer. It isn’t uncommon to find a 1:1 relationship between lines of code and lines of tests in larger organizations with a high degree of automation. That means you’ll be spending half your time writing tests.