写单元测试看看这个例子:为抽象类
class A:
def do_stuff(self):
# ...
some_var = self.helper_method()
# ...
def helper_method(self):
# This method must be implemented by subclass
raise NotImplementedError()
class B(A):
def helper_method(self):
# implementation for class B
class C(A):
def helper_method(self):
# implementation for class C
我的任务是编写单元测试A
,B
和C
类(特别是do_stuff
)。
但是,如果我不能直接使用它的某些方法,我该如何测试A
类? 我应该只测试B
和C
类(它的实现为helper_method
) 或者可能有测试Python中抽象类的常用方法?
使用具体的子类来测试它。 –
并检查覆盖范围以确保抽象类代码在测试期间被覆盖。 –