我已经完成了
Beck所描述的“
Replace Method with Method Object”重构.
现在,我有一个带有“run()”方法的类和一堆成员函数,它们将计算分解为更小的单元.如何测试这些成员函数?
我的第一个想法是我的单元测试基本上是“run()”方法的副本(具有不同的初始化),但是在每次调用成员函数之间使用断言来检查计算的状态.
(我正在使用Python和unittest模块.)
class Train:
def __init__(self, options, points):
self._options = options
self._points = points
# other initializations
def run(self):
self._setup_mappings_dict()
self._setup_train_and_estimation_sets()
if self._options.estimate_method == 'per_class':
self._setup_priors()
self._estimate_all_mappings()
self._save_mappings()
def _estimate_all_mappings():
# implementation, calls to methods in this class
#other method definitions
作为run()方法实现的一部分,我绝对期望在调用不同方法之前和之后成员属性的状态应该是什么.我应该对这些“私人”属性做出断言吗?我不知道如何对这些方法进行单元测试.
另一种选择是我真的不应该测试这些.