Stub classes


class reahl.stubble.stub.StubClass(orig, check_attributes_also=False)

A stub class is a class you use in a test as a substitute for another class, but with some methods implemented differently to the real class for purposes of the current test.

You mark a class as a stub class by decorating it with @stubclass(TheRealClass).

If you do this, the signatures of the methods you supply on the stubclass are checked against those on the real class in order to ensure your tests will break if the signature of the methods provided on the stub no longer match that of the same methods in TheRealClass.


class reahl.stubble.stub.Exempt(value)

A method on a stub class that is decorated with @exempt will not be checked against the real class.

This allows you to add methods on the stub that are NOT present on the real class.


class reahl.stubble.stub.SlotConstrained

Assign an instance of slotconstrained to a variable in class scope to check that a similarly named variable exists in the __slots__ of the real class:

class TheRealClass:
    __slots__ = ('a')

class Stub:
    a = slotconstrained()


class reahl.stubble.stub.CheckedInstance

Assign an instance of checkedinstance to a variable in class scope to check that a similarly named class variable exists on the real class:

class TheRealClass:
    a = 'something'

class Stub:
    a = checkedinstance()


class reahl.stubble.stub.Impostor

A class that inherits from Impostor, and is also a @stubclass(TheRealClass) gains the dubious benefit that the following would be true:

isinstance(stub_class_instance, TheRealClass)


class reahl.stubble.stub.Delegate(real)

A class that inherits from Delegate, and is an @stubclass(TheRealClass) can be constructed as a wrapper to an already existing instance of TheRealClass.

When methods are called on it that it defines itself, they will be called. When calling methods not defined on it, these are delegated to the wrapped TheRealClass.

Like an Impostor, the following is true for a Delegate:

isinstance(stub_class_instance, TheRealClass)



class reahl.stubble.easteregg.EasterEgg(name='test', location=None)

A stubbed-out Distribution that can be used to fake a Distribution for testing.

Once an EasterEgg has been constructed, it needs to be added to the global pkg_resources.working_set. If the location is changed after being added, it is necessary to .activate() the EasterEgg before it will be active on sys.path.

  • name – A unique name for this Distribution.

  • location – The location on disk where the contents of this Distribution reside.

Changed in version 3.2: Added the location keyword argument and deprecated set_module_path().

Changed in version 4.0: Removed set_module_path()


Adds this EasterEgg to the global pkg_resources.working_set object.


Return the entry point map for group, or the full entry map


Ensure distribution is importable on path (default=sys.path)

Intercepting calls


class reahl.stubble.intercept.SystemOutStub

The SystemOutStub can be used as context manager to test output that some code sends to sys.stdout.

For example, the following code does not output anything to the console:

with SystemOutStub() as monitor:

assert monitor.captured_output == 'hello\n'

The output captured during the time the SystemOutStub was active.


class reahl.stubble.intercept.CallMonitor(method)

The CallMonitor is a context manager which records calls to a single method of an object or class. The calls are recorded, but the original method is also executed.

For example:

class SomeClass:
    def foo(self, arg):
        return 'something'

s = SomeClass()

with CallMonitor( as monitor:
    assert'a value') == 'something'

assert monitor.times_called == 1
assert monitor.calls[0].args == ('a value',)
assert monitor.calls[0].kwargs == {}
assert monitor.calls[0].return_value == 'something'
property times_called

The number of calls that were made during the time the CallMonitor was active.


A list of MonitoredCalls made, one for each call made, in the order they were made


class reahl.stubble.intercept.MonitoredCall(args, kwargs, return_value)

The record of one call that was made to a method. This class is not intended to be instantiated by a programmer. Programmers can query instances of MonitoredCall returned by a reahl.stubble.intercept.CallMonitor or reahl.stubble.intercept.InitMonitor.


The value returned by the call


The tuple with positional arguments passed during the call


The dictionary with keyword arguments passed during the call


class reahl.stubble.intercept.InitMonitor(monitored_class)

An InitMonitor is like a reahl.stubble.intercept.CallMonitor, except it is used to intercept calls to the __init__ of a class. (See reahl.stubble.intercept.CallMonitor for attributes.)

property times_called

The number of calls that were made during the time the CallMonitor was active.


A list of MonitoredCalls made, one for each call made, in the order they were made


reahl.stubble.intercept.replaced(method, replacement, on=None)

A context manager which replaces the method passed in as method with the callable in replacement for the duration of the managed context.

class SomethingElse:
    def foo(self, n, y='yyy'):
        assert None, 'This should never be reached in this test'

s = SomethingElse()

def replacement(n, y=None):
    return y

with replaced(, replacement):
    assert, y='a') == 'a'
    assert == None

assert == 'yyy'