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 a fixture for a doctest

We'll provide a mock time.time() in our test fixture and use it in our doctest.

  1. Create a file called times.txt containing the following doctest code:
    >>> from time import time
    This isn't a reasonable test for any purpose, but it serves to
    illustrate a test that can't work without a mock object in place.
    >>> '%0.1f' % time()
    '1.0'
    >>> '%0.1f' % time()
    '1.1'
    >>> '%0.1f' % time()
    '1.2'
    
  2. Run the doctest file using Nose, and the following screen gets displayed:
    Time for action - creating a fixture for a doctest
  3. Unless your computer's clock was reset to the beginning of the epoch at just the right moment, the doctest failed. We need a mock to replace time.time() ...

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