如何在python单元测试中模拟连接错误并请求超时

时间:2014-01-02 15:10:14

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

假设我的django / flask应用程序从API中提取信息,我如何测试连接异常是否被捕获并正确处理?

例如,这里是一个调用API的函数:

import requests
def call_the_api():
    url = 'http://httpbin.org/get'
    try:
        req = requests.get(url)
        if req.json().get('errors'):
            logger.warn("API error response")
            return {'request_error': 'api_error_response'}
    except requests.exceptions.ConnectionError:
        logger.warn('ConnectionError')
        return {'request_error': 'ConnectionTimeout'}
    except requests.exception.Timeout:
        logger.warn('API request timed out')
        return {'request_error': 'Timeout'}
    except Exception, ex:
        logger.warn("API request Exception: %s", ex)
        return {'request_error': ex}
    else:
        return req.json()

为了测试来自API的响应,我发现mock非常有用。

def mock_get_request():
    response = requests.get.return_value
    json_file = 'sample_response.json'
    json_file_path = os.path.join(os.path.dirname(__file__), json_file)
    with open(json_file_path, 'r') as f:
        response.content = response.text = f.read()
    response.status_code = 200
    response.encoding = 'utf-8'
    response.json = lambda: json.loads(response.content.decode(response.encoding))
    response.url = u'%s' % args[0]
    return response

class TestSuitabilityFunctions(TestCase):
    def test_call_the_api(self):
        requests.get = MagicMock(side_effect=mock_get_request)
        resp = call_the_api()
        self.assertEqual(resp.get('url'), "http://httpbin.org/get")

所以我的问题是如何模拟连接超时或错误?

1 个答案:

答案 0 :(得分:2)

未经测试的代码,但......

def connection_error():
    raise requests.exceptions.ConnectionError

class TestSuitabilityFunctions(TestCase):
    @patch.object(module_that_youre_testing, "requests")
    def test_connection_error(self, mock_requests):
        mock_requests.get = MagicMock(side_effect=connection_error)
        with self.assertRaises(requests.exceptions.ConnectionError) as cm:
            resp = call_the_api()
        exception = cm.exception
        self.assertEqual(resp, {'request_error': 'ConnectionTimeout'})

...或类似的应该做的伎俩。我不记得assertRaises如何与捕获的错误进行交互。也许你甚至不需要assertRaises部分。