我在测试中遇到一个从函数中引发异常的问题:
### Implemetation
def MethodToTest():
myVar = StdObject()
try:
myVar.raiseError() # <--- here
return True
except Exception as e:
# ... code to test
return False
### Test file
@patch('stdLib.StdObject', autospec=True)
def test_MethodeToTest(self, mockedObjectConstructor):
mockedObj = mockedObjectConstructor.return_value
mockedObj.raiseError.side_effect = Exception('Test') # <--- do not work
ret = MethodToTest()
assert ret is False
我希望使用raiseError()
函数来引发错误。
我在SO上找到了几个示例,但没有符合我的需求。