我正在寻找测试抽象基类中定义的方法的方法/最佳实践。我能直接想到的一件事是对基类的所有具体子类执行测试,但有时这似乎有些过分。
考虑这个例子:
import abc
class Abstract(object):
__metaclass__ = abc.ABCMeta
@abc.abstractproperty
def id(self):
return
@abc.abstractmethod
def foo(self):
print "foo"
def bar(self):
print "bar"
是否可以测试bar
不做任何子类化?
在较新版本的 Python 中,您可以使用unittest.mock.patch() https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch
class MyAbcClassTest(unittest.TestCase):
@patch.multiple(MyAbcClass, __abstractmethods__=set())
def test(self):
self.instance = MyAbcClass() # Ha!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)