unittest.mock: asserting partial match for method argument
import mock
class AnyStringWith(str):
def __eq__(self, other):
return self in other
...
result = database.Query('complicated sql with an id: %s' % id)
database.Query.assert_called_once_with(AnyStringWith(id))
...
Preemptively requires a matching string
def arg_should_contain(x):
def wrapper(arg):
assert str(x) in arg, "'%s' does not contain '%s'" % (arg, x)
return wrapper
...
database.Query = arg_should_contain(id)
result = database.Query('complicated sql with an id: %s' % id)
UPDATE
Using libraries like callee
, you don't need to implement AnyStringWith
.
from callee import Contains
database.Query.assert_called_once_with(Contains(id))
https://callee.readthedocs.io/en/latest/reference/operators.html#callee.operators.Contains
You can just use unittest.mock.ANY
:)
from unittest.mock import Mock, ANY
def foo(some_string):
print(some_string)
foo = Mock()
foo("bla")
foo.assert_called_with(ANY)
As described here - https://docs.python.org/3/library/unittest.mock.html#any