Django also provides an API ( LiveServerTestCase) and tools for using different testing frameworks, for example you can integrate with the popular Selenium framework to simulate a user interacting with a live browser. These allow you to simulate requests, insert test data, and inspect your application's output. The Django framework adds API methods and tools to help test web and Django-specific behavior.
Despite the name, this test framework is suitable for both unit and integration tests. Testing a website is a complex task, because it is made of several layers of logic – from HTTP-level request handling, queries models, to form validation and processing, and template rendering.ĭjango provides a test framework with a small hierarchy of classes that build on the Python standard unittest library.
This tutorial shows how to write automated tests for Django, by adding a number of tests to the LocalLibrary website. test-driven and behavior-driven development).
For these reasons, some software development processes start with test definition and implementation, after which the code is written to match the required behavior (e.g. Often they are the basis for your code examples and documentation. In addition, automated tests can act as the first real-world "user" of your code, forcing you to be rigorous about defining and documenting how your website should behave.
If we were to continue as we are, eventually we'd be spending most of our time testing, and very little time improving our code.Īutomated tests can really help with this problem! The obvious benefits are that they can be run much faster than manual tests, can test to a much lower level of detail, and test exactly the same functionality every time (human testers are nowhere near as reliable!) Because they are fast, automated tests can be executed more regularly, and if a test fails, they point to exactly where code is not performing as expected. As we make changes and grow the site, the time required to manually check that everything works "properly" will only grow. Even with this relatively small site, manually navigating to each page and superficially checking that everything works as expected can take several minutes. The Local Library currently has pages to display lists of all books and authors, detail views for Book and Author items, a page to renew BookInstances, and pages to create, update, and delete Author items (and Book records too, if you completed the challenge in the forms tutorial).