Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 3.x 如何在python中模拟sendgrid web api v.3方法_Python 3.x_Unit Testing_Mocking_Sendgrid - Fatal编程技术网

Python 3.x 如何在python中模拟sendgrid web api v.3方法

Python 3.x 如何在python中模拟sendgrid web api v.3方法,python-3.x,unit-testing,mocking,sendgrid,Python 3.x,Unit Testing,Mocking,Sendgrid,我正在尝试测试是否在不发送电子邮件的情况下调用了SendGrid方法。当我运行我的测试时,该方法没有被修补,而是运行发送电子邮件的原始方法。我不知道为什么我的补丁不起作用。此问题与SendGrid类似,但使用的是不同版本的SendGrid # api/Login/utils_test.py from .utils import send_email from unittest.mock import patch @patch('api.Login.utils.sg.client.mail.se

我正在尝试测试是否在不发送电子邮件的情况下调用了SendGrid方法。当我运行我的测试时,该方法没有被修补,而是运行发送电子邮件的原始方法。我不知道为什么我的补丁不起作用。此问题与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
时,我得到一个断言错误:

assert False
+  where False = <MagicMock name='post' id='4370000808'>.called
assert False
+其中False=。调用

我希望测试能够通过,不会有任何输出。

不是在Python方面,而是在SendGrid方面,您是否尝试过使用?

从SendGrid Python repo问题中找到了解决方法

将结束调用,因为该修补程序似乎不适用于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
另一种选择是与我们的。这将创建SendGrid API的本地模拟版本,以便您可以针对我们的任何端点进行测试

然后可以运行
prism run--mock--list--spechttps://raw.githubusercontent.com/sendgrid/sendgrid-oai/master/oai_stoplight.json
从您的命令行


要让Prism自动启动,请参见。

您可以编辑您的帖子,告诉我们您的确切期望吗?@gkubed edited,谢谢!我没有,但我认为我遇到的问题是该方法没有被修补。我怀疑这就是我在@patch中描述路径的方式。沙盒模式需要一个具有实际生产权限的API密钥。无法生成仅沙盒的API密钥是一件奇怪的事情。