mockify - Library core

Library core module.

class mockify.Call(name, args=None, kwargs=None)

Bases: object

Binds mock name with arguments it was called with or it is expected to be called with.

Call objects are created in mock frontends (like mockify.mock.Function mock class) by methods expected_call and __call__ by simply passing their argument to Call constructor.

Instances of this class are comparable. Two Call objects are equal if and only if all attributes (name, args and kwargs) are the same. For example:

>>> Call('foo') == Call('foo')
True
>>> Call('foo') != Call('bar')
True
>>> Call('foo', (1, 2), {'c': 3}) == Call('foo', (1, 2), {'c': 3})
True

Call objects can also be created with use of matchers, for example mockify.matchers.Any, that will match any value:

>>> from mockify.matchers import _
>>> Call('foo', (_, _)) == Call('foo', (1, 2))
True
>>> Call('foo', (_, _)) == Call('foo', (3, 4))
True
Parameters:
  • name – Function or method name.
  • args – Positional arguments
  • kwargs – Named arguments
args

Mock positional args.

classmethod create(*args, **kwargs)

Factory method for easier Call object creating.

You must give at least one positional argument - the name. All other will be passed to constructor’s args and kwargs parameters.

New in version 0.5.

kwargs

Mock named args.

name

Mock name.

class mockify.Expectation(expected_call, filename, lineno)

Bases: object

Class representing single expectation.

Instances of this class are normally created by registry objects using Registry.expect_call() method. Each instance of this class is correlated with exactly one mockify.engine.Call object representing expected mock call pattern.

After Expectation object is created by call to some expect_call method, it can be mutated using following methods:

Parameters:
  • call – Instance of mockify.engine.Call representing expected mock call pattern
  • filename – File name were this expectation was created
  • lineno – Line number where this expectation was created
__call__(call)

Call this expectation object.

If given call object does not match expected_call then this method will raise TypeError exception.

Otherwise, total call count is increased by one and:

  • if actions are recorded, then next action is executed and its result returned or mockify.exc.OversaturatedCall exception is raised if there are no more actions
  • if there are no actions recorded, just None is returned
expected_call

Instance of mockify.engine.Call representing expected mock call pattern.

This basically is exactly the same Call object as was passed to Expectation constructor.

format_action()

Return textual representation of next action to be executed.

This method uses action’s __str__ method to render action name.

Returns None if there were no actions recorded or all were consumed.

This is used by mockify.exc.Unsatisfied exception when rendering error message.

format_actual()

Return textual representation of how many times this expectation was called so far.

This is used by mockify.exc.Unsatisfied exception when rendering error message.

format_expected()

Return textual representation of how many times this expectation is expected to be called.

This is used by mockify.exc.Unsatisfied exception when rendering error message.

format_location()

Return textual representation of place (filename and lineno) where this expectation was created.

Basically, it just returns [filename]:[lineno] string, where filename and lineno are given via Expectation constructor.

is_satisfied()

Check if this expectation is satisfied.

match(call)

Check if expected_call matches call.

times(expected_count)

Record how many times this expectation is expected to be called.

Parameters:expected_count

Expected call count.

This can be either integer number (exact call count) or instance of one of classes from mockify.times module.

will_once(action)

Attach action to be executed when this expectation gets consumed.

This method can be used several times, making action chains. Once expectation is consumed, next action is executed and removed from the list. If there are no more actions, another call will fail with mockify.exc.OversaturatedCall exception.

After this method is used, you can also use will_repeatedly() to record repeated action that will get executed after all single actions are consumed.

Parameters:action

Action to be executed.

See mockify.actions for details.

will_repeatedly(action)

Attach repeated action to be executed when this expectation is called.

This method is used to record one action that gets executed each time this expectation object is called. By default, when repeated action is recorded, expectation can be called any number of times (including zero).

After setting repeated action, you can also set expected call count using times().

Parameters:action

Action to be executed.

See mockify.actions for details.

class mockify.Registry(expectation_class=None, uninterested_call_strategy='fail')

Bases: object

Acts like a database for Expectation objects.

This class is used as a backend for higher level mocking utilities (a.k.a. frontends), like mockify.mock.Function mocking class. It provides methods to record, lookup and verifying of expectations.

There can be many instances of registry classes, or one that can be shared between various mock frontends. For example, you can create one registry in setup code, then create various mocks inside your tests, to finally trigger assert_satisfied() of that single registry in test’s teardown code. Or you can just use frontends with their defaults. It is completely up to you.

Parameters:
  • expectation_class

    This is optional.

    Used to give custom subclass of Expectation to be used inside this registry.

  • uninterested_call_strategy

    Setup the way how uninterested calls are treated.

    Following values are available:

    • fail - issue mockify.exc.UninterestedCall exception on each unexpectedly called mock (default)
    • ignore - do nothing with uninterested calls
    • warn - issue a warning on each uninterested call

    New in version 0.4.

__call__(call)

Call a mock.

When this method is called, registry performs a lookup of matching unsatisfied expectations and calls first expectation found. If there are no matching expectation, then mockify.exc.UninterestedCall exception is raised. If there are matching expectations but all are satisfied, then last is called (making it oversaturated).

Parameters:call – Instance of mockify.engine.Call class representing mock being called
assert_satisfied(*names)

Assert that all expectations are satisfied.

If there is at least one unsatisfied expectation, then this method will raise mockify.exc.Unsatisfied exception containing list of failed expectations.

This method can be called as many times as you want.

Changed in version 0.2: Accepts names of mocks to check as positional args. If one or more names are given, then this method limits checking only to mocks of matching names.

expect_call(call, filename, lineno)

Register expectation.

Returns instance of expectation_class (usually Expectation) representing newly created expectation.

Parameters:
  • call – Instance of mockify.engine.Call class representing exact mock call or a pattern (if created with matchers) that is expected to be executed
  • filename – Path to file were expectation is created
  • lineno – Line number (inside filename) where expectation is created
mockify.assert_satisfied(*subjects)

Context manager for verifying multiple subjects at once.

Each passed subject must have assert_satisfied method defined, so it can be used with mockify.mock.Function or mockify.engine.Registry instances for example.

Basically, the role of this helper is to ensure that all subjects become satisfied after leaving wrapped context. For example:

>>> from mockify.mock import Function
>>> foo = Function('foo')
>>> bar = Function('bar')
>>> foo.expect_call()
<mockify.Expectation: foo()>
>>> bar.expect_call().times(0)
<mockify.Expectation: bar()>
>>> with assert_satisfied(foo, bar):
...     foo()

And that’s it - you don’t have to explicitly check if foo and bar are satisfied, because the helper will do it for you. And also it emphasizes part of code that actually uses given mocks.