如何在 python 中模拟 sendgrid web api v.3 方法
How to mock a sendgrid web api v.3 method in python
我正在尝试测试是否在未发送电子邮件的情况下调用了 SendGrid 方法。当我 运行 我的测试时,该方法没有打补丁,而是 运行 发送电子邮件的原始方法。我不确定为什么我的补丁不起作用。此题与 类似,但使用不同版本的 SendGrid。
# api/Login/utils_test.py
from .utils import send_email
from unittest.mock import patch
@patch('api.Login.utils.sg.client.mail.send.post')
def test_send_email(mock_mail_send):
send_email(email, subject, html)
assert mock_mail_send.called
# api/Login/utils.py
from api import sg
def send_email(email, subject, html):
msg = create_email(email, subject, html)
request_body = msg.get()
response = sg.client.mail.send.post(request_body=request_body)
# api/__init__.py
from server import sg
# server.py
import sendgrid
import os
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
当前,当我从 api 目录中 运行 pytest Login/utils_test.py
时,我得到一个 AssertionError:
assert False
+ where False = <MagicMock name='post' id='4370000808'>.called
我希望测试通过但没有输出。
不是在 Python 端,而是在 SendGrid 端,您尝试过使用 sandbox mode 吗?
从 sendgrid-python 回购问题中找到解决方法 https://github.com/sendgrid/sendgrid-python/issues/293
将结束通话,因为补丁似乎无法与 SendGrid web api v.3 一起使用,而且看起来他们不会支持它。
更新如下:
# api/utils.py
def send_email(email, subject, html):
msg = create_email(email, subject, html)
request_body = msg.get()
response = _send_email(request_body)
print(response.status_code)
print(response.body)
print(response.headers)
def _send_email(request_body):
"""Wrapping the SendGrid mail sending method in order to patch it
while testing. https://github.com/sendgrid/sendgrid-
python/issues/293"""
response = sg.client.mail.send.post(request_body=request_body)
return response
# api/utils_test.py
@patch('api.Login.utils._send_email')
def test_send_email(mock_mail_send):
send_email(email, subject, html)
assert mock_mail_send.called
另一种选择是使用 Prism in conjunction with our Open API definition。这将创建 SendGrid API 的本地模拟版本,因此您可以针对我们的任何端点进行测试。
然后您可以从命令行 运行 prism run --mock --list --spec https://raw.githubusercontent.com/sendgrid/sendgrid-oai/master/oai_stoplight.json
。
要让 Prism 自动启动,请参阅 this example。
我正在尝试测试是否在未发送电子邮件的情况下调用了 SendGrid 方法。当我 运行 我的测试时,该方法没有打补丁,而是 运行 发送电子邮件的原始方法。我不确定为什么我的补丁不起作用。此题与
# api/Login/utils_test.py
from .utils import send_email
from unittest.mock import patch
@patch('api.Login.utils.sg.client.mail.send.post')
def test_send_email(mock_mail_send):
send_email(email, subject, html)
assert mock_mail_send.called
# api/Login/utils.py
from api import sg
def send_email(email, subject, html):
msg = create_email(email, subject, html)
request_body = msg.get()
response = sg.client.mail.send.post(request_body=request_body)
# api/__init__.py
from server import sg
# server.py
import sendgrid
import os
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
当前,当我从 api 目录中 运行 pytest Login/utils_test.py
时,我得到一个 AssertionError:
assert False
+ where False = <MagicMock name='post' id='4370000808'>.called
我希望测试通过但没有输出。
不是在 Python 端,而是在 SendGrid 端,您尝试过使用 sandbox mode 吗?
从 sendgrid-python 回购问题中找到解决方法 https://github.com/sendgrid/sendgrid-python/issues/293
将结束通话,因为补丁似乎无法与 SendGrid web api v.3 一起使用,而且看起来他们不会支持它。
更新如下:
# api/utils.py
def send_email(email, subject, html):
msg = create_email(email, subject, html)
request_body = msg.get()
response = _send_email(request_body)
print(response.status_code)
print(response.body)
print(response.headers)
def _send_email(request_body):
"""Wrapping the SendGrid mail sending method in order to patch it
while testing. https://github.com/sendgrid/sendgrid-
python/issues/293"""
response = sg.client.mail.send.post(request_body=request_body)
return response
# api/utils_test.py
@patch('api.Login.utils._send_email')
def test_send_email(mock_mail_send):
send_email(email, subject, html)
assert mock_mail_send.called
另一种选择是使用 Prism in conjunction with our Open API definition。这将创建 SendGrid API 的本地模拟版本,因此您可以针对我们的任何端点进行测试。
然后您可以从命令行 运行 prism run --mock --list --spec https://raw.githubusercontent.com/sendgrid/sendgrid-oai/master/oai_stoplight.json
。
要让 Prism 自动启动,请参阅 this example。