使用Python模拟异常范围外的异常

时间:2017-12-09 16:42:22

标签: python python-2.7 unit-testing mocking

******#我想模仿超出Python类Exception范围的异常。下面是我的无功能代码。请帮助确定哪里出错了。 ******

来自模拟导入补丁

import unittest2

A(():

pass

B级(对象):

def a(self):
    return True  

def b(self):
    ***# This function runs without problem and created an error outside of class Exception***
    try:
        raise A()
    except Exception as ex:
        print type(ex).__name__
    except:
        print 'error outside of Exception is captured.'    

def c(self):
    # This is for test
    try:
        self.a()
    except Exception as ex:
        print type(ex).__name__
    except:
        print 'error outside of Exception is captured.'   

班级考试(unittest2)

def raise_error(self):
    raise A()

@patch('B.a')
def test_c(self, mock_a):
    ***# This does not function as B().b(), instead it throws an TypeError Exception. I need to mock an error not inherited from class Exception ***
    mock_a.side_effect = self.raise_error
    B().c()

0 个答案:

没有答案