我所做的:
我有一个函数def get_holidays():,它引发了一个Timeout错误。我的测试函数test_get_holidays_raises_ioerror():首先设置requests.get.side_effect = IOError,然后使用pytest.raises(IOError)来断言该函数是否引发了{}。在
问题是什么:
理想情况下,这应该失败,因为我实际的get_holidays()不会引发IOError。但测试通过了。在
可能原因:
这可能是因为Timeout是从IOError类继承的。在
我想要什么:
如果IOError被引发,希望特别声明。在
代码:from mock import Mock
import requests
from requests import Timeout
import pytest
requests = Mock()
# Actual function to test
def get_holidays():
try:
r = requests.get('http://localhost/api/holidays')
if r.status_code == 200:
return r.json()
except Timeout:
raise Timeout
return None
# Actual function that tests the above function
def test_get_holidays_raises_ioerror():
requests.get.side_effect = IOError
with pytest.raises(IOError):
get_holidays()