【发布时间】:2019-03-07 07:00:04
【问题描述】:
我想在模拟 Base 时为 Subclass 编写测试,因为 Base 来自外部库。鉴于我们修改了基类中的所有可调用对象,我该如何模拟它。
class SubClass(Base):
def __init__(self, *args, **argv):
super().__init__(*args, **argv)
for attr_name in Base.__dict__:
attr = getattr(self, attr_name)
if callable(attr):
setattr(self, attr_name, functools.partial(__class__.sanity_check, attr))
@classmethod
def sanity_check(func):
txt = func()
if 'banned_word' in txt:
raise Exception('Device has banned word on it!')
return txt
【问题讨论】:
-
模拟整个基类并不是一个好习惯。你真正想测试什么?
标签: python python-unittest python-unittest.mock