Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何测试模拟方法的第二个参数?_Python_Unit Testing_Python Mock - Fatal编程技术网

Python 如何测试模拟方法的第二个参数?

Python 如何测试模拟方法的第二个参数?,python,unit-testing,python-mock,Python,Unit Testing,Python Mock,我试图模拟sendmails()方法,并想测试是否使用test@test.com“电子邮件地址 @mock.patch('apps.dbank.management.commands.optin_invites.OptinBase.sendEmails') def test_x_send_emails(self, send_emails_mock): oi = OptinInvitesX() oi.compute(True, "test@test.com") self.asse

我试图模拟
sendmails()
方法,并想测试是否使用test@test.com“电子邮件地址

@mock.patch('apps.dbank.management.commands.optin_invites.OptinBase.sendEmails')
def test_x_send_emails(self, send_emails_mock):
   oi = OptinInvitesX()
   oi.compute(True, "test@test.com")
   self.assertTrue(send_emails_mock.assert_called_with(???, test_email_address="test@test.com"))

我可以使用
assert\u调用_with
,但是我不关心这个测试用例的第一个参数。有没有一种方法可以说第一个参数接受任何东西?

您描述的是以下参数的基本用法:

有时,您可能需要对mock调用中的某些参数进行断言,但要么不关心某些参数,要么希望将它们分别从
call_args
中提取出来,并对它们进行更复杂的断言

若要忽略某些参数,可以传入比较等于所有参数的对象。调用
assert\u调用了\u with()
assert\u调用了\u一次\u with()
将成功,无论传入了什么

因此,在您的情况下,您可以使用:

# only asserting on 'test_email_address' argument:
send_emails_mock.assert_called_with(mock.ANY, test_email_address="test@test.com")
请注意,您实际上并不想在该行中使用
self.assertTrue
。mock方法是它自己的断言