testdoubles

testdoubles is a library that provides testdoubles.


Keywords
testdoubles
License
BSD-3-Clause
Install
pip install testdoubles==0.1.0

Documentation

testdoubles

Latest PyPI version Number of PyPI downloads Build Status Coverage Status Dependencies Status

testdoubles is a testing framework for python that provides test doubles.

BSD3 license Documentation

Note

This is work in progress!

Overview

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.

Rationale

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.

Where to patch

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.

One objects to rule them all

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.

Features

  • 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

Contributing

Stories in Ready

See CONTRIBUTING.rst for details.