如何在Python的请求库中模拟会话?

时间:2018-03-06 09:58:06

标签: python unit-testing testing mocking python-requests

我一直在试图弄清楚如何嘲笑Python的请求库会话但到目前为止还无法找到解决方案。这是我需要为其编写测试的common.py代码:

import requests

def request_url(method, url):
    return _request_url(method, url)

def _request_url(method, url):
    session = requests.session()
    adapter = TlsAdapter(ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1)
    session.mount("https://", adapter)
    return session.request(method, url)

1 个答案:

答案 0 :(得分:1)

只是模拟整个_request_url函数,而不是打扰会话对象。该函数所做的只是提供一个响应对象,模拟该函数并返回一个模拟响应对象

但是,如果您正在测试_request_url函数本身,那么只需模拟session名称;额外的调用将全部传递给mock。然后,您可以为mocked.return_value.rquest.return_value对象提供您选择的响应对象。

所以

from unittest import mock

with mock.patch('requests.session') as mock_session:
    session_instance = mock_session.return_value
    mock_response = session_instance.request.return_value

    response = _request_url('METHOD', 'some url')

    assert response is mock_response
    session_instance.mount.assert_called()
    session_instance.request.assert_called_with('METHOD', 'some url')

TestCase方法:

@mock.patch('requests.session')
def test_request_url(self,  mock_session):
    session_instance = mock_session.return_value
    mock_response = session_instance.request.return_value

    response = _request_url('METHOD', 'some url')

    self.assertTrue(response is mock_response)
    session_instance.mount.assert_called()
    session_instance.request.assert_called_with('METHOD', 'some url')

演示:

>>> from unittest import mock
>>> import requests, ssl
>>> class TlsAdapter:
...     # mocked adapter, just for illustration purposes
...     def __init__(self, *args, **kwargs): pass
...
>>> def _request_url(method, url):
...     session = requests.session()
...     adapter = TlsAdapter(ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1)
...     session.mount("https://", adapter)
...     return session.request(method, url)
...
>>> with mock.patch('requests.session') as mock_session:
...     session_instance = mock_session.return_value
...     mock_response = session_instance.request.return_value
...     response = _request_url('METHOD', 'some url')
...     assert response is mock_response
...     session_instance.mount.assert_called()
...     session_instance.request.assert_called_with('METHOD', 'some url')
...
>>> # nothing happened, because the test passed, no assertion errors were raised
...