Simpledependency: An Embarrassingly Simple Library for Replacing Dependencies
Simpledependency lets you globally change the behaviour of one class so that it is instantiated as a different class.
Why is this good? It’s good for replacing far reaching dependencies that would screw with unit tests, e.g., make Memcached always instantiated as a faked version.
Usage
Decorate the replaceable classes with the @dependency_decorator decorator.
@dependency_decorator
class Example(object):
def __init__(self):
print 'Example class called.'
Create a replacement class, in my case this is usually a mock or fake object for unit testing.
class MockExample(object):
def __init__(self):
print 'Mock example class called.'
Actually replace the dependency.
override_dependency(original_class=Example, replacement_class=MockExample)