【发布时间】:2010-05-06 21:10:50
【问题描述】:
我已经执行了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() 方法实现的一部分,在调用不同方法之前和之后成员属性的状态应该是什么,我绝对有期望。我应该对这些“私有”属性做出断言吗?我不知道如何对这些方法进行单元测试。
另一种选择是我真的不应该测试这些。
【问题讨论】:
-
一些代码或伪代码将有助于理解您的问题。从您所写的内容来看,听起来您可能会从 unittest.TestCase.setUp() 方法中受益。 docs.python.org/library/unittest.html#unittest.TestCase.setUp
-
我已经使用您提供的代码编辑了您的问题。能否检查缩进是否正确,必要时重新编辑?
-
@ire_and_curses。谢谢,修改了一下。
标签: python unit-testing refactoring tdd