我与非预定数量内它的迭代耗时的方法,我需要测试:蟒/ django的单元测试功能倍率
def testmethod():
objects = get_objects()
for objects in objects:
# Time-consuming iterations
do_something(object)
一次迭代是足以测试我。仅使用一次迭代测试此方法的最佳做法是什么?
我与非预定数量内它的迭代耗时的方法,我需要测试:蟒/ django的单元测试功能倍率
def testmethod():
objects = get_objects()
for objects in objects:
# Time-consuming iterations
do_something(object)
一次迭代是足以测试我。仅使用一次迭代测试此方法的最佳做法是什么?
也许把你方法引入
def my_method(self, objs=None):
if objs is None:
objs = get_objects()
for obj in objs:
do_something(obj)
然后在您的测试,你可以用自定义的OBJ参数调用它。
更新:
我误解了原来的问题,所以这里的我会怎样解决这个问题,而不改变源代码:
LAMBDA你的调用来获取对象返回一个对象。例如:
from your_module import get_objects
def test_testmethdod(self):
original_get_objects = get_objects
one_object = YourObject()
get_objects = lambda : [one_object,]
# ...
# your tests here
# ...
# Reset the original function, so it doesn't mess up other tests
get_objects = original_get_objects
谢谢。我会试试这个 – dragoon 2010-06-24 22:25:24