Mocking objects

Mocking is an exceptionally useful part of any tester's tool kit. Mocking allows for custom objects to be over written with an object that can be used to verify if a method is doing the correct thing to its arguments. Sometimes, this may need a bit of re-imagining and a refactoring of your app so as to work in a testable way, but otherwise the concept is simple. We create a mocking object, run it through the method, and then run the tests on that object. It lends itself particularly well to databases and ORM models such as from SQLAlchemy.

There are lots of Mocking frameworks available but, for this book, we shall be using Mockito:

pip install mockito

It is one of the simplest to use:

>>> from mockito import *
>>> mock_object = ...

Get Learning Flask Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.