Python的assert_called_with,是否有通配符?

时间:2013-12-06 16:23:24

标签: python testing mocking assert

假设我在python中设置了这样的类。

from somewhere import sendmail

class MyClass:

    def __init__(self, **kargs):
        self.sendmail = kwargs.get("sendmail", sendmail)  #if we can't find it, use imported def

    def publish():

        #lots of irrelevant code
        #and then

        self.sendmail(mail_to, mail_from, subject, body, format= 'html')

所以你可以看到,我有点自己选择参数化我用于self.sendmail的函数

现在在测试文件中。

Class Tester():

    kwargs = {"sendmail": MagicMock(mail_from= None, mail_to= None, subject= None, body= None, format= None)}
    self.myclass = MyClass(**kwargs)

    ##later on
    def testDefaultEmailHeader():

        default_subject = "Hello World"
        self.myclass.publish()

        self.myclass.sendmail.assert_called()  #this is doing just fine
        self.myclass.sendmail.assert_called_with(default_subject)  #this is having issues

出于某种原因,我收到错误消息

AssertionError: Expected call: mock('Hello World')
                Actual Call : mock('defaultmt', 'defaultmf', 'Hello World', 'default_body', format= 'html')

所以基本上,断言期望sendmail只用一个变量调用,当它最终用全5调用时。问题是,我不关心其他4个变量是什么!我只是想确保用正确的主题调用它。

我尝试了模拟占位符ANY,并得到了相同的东西

self.myclass.sendmail.assert_called_with(ANY, ANY, 'Hello World', ANY, ANY)

AssertionError: Expected call: mock(<ANY>, <ANY>, 'Hello World', <ANY>, <ANY>)
Actual Call : mock('defaultmt', 'defaultmf', 'Hello World', 'default_body, 'format= 'html') 

真的不确定如何继续这个。如果我们只关心其中一个变量并且想忽略其余变量,那么任何人都有任何建议吗?

2 个答案:

答案 0 :(得分:34)

如果您使用命名参数sendmail调用subject,那么最好检查命名参数是否符合您的预期:

args, kwargs = self.myclass.sendmail.call_args
self.assertEqual(kwargs['subject'], "Hello World")

这确实假设sendmail的两个实现都有一个名为subject的命名参数。如果不是这种情况,您可以使用位置参数执行相同的操作:

args, kwargs = self.myclass.sendmail.call_args
self.assertTrue("Hello World" in args)

你可以明确说明参数的位置(即传递给sendmail的第一个参数或第三个参数,但这取决于被测试的sendmail的实现。)

答案 1 :(得分:1)

python库没有默认的通配符实现。但这很容易实现。

class AnyArg(object):
    def __eq__(a, b):
        return True

然后使用AnyArg,可以使用assert_called_with使用通配符:

self.myclass.sendmail.assert_called_with(
    subject="Hello World",
    mail_from=AnyArg(),
    mail_to=AnyArg(),
    body=AnyArg(),
    format=AnyArg(),
)