Python's assert_called_with, is there a wildcard character?
If you're calling sendmail
with a named parameter subject
then it's better to check whether the named argument matches what you expect:
args, kwargs = self.myclass.sendmail.call_args
self.assertEqual(kwargs['subject'], "Hello World")
This does assume both implementations of sendmail
have a named parameter called subject
. If that's not the case you can do the same with a positional parameter:
args, kwargs = self.myclass.sendmail.call_args
self.assertTrue("Hello World" in args)
You can be explicit about the position of the argument (i.e., the first argument or the third argument that's passed to sendmail
but that depends on the implementation of sendmail
being tested).
The python libraries do not have a default wild card implementation. But it's pretty simple to implement.
class AnyArg(object):
def __eq__(a, b):
return True
Then with AnyArg
, wild cards are possible with assert_called_with
:
self.myclass.sendmail.assert_called_with(
subject="Hello World",
mail_from=AnyArg(),
mail_to=AnyArg(),
body=AnyArg(),
format=AnyArg(),
)