A test database is an isolated, temporary database that lets automated tests use Django’s ORM without changing application data. Django creates it when the test runner starts; it begins empty, so tests must create the objects they need. section 8.3 section 8.10
Each test runs inside a database transaction. When the test finishes, Django rolls that transaction back, returning the test database to its previous state. Tests therefore start from a known environment and can run in any order. section 8.3
A typical database-backed view test follows this flow:
- Create required ORM objects in setUp(), making shared test data available before each test. section 8.3
- Call the view through Django’s test client with client.get() or client.post(). section 8.10
- Check the response status, context, and rendered content, including negative cases such as a 404 response. section 8.10
Testing a musician view
setUp() creates a Musician in the test database. The test builds a URL containing that object’s ID, requests it with client.get(), then checks for a 200 status, the expected musician in the template context, and the musician’s name in the response content. section 8.3
