Note: (all of these classes and methods are available from the toplevel mocktest module, despite the use of fully-qualified names here)
A subclass of unittest.TestCase with the following additions:
Fail the test if an object does not satisfy the given matcher.
Enhanced assertRaises, able to: - check arguments (args) - check keyword arguments (kwargs) - match a regular expression on the resulting expression message (matching) - compare message strings (message)
Enhanced assertRaises, able to: - check arguments (args) - check keyword arguments (kwargs) - match a regular expression on the resulting expression message (matching) - compare message strings (message)
This is (mostly) the default implementation of unittest.run the only modification is that a self.FailureException raised in the teardown method counts for a failure
Generate a subclass inheriting only private methods and setUp/tearDown, for the purpose of inheriting test setup but not any actual test implementations
Replace a method on an object. Just like expect, except that no verification against the number of calls received will be performed.
Return type: | MockAct |
---|
Add an expectation to a method of obj. By default, the method is expected at least once. E.g:
>>> expect(some_object).method
Return type: | MockAct |
---|
Make a mock object.
Parameters: |
|
---|---|
Return type: |
Replace children of an existing object for the duration of this test. E.g:
>>> modify(obj).child = replacement_child
>>> modify(obj).grand.child = replacement_grand_child
>>> modify(obj)['item'] = replacement_item
All replaced attributes / items will be reverted when the test completes.
Return type: | RecursiveAssignmentWrapper |
---|
a named object
These classes are not exposed for you to instantiate, but instances are returned instances from other (public) methods:
The return value from mock().
An object that returns new instances of itself when attributes are accessed (unless create_unknown_children is False).
Returns None (and saves the call information) when called
Set children via kwargs, e.g.:
>>> mock("name").with_children(x=1, y='child y')
>>> obj.x
1
>>> obj.y
'child y'
Set child methods via kwargs, e.g.:
>>> mock("name").with_methods(x=1, y=mock('child y'))
>>> obj.x()
1
>>> obj.y()
'child y'
The return value from modify().
Assigning a value to an attribute of this object assigns the same value to the original object, but only for the duration of the current test.
The same goes for items (i.e dictionary-style access).
Getting an attribute or item from this object replaces that item on the original object with a wrapped version that always supports assignment of attributes / items. This is useful e.g to replace readonly attributes on nested objects:
# does NOT work, as write is not assignable >>> modify(sys.stderr).write = my_write_func
# but we can work around it, by replacing the whole stderr # object with one that has all the values of the real stderr # but allowing us to override of any attribute: >>> modify(sys).stderr.write = my_write_func
Set children via kwargs, e.g.:
>>> modify(obj).children(x=1, y='child y')
>>> obj.x
1
>>> obj.y
'child y'
Copy all non-special attributes of other, setting the value of each child to value. The default value paramater is a function returning None for all arguments.
Set child methods via kwargs, e.g.:
>>> modify(obj).methods(x=1, y=mock('child y'))
The return type from when() and expect().
Set the conditions under which this act will apply. Arguments can be normal objects (checked for equality), or Matcher instances.
When this act matches, call the given func and return its value.
When this act matches, raise the given exception instance.
When this act matches, return the given result to the caller. If provided with multiple arguments, those values will be returned in order. e.g:
>>> expect(obj).foo().and_return(True, True, False)
>>> obj.foo()
True
>>> obj.foo()
True
>>> obj.foo()
False
Once all return values have been used up, any further calls with throw a MockError.
Note: and_raise(), and_return() and and_call() each have a then_* alias for better readability when using when(). e.g:
>>> expect(obj).foo().and_return(True)
Is readable, however when using when(), the following is more readable:
>>> when(obj).foo().then_return(True)
Both and_ and then_ versions have the same effect however.
Expect this act to match at least number times.
Expect this act to match at most number times.
Expect this act to match between lower and upper times.
Expect this act to be triggered exactly number times. Usually followed by times() for readability, as in:
>>> expect(obj).meth.exactly(3).times()
Alias for exactly(0).times
Alias for exactly(1).times
When this act matches, call the given func and return its value.
When this act matches, raise the given exception instance.
When this act matches, return the given result to the caller. If provided with multiple arguments, those values will be returned in order. e.g:
>>> expect(obj).foo().and_return(True, True, False)
>>> obj.foo()
True
>>> obj.foo()
True
>>> obj.foo()
False
Once all return values have been used up, any further calls with throw a MockError.
Note: and_raise(), and_return() and and_call() each have a then_* alias for better readability when using when(). e.g:
>>> expect(obj).foo().and_return(True)
Is readable, however when using when(), the following is more readable:
>>> when(obj).foo().then_return(True)
Both and_ and then_ versions have the same effect however.
Alias for exactly(3).times
Alias for exactly(2).times
Match only when condition_func returns true, when called with the same arguments as this method.
>>> when(obj).method().then_return(True)
Will set obj.method to an instance of StubbedMethod (for the duration of the current test)
The set of calls this stub has received, as a list of Call instances.
An encapsulation of call arguments. Can compare for equality with a tuple of (args, kwargs), e.g:
>>> Call.like(1,2,3, x=4) == ((1,2,3), {'x':4})
True
Parameters: |
|
---|---|
Members : | args, kwargs |
capture a call with the given arguments
apply this call’s arguments to the given callable