我想使用monkey补丁来更改所有语句,比如time.sleep(5)
到myServer.sleep(5)
。我怎么能意识到呢?非常感谢你!你知道吗
import test
import subprocess
import ast
import os
import time
import sys
if __name__ == "__main__":
def insomniac(duration):
pass # don't sleep
_original_sleep = time.sleep
time.sleep = insomniac
def dont_write(stuff):
pass # don't write
_original_write = sys.stdout.write
sys.stdout.write = dont_write
execfile("test.py")
exit(0)
如果您的重点是测试,也就是说,您希望确保以特定的方式调用某个外部库中超出您控制范围的可调用项,那么有一些测试库可以帮助您做到这一点,例如^{} 。正确地设置所有这些东西是相当复杂的,所以当我进行编码时,我通常会尝试以一种最小化模拟使用的方式来构造代码。你知道吗
如果你真的想把脚趾朝这个方向弄湿的话,看看documentation for ^{} 。在这种情况下,您可能应该将测试技术形式化,以利用^{} 。如果你迷路了,Writing unit tests in Python: How do I start?。你知道吗
相关问题 更多 >
编程相关推荐