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

Integrating with Python Mocker

You've used Mocker enough to see the repetitiveness involved in creating a mocking context at the beginning of the text and calling its verify and restore methods at the end. Mocker simplifies this for you by providing a class called MockerTestCase in the mocker module. MockerTestCase behaves just like a normal unittest TestCase, except that for each test, it automatically creates a mocking context, which it then verifies and restores after the test. The mocking context is stored in self.mocker.

The following example demonstrates MockerTestCase by using it to write a test involving a mock of time.time. Before the test gets executed, a mocking context is stored in self.mocker. After the test is run, the context is ...

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