Argument Matchers¶
mock_callable(), mock_async_callable() and mock_constructor() allow the definitions of what call arguments to accept by using .for_call()
. Eg:
self.mock_constructor(storage, 'Client')\
for_call(timeout=60)\
to_return_value(self.storage_mock)
This validation is strict: tests will work with Client(timeout=60)
but fail with Client(timeout=61)
. Perhaps letting tests pass with “any positive integer” would be enough. This is precisely what argument matchers allow us to do:
from testslide.matchers import IntGreaterThan
(...)
self.mock_constructor(storage, 'Client')\
for_call(timeout=IntGreaterThan(0))\
to_return_value(self.storage_mock)
This matches for Client(timeout=5)
, Client(timeout=60)
but not for Client(timeout=0)
or Client(timeout=-1)
.
Logic Operations¶
Argument matchers can be combined using bitwise operators:
# String containing "this" AND ending with "that"
StrContaining("this") & StrEndingWith("that")
# String containing "this" OR ending with "that"
StrContaining("this") | StrEndingWith("that")
# String containing "this" EXCLUSIVE OR ending with "that"
StrContaining("this") ^ StrEndingWith("that")
# String NOT containing "this"
~StrContaining("this")
Integers¶
Matcher |
Description |
---|---|
|
Any |
|
Any integer but the given value |
|
Integer |
|
Integer |
|
Integer |
|
Integer |
|
Integer |
Floats¶
Matcher |
Description |
---|---|
|
Any |
|
Any float but the given value |
|
Float |
|
Float |
|
Float |
|
Float |
|
Float |
Strings¶
Matcher |
Description |
---|---|
|
Any |
|
Any string that matches the regular expression compiled by |
|
A string which contains |
|
A string that starts with |
|
A string that ends with |
Lists¶
Matcher |
Description |
---|---|
|
Any |
|
Any list containing |
|
Any list which contains every element of |
|
A list which has at least one element |
|
An empty list: |
Dictionaries¶
Matcher |
Description |
---|---|
|
Any |
|
A dictionary with any at least one key |
|
An empty dictionary: |
|
A dictionary containing all keys from |
|
A dictionary containing all key / value pairs from |
Collections¶
Matcher |
Description |
---|---|
|
A container that contains |
|
A container that contains every element of |
|
Any iterable |
|
An iterable containing all the elements in |
|
An object where |
|
An object where |
Generic¶
Matcher |
Description |
---|---|
|
Any object |
|
Any object where |
|
Any object where |
|
Any object where |
|
Any object where |
self.mock_callable(os, 'remove')\
.for_call(AnyWithCall(lambda path: path.endswith("py"))\
.to_return_value(None)\
.and_assert_called_once()