testdoubles is a testing framework for python that provides test doubles.
Note
This is work in progress!
testdoubles provides mocks, fakes, stubs and dummies according to the definition by Martin Fowler although Jeff Atwood provides a much more vivid definition and mechanisms for substituting objects with test doubles in a reliable fashion without modifying the code under test.
The standard mock library (available from Python 3.3 and as a separate library) is a great tool for isolating the system under test but it has limitations which testdoulbes lifts and idiosyncrasies which testdoubles removes.
You need to know where to patch.
If you want to patch an object, you have to do so in the module where it's imported into and not the module it's imported from. Alex Marandon explains it quite well.
This limitation completely violates the Zen of Python:
- "Complex is better than complicated." - mock unnecessarily makes patching complicated when there are (complex) solutions available to lift this limitation.
- "There should be one-- and preferably only one --obvious way to do it." - mock does not make patching obvious.
mock has only one object, Mock that is being used for substituting the dependencies of the system under test. Since different test doubles have different purposes the distinction is important.
The distinction improves the test code readability and clarifies what exactly is being tested and how. The distinction also prevents mixing different kinds of test doubles which reduces the mental strain when writing tests.
- Complete API compatibility with mock (testdoubles can be used as a drop in replacement so you're old tests will still work).
- Patching of the dependency and not the call site.
- Completely non-intrusive to the code under test.
- Minimally intrusive to the testing code.
- Mock Objects
- Stub Objects
- Fake Objects
- Dummy Objects
See CONTRIBUTING.rst for details.