O'Reilly logo

Learning Python Testing by Daniel Arbuckle

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The doctest language

Like program source code, doctest tests are written in plain text. The doctest module extracts the tests and ignores the rest of the text, which means that the tests can be embedded in human-readable explanations or discussions. This is the feature that makes doctest suitable for uses such as program specifications.

Example – creating and running a simple doctest

We are going to create a simple doctest file, to show the fundamentals of using the tool. Perform the following steps:

  1. Open a new text file in your editor, and name it test.txt.
  2. Insert the following text into the file:
    This is a simple doctest that checks some of Python's arithmetic
    operations.
    
    >>> 2 + 2
    4
    
    >>> 3 * 3
    10
  3. We can now run the doctest. At the command prompt, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required