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 = mock() >>> mock_object.example() >>> verify(mock_object).example() True
The preceding code imports the functions from the Mockito
library, creates a mock
object that can be used for mocking, runs a method on it, and...