我试图编写一个测试,当试图打开一个文件读取时,通过调用open()
嘲讽PermissionError
。不过,我似乎无法得到测试工作。 PermissionError
似乎被抛出,但我的测试失败,因为这一点,即使我试图断言它被抛出。Assert当嘲笑python3时抛出错误
下面包含我的尝试之一:
fileMethods.py
def readfile(myfile):
with open(myfile, 'r') as file:
filecontent = file.read()
file.close()
return filecontent
fileMethods_test.py
def test_readfile_throws_PermissionError(self):
with mock.patch('fileMethods.open') as openMock:
openMock.side_effect = PermissionError
self.assertRaises(PermissionError, fileMethods.readfile('file_to_readin'))
我失去了一些东西明显或我的方式测试此方法不正确?
要么用'with'打开文件,要么手动关闭它。 –