API¶
Double classes¶
-
class
Stub
([collaborator])¶
-
class
Spy
([collaborator])¶
-
class
ProxySpy
([collaborator])¶
-
class
Mock
([collaborator])¶
-
class
Mimic
(double, collaborator)¶
Stubbing¶
-
Method.
raises
(exception)¶ Stub method will raise the given exception when invoked. Method parameters are relevant, and they may be literal values or hamcrest matchers:
with Stub() as stub: stub.method().raises(ValueError)
-
Method.
returns
(value)¶ Stub method will return the given value when invoked:
with Stub() as stub: stub.method().returns(100)
See Stub.
-
Method.
returns_input
()¶ Stub method will return input parameters when invoked:
with Stub() as stub: stub.method().returns_input()
-
Method.
delegates
(delegate)¶ Stub method will return values generated by the delegate, that may be a function, generator or iterable object:
with Stub() as stub: stub.method().delegates([1, 2, 4, 8])
See Stub delegates.
-
Method.
attach
(callable)¶ Stub methods are observable. You may attach arbitrary callable that will be invoked any time the stub method does:
counter = itertools.count() stub.method.attach(counter.next)
See Stub observers.
Matchers¶
for Spy methods¶
-
called.
async_mode
(timeout)¶ The
called
assertion waits the corresponding invocation a maximum of timeout seconds.Parameters: timeout (int) – how many second wait before assume assertion fails. assert_that(spy.method, called().async_mode(1))
See async_mode.
-
called.
times
(value)¶ The spy method must be invoked value times to consider the assertion right. The value parameter may an integer or hamcrest matcher as well.
Parameters: value (int or hamcrest Matcher) – how many times the method should be called. assert_that(spy.method, called().times(less_that(3)))
-
called.
with_args
(*args, **kargs)¶ The spy method must be invoked with the given positional or/and named parameters. All of them may be literal values and hamcrest matchers.
assert_that(spy.method, called().with_args("mary", greater_that(4)))
-
called.
with_some_args
(**kargs)¶ The spy method must be invoked with AT LEAST the given parameter values. It supports literal values and hamcrest matchers.
assert_that(spy.method, called().with_some_args(name="mary"))
Module level functions¶
-
assert_that
(item, matcher)¶ A convenient replace for the hamcrest assert_that method. See assert_that().
-
wait_that
(item, matcher, reason='', delta=1, timeout=5)¶ It test the matcher over item until it matches or fails after timemout seconds, polling the matcher each delta seconds.
-
method_returning
(value)¶ Creates an independent Stub method that returns the given value. It may be added to any object:
some.method = method_returning(20)
See Ad-hoc stub methods.
-
method_raising
()¶ Creates an independent Stub method that raises the given exception. It may be added to any object:
some.method = method_raising(ValueError)
See Ad-hoc stub methods.
-
set_default_behavior
()¶ Set the default behavior for undefined Stub methods. The built-in behavior is to return None. See Changing default stub behavior.