liveBook · Manning

Test database

As explained in Django in Action

A test database is an isolated database created for executing automated tests without modifying application data.

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

How Django in Action covers it

How deep it goes
Explains creation, empty initial state, per-test transactions, rollback, setup, and response assertions.
Examples
Uses a Musician fixture, a musician-detail GET request, status and context assertions, and rendered-name verification.
What it assumes
Assumes Django models and TestCase usage, while showing from the setup stage that test data must be created explicitly.
What you can do after
Diagnose view behavior safely by creating ORM fixtures, sending test requests, and checking status, context, and content.
How it connects
Connects test databases with Django TestCase, the ORM, database transactions, the test runner, and HTTP test clients.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.