这其实是与语言无关的。但我会以Python为例,给你提供一些背景。
我有一个父类:
class Mammal(object):
def __init__(self):
""" do some work """
def eat(self, food):
"""Eat the food"""
way_to_eat = self._eating_method()
self._consume(food)
def _eating_method(self):
"""Template method"""
def _consume(self, food):
"""Template method"""
这里eat
是唯一的公共方法,而_consume
和_eating_method
实际上是受保护的方法,将由子类实现。
当你只编写了Mammal
类时,你会测试什么?
显然测试所有4个方法。
现在让我们介绍一个子类。
class Tiger(Mammal):
def _eating_method(self):
"""Template method"""
def _consume(self, food):
"""Template method"""
看这个类,它只有2个受保护的方法。
我应该测试Tiger
的所有4个方法(包括继承的2个方法)还是只测试引入变更的部分(仅覆盖2个重写的方法)?
理想情况是什么?
eat
方法会测试每种可能的_eating_method
和_consume
结果。因此只需要测试更改过的方法即可。 - Daniel