A database transaction groups several database actions into one atomic unit: either the whole group completes, or the changes can be rolled back. Django uses this idea to keep tests isolated. Each test method runs inside a transaction, and Django rolls back its database changes when the method finishes. section 8.3
A Django database test typically follows this flow:
- Start with Django’s separate test database, which begins empty. section 8.3
- Create required ORM objects in setUp(), such as a Musician used by every test. section 8.3
- Call the view through Django’s test client and inspect its response. section 8.10
- Finish the test and let Django roll back its transaction, leaving the test database ready for the next test. section 8.3
Testing a musician view
setUp() creates a Musician object. test_musician_view() requests /bands/musician/<id>/, then checks for a 200 status, the expected object in the response context, and the musician’s name in the rendered content. Afterward, the transaction undoes the created database state. section 8.3













