带有__init__属性的Python模型规范

时间:2017-08-29 00:42:42

标签: python python-2.7 unit-testing

使用mock.patch在Python中进行测试时,如果调用了类中没有的属性,则可以使用spec来确保测试失败。但是这些不允许您使用__init__中创建的属性:

class A(object):        
  def __init__(self):                                                                                  
    self.init_prop = 4

  def regular_prop():
    pass

with patch('__main__.A', spec=True) as A_mock:
   # accessing regular_prop on an instance of A_mock succeeds
   # accessing init_prop on an instance of A_mock raises an exception

有没有办法让patch或其他工具自动捕获__init__中创建的属性?

0 个答案:

没有答案
相关问题