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 module fixture

We'll build a test module with a module-level fixture. In the fixture, we'll replace the datetime.date.today function, which normally returns an object representing the current date. We want it to return a specific value, so that our tests can know what to expect.

  1. Create a directory called tests. We'll use this directory in this Time for action, as well as in the next one.
  2. Within the tests directory, create a file called module_fixture_tests.py containing the following code:
    from unittest import TestCase from mocker import Mocker from datetime import date mocker = Mocker() def setup(): fake_date = mocker.replace(date) fake_date.today() mocker.result(date(year = 2009, month = 6, day = 12)) mocker.count(1, ...

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