正确的mock.patch smtplib.SMTP方法

use*_*028 7 python mocking smtplib

smtplib.SMTP.sendmail尝试在单元测试中模拟.修补调用。该sendmail方法似乎已成功模拟,我们可以将其查询为MagicMock,但sendmail 模拟的calledcalled_args属性未正确更新。看来我没有正确应用补丁。

这是我正在尝试的一个简化示例:

import unittest.mock
with unittest.mock.patch('smtplib.SMTP', autospec=True) as mock:
    import smtplib
    smtp = smtplib.SMTP('localhost')
    smtp.sendmail('me', 'me', 'hello world\n')
    mock.assert_called()           # <--- this succeeds
    mock.sendmail.assert_called()  # <--- this fails
Run Code Online (Sandbox Code Playgroud)

此示例生成:

AssertionError: Expected 'sendmail' to have been called.
Run Code Online (Sandbox Code Playgroud)

如果我将补丁更改为smtp.SMTP.sendmail;例如:

with unittest.mock.patch('smtplib.SMTP.sendmail.', autospec=True) as mock:
    ...
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我可以成功访问模拟的called_args和属性,但由于允许进行初始化,因此与主机建立了实际的 smtp 会话。这是单元测试,我不希望发生实际的网络。calledsmtplib.SMTP

小智 12

我今天遇到了同样的问题,忘记了我正在使用上下文,所以只需更改

mock.sendmail.assert_called()
Run Code Online (Sandbox Code Playgroud)

mock.return_value.__enter__.return_value.sendmail.assert_called()
Run Code Online (Sandbox Code Playgroud)

这看起来很混乱,但这是我的例子:

msg = EmailMessage()
msg['From'] = 'no@no.com'
msg['To'] = 'no@no.com'
msg['Subject'] = 'subject'
msg.set_content('content');

with patch('smtplib.SMTP', autospec=True) as mock_smtp:
    misc.send_email(msg)

    mock_smtp.assert_called()

    context = mock_smtp.return_value.__enter__.return_value
    context.ehlo.assert_called()
    context.starttls.assert_called()
    context.login.assert_called()
    context.send_message.assert_called_with(msg)
Run Code Online (Sandbox Code Playgroud)