【发布时间】:2011-10-24 17:23:07
【问题描述】:
我使用 Python 的模拟框架进行测试 - 效果很好!
但是,我无法弄清楚的一件事是如何修补一个函数,以便我将调用替换为另一个函数。
例子:
# module_A.py
def original_func(arg_a,arg_b):
# ...
# module_B.py
import module_A
def func_under_test():
# ...
module_A.original_func(a,b)
# Some code that depends on the behavior of the patched function
# ...
# my test code
def alternative_func(arg_a,arg_b):
# do something essential for the test
def the_test():
# patch the original_func with the alternative_func here
func_under_test()
# assertions
通常断言就足够了,但在这种情况下,我需要 alternative_func 在调用它时而不是 original_func。
还要注意alternative_func 需要相同的参数。
我敢肯定这非常容易,而且可能时间已经很晚了,但我就是没看到...
【问题讨论】:
标签: python unit-testing patch