你如何测试一个函数抛出异常?
How do you test that a function throws an exception?
我在下面有一个函数,想编写一个单元测试来检查我的代码是否使用模拟库捕获 ConnectionError
def get_foo():
try:
return requests.get("http://www.bongani.com")
except requests.exceptions.ConnectionError:
print("Error")
我有:
import unittest
import mock
class MyTestCase(unittest.TestCase):
@mock.patch('requests.get')
def test_foo(self, mock_requests_get):
mock_requests_get.side_effect = requests.exceptions.ConnectionError()
with self.assertRaises(requests.exceptions.ConnectionError):
get_foo()
if __name__ == '__main__':
unittest.main()
我收到这个错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1305 , in patched
return func(*args, **keywargs)
File "<ipython-input-24-15266e4f708a>", , in test_foo
get_foo()
AssertionError: ConnectionError not raised
我想模拟 return requests.get("http://www.bongani.com")
行,以便它在调用时引发异常
确保修补正确的路径
@mock.patch('path.to.get_foo.requests.get')
您不想实例化异常。
mock_requests_get.side_effect = requests.exceptions.ConnectionError
异常不会冒出来(即它被抑制),因为你捕获了它,然后你没有重新引发它。反过来,您的单元测试将失败,因为您正在测试您的函数在抑制异常时引发异常。
您可以更改函数或函数的单元测试,具体取决于所需的行为。
假设测试正确,那么您的函数需要像这样重新引发异常:
def get_foo():
try:
return requests.get("http://www.bongani.com")
except requests.exceptions.ConnectionError:
print("Error")
raise # <---------------- add this line
另一种情况,功能是对的,测试是错的。您将测试修改为如下所示:
class MyTestCase(unittest.TestCase):
@mock.patch('requests.get')
def test_foo(self, mock_requests_get):
mock_requests_get.side_effect = requests.exceptions.ConnectionError()
self.assertIsNone(get_foo()) # <----- no more assertRaises
我在下面有一个函数,想编写一个单元测试来检查我的代码是否使用模拟库捕获 ConnectionError
def get_foo():
try:
return requests.get("http://www.bongani.com")
except requests.exceptions.ConnectionError:
print("Error")
我有:
import unittest
import mock
class MyTestCase(unittest.TestCase):
@mock.patch('requests.get')
def test_foo(self, mock_requests_get):
mock_requests_get.side_effect = requests.exceptions.ConnectionError()
with self.assertRaises(requests.exceptions.ConnectionError):
get_foo()
if __name__ == '__main__':
unittest.main()
我收到这个错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1305 , in patched
return func(*args, **keywargs)
File "<ipython-input-24-15266e4f708a>", , in test_foo
get_foo()
AssertionError: ConnectionError not raised
我想模拟 return requests.get("http://www.bongani.com")
行,以便它在调用时引发异常
确保修补正确的路径
@mock.patch('path.to.get_foo.requests.get')
您不想实例化异常。
mock_requests_get.side_effect = requests.exceptions.ConnectionError
异常不会冒出来(即它被抑制),因为你捕获了它,然后你没有重新引发它。反过来,您的单元测试将失败,因为您正在测试您的函数在抑制异常时引发异常。
您可以更改函数或函数的单元测试,具体取决于所需的行为。
假设测试正确,那么您的函数需要像这样重新引发异常:
def get_foo():
try:
return requests.get("http://www.bongani.com")
except requests.exceptions.ConnectionError:
print("Error")
raise # <---------------- add this line
另一种情况,功能是对的,测试是错的。您将测试修改为如下所示:
class MyTestCase(unittest.TestCase):
@mock.patch('requests.get')
def test_foo(self, mock_requests_get):
mock_requests_get.side_effect = requests.exceptions.ConnectionError()
self.assertIsNone(get_foo()) # <----- no more assertRaises