This book has been written for Python 3.4. The version of unittest
that comes with the Python 2.x standard library is an older version that doesn't support all the features that we discussed in this book. Additionally, the mock
library was only made a part of the standard library from Python 3.3 onward.
Fortunately, all the features present in the newer versions of Python have been backported under the unittest2
library. We can install this version from PyPi with the following command:
pip install unittest2
Once installed, we have to use the unittest2
library in all references like the following:
import unittest2 class StockTest(unittest2.TestCase): ...
With these changes, we will be able to use all the features that we have been discussing in this book in all versions from Python 2.5 onward.
The same goes for the mocking library as well. The mock
library was only added to the standard library with Python 3.3. The current mock library has been backported and is also available from PyPi. We can install it with the following command:
pip install mock
And we import it with the following command:
import mock
We can then use all the mocking goodness discussed in this book with earlier versions of Python as well.