O'Reilly logo

Python Testing Beginner's Guide 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

Time for action - creating and running your first doctest

We'll create a simple doctest, to demonstrate the fundamentals of using doctest.

  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. The details of how we do that depend on which version of Python we're using. At the command prompt, change to the directory where you saved test.txt.
  4. If you are using Python 2.6 or higher, type:
    $ python ‑m doctest test.txt
    
    
  5. If you are using python 2.5 or lower, the above command may seem to work, but it won't produce the expected result. This is because Python 2.6 is the first version ...

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