allows package

allows.allow(mock_subject: unittest.mock.Mock) → allows.grammar.MockExtensionGrammar[source]

Prepare to extend a Mock from the Python Standard Library with a SideEffect.

allows.receive_method(name: str) → allows.grammar.SideEffectBuilderGrammar[source]

Start building a side effect on a named method of a Mock.

my_mock = Mock()
allow(my_mock).to(receive_method('foo').and_return('bar))

assert my_mock.foo() == 'bar'
allows.return_value(return_value: Any) → allows.grammar.SideEffectBuilderGrammar[source]

Start building a side effect which returns a value when called.

my_mock = Mock()
allow(my_mock).to(return_value('fooby'))

assert my_mock() == 'fooby'
allows.raise_exception(raised_exception: Exception) → allows.grammar.SideEffectBuilderGrammar[source]

Start building a side effect which raises an exception.

my_mock = Mock()
allow(my_mock).to(raise_exception(ValueError))

raised = False
try:
    my_mock()
except ValueError:
    raised = True
assert raised
allows.be_called_with(*args, **kwargs) → allows.grammar.SideEffectBuilderGrammar[source]

Start building a side effect which accepts arguments and keyword arguments

my_mock = Mock()
allow(my_mock).to(be_called_with('spam', foo='bar').and_return('eggs))

assert my_mock('spam', foo='bar') == 'eggs'

Submodules

allows.grammar module

class allows.grammar.MockExtensionGrammar(mock_subject: unittest.mock.Mock)[source]

MockExtensionGrammar is created by the allow factory.

This enables grammar for creating and binding a mock side effect like:

allow <Mock> to <Have Side Effect>

class allows.grammar.SideEffectBuilderGrammar(method_name=None, builder=None)[source]

SideEffectBuilderGrammar is initiated by the return_value, raise_exception, receive_method, be_called_with, have_effect factory methods.

The grammar is chainable, but a side effect can have only one effect (return, exception, effect) per grammar expression. However, side effects will automatically combine if multiple expressions are applied to the same mock/method.

This enables grammar for building the side effect like:

allow <Mock> to

be_called_with <Args> on_method <Name> and_return_value <Value>

and_raise_exception(raised_exception)[source]

Raise an exception. Alias and_raise.

and_return_value(*return_value)[source]

Add a return value (or a list of return values to cycle through). Alias and_return.

apply_to(mock_subject: unittest.mock.Mock) → unittest.mock.Mock[source]

Apply the built side effect to the given Python Mock.

called_with(*args, **kwargs)[source]

Specify call args that trigger the side effect. Alias when_called_with.

on_method(method_name)[source]

Specify method name on the mocked object which will have the side effect applied.

allow(my_mock).to(return_value(5).on_method(‘foo’)) assert my_mock.foo() == 5

with_effect(effect: Callable)[source]

Apply a generic effect when invoking the side effect.

allows.side_effect module

class allows.side_effect.SideEffect(call_args=None, effect=None, default_effect=None)[source]

Callable object that can compose many side effects with corresponding arguments or default response.

merge(other: allows.side_effect.SideEffect) → allows.side_effect.SideEffect[source]

Build a new SideEffect from this one and another

class allows.side_effect.SideEffectBuilder(call_args=None, effect=None)[source]

Uses builder pattern to set up Callable effect and Call argument values for SideEffect objects.

Only one effect can be applied in a given builder (return, exception, effect).

build()[source]

Create the side effect.

with_call_args(*args, **kwargs)[source]

Only invoke side effect when certain args are present

with_effect(effect: Callable)[source]

Add a generic effect to the side effect.

with_raised_exception(raised_exception: Exception)[source]

Raise an exception.

with_return_value(*return_values)[source]

Add a return value.

allows.side_effect.no_op(*args, **kwargs)[source]

allows.exception module

exception allows.exception.AllowsException[source]

Raised when invariants are violated while creating SideEffects (like multiple side effects specified)