Python IOError异常处理的Unittest
鉴于此代码:Python IOError异常处理的Unittest,python,unit-testing,mocking,ioerror,Python,Unit Testing,Mocking,Ioerror,鉴于此代码: try: #do something except IOError as message: logging.error(message) raise message 我想测试异常处理部分,以便全面覆盖。 在我尝试过的单元测试中: with patch(new=Mock(side_effect=IOError(errno.EIO))): self.assertRaises(IOError)
try:
#do something
except IOError as message:
logging.error(message)
raise message
我想测试异常处理部分,以便全面覆盖。
在我尝试过的单元测试中:
with patch(new=Mock(side_effect=IOError(errno.EIO))):
self.assertRaises(IOError)
但它不起作用。
这种方法正确吗?实际上,您需要启动模拟,以便
副作用开始,例如:
class Test(unittest.TestCase):
def test(self):
mock = m.Mock()
mock.side_effect = Exception("Big badaboum")
self.assertRaises(Exception, mock)
import unittest.mock as m
import unittest
def raise_error():
try:
print("Hello") #placeholder for the try clause
except Exception as e:
print(e) #placeholder for the exceptclause
class Test(unittest.TestCase):
@m.patch("__main__.raise_error", side_effect=Exception("Big badaboum")) #replace __main__ by the name of the module with your function
def test(self, mock):
with self.assertRaises(Exception):
mock()
unittest.main()
self.assertRaises
可以将callable作为第二个参数,使其等效于:
class Test(unittest.TestCase):
def test(self):
mock = m.Mock()
mock.side_effect = Exception("Big badaboum")
with self.assertRaises(Exception):
mock()
如果您想在补丁测试中使用它,可以执行以下操作:
class Test(unittest.TestCase):
def test(self):
mock = m.Mock()
mock.side_effect = Exception("Big badaboum")
self.assertRaises(Exception, mock)
import unittest.mock as m
import unittest
def raise_error():
try:
print("Hello") #placeholder for the try clause
except Exception as e:
print(e) #placeholder for the exceptclause
class Test(unittest.TestCase):
@m.patch("__main__.raise_error", side_effect=Exception("Big badaboum")) #replace __main__ by the name of the module with your function
def test(self, mock):
with self.assertRaises(Exception):
mock()
unittest.main()
编辑:要测试except块内错误的引发,您需要在您编写的try块内模拟函数调用,例如:
import unittest.mock as m
import unittest
def do_sthing():
print("Hello")
def raise_error():
try:
do_sthing() #this call can be mocked to raise an IOError
except IOError as e:
print(e.strerror)
raise ValueError("Another one")
class Test(unittest.TestCase):
def test(self):
with m.patch("__main__.do_sthing", side_effect=IOError("IOError")):
self.assertRaises(ValueError, raise_error)
unittest.main()
您也可以使用decorator语法(只需将上面的测试重写以节省一些CPU周期):
你好,asettouf,谢谢你的回复。我试过了,虽然看起来测试通过了,但代码覆盖率显示try和except块都没有测试。我想我遗漏了一些东西。@Giuseppe“代码覆盖率”,哪种?另外,您是否有针对何时未引发异常的测试?到目前为止,我使用self.assertRaises(IOError,myClass.myFunction)测试了何时未引发异常。它是有效的,但当然它失败了,因为它是虚拟的,所以不会引发异常。现在我被要求有一个完整的覆盖和测试也除了块。为此,我需要模拟一个IOError@Giuseppe我的错误,我之前的评论可能让人困惑,您要做的是在try块内模拟调用以引发IOError
,然后self.assertRaises(SomeException,call)
。我编辑了我的帖子以获取更多信息clarity@Giuseppe就本例而言,这是一个随机选择。我们可以将其替换为IOError
,并获得相同的结果