liveBook · Manning

Test runner

As explained in Django in Action

A test runner is a software component that discovers, selects, and executes automated tests.

A test runner finds automated tests, executes them, and reports whether they pass. Think of it as the stage manager for your test suite: it decides which cases perform, and when. section 8.1.2 section 8.10

The surrounding unittest structure gives the runner its work:

  • Fixtures prepare data before a test and clean it up afterward. section 8.1.2
  • Test cases contain the code that performs tests; unittest defines them as classes. section 8.1.2
  • Test suites collect test cases for execution. section 8.1.2
  • Python’s default runner searches for files beginning with test_ and runs the cases it finds. section 8.1.2

Django builds on unittest with its own TestCase and runner. The Django runner looks for tests.py files in application directories and methods beginning with test_. Invoke it through the test management command. Django also creates a separate test database, lets tests populate ORM models, and wraps each test method in a transaction that is undone afterward, keeping tests isolated and repeatable. section 8.10 section 8.1.2

Run one test

From the project environment, run `python manage.py test bands.tests.TestBands.test_musician_view` to execute a specific test method instead of the whole suite. section 8.7.2

(venv) RiffMates$ python manage.py test =music

A shortcut command can select every test whose name contains a matching term, such as `python manage.py test =music`. section 8.7.2

How Django in Action covers it

How deep it goes
Explains discovery conventions, database isolation, transactions, and command-line selection, but stops short of runner implementation internals.
Examples
Uses tests.py discovery, test_ methods, a Django test database, and commands selecting one test or matching test names.
What it assumes
Assumes basic Python tests and classes, while defining fixtures, test cases, suites, runners, and Django’s testing additions.
What you can do after
Operate Django’s test management command to run complete suites, individual methods, or selected groups of tests.
How it connects
Connects fixtures, test cases, test suites, unittest, TestCase, the Django ORM, and test management commands.

Also explained in

Compare how 5 books explain test runner →

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.