Coding a test harness for doctest

The tests we have written so far are very simple, because the function we are testing is simple. There are two inputs and one output with no side effects. No objects have to be created. This isn't the most common use case for us. Often, we have objects that interact with other objects.

The doctest module supports creating objects, invoking methods, and checking results. With this recipe, we will explore this in more detail.

An important aspect of doctest is that it finds individual instances of docstrings, and runs them in a local context. Variables declared in one docstring cannot be used in another docstring.

How to do it...

  1. Create a new file called recipe19.py to contain the code from this recipe.
  2. Write a simple ...

Get Python Testing Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.