I am fairly new to Python and I would like to make a test where I read some values from a test json file rather than making an expensive http call.
In my test.py
, I created a method with the same arguments and name as the method to be replaced. The method to be replaced is a top level method in task.py
:
#In task.py
def foo(arg1: str, arg2: str, arg3 = None, arg4 = None):
# do expensive stuff
response = {"error": "For the sake of example", "result": ""}
def do_something():
return foo("", "")
#In test.py
import task
def foo(arg1: str, arg2: str, arg3 = None, arg4 = None):
config_path = os.path.join(test_files_dir, "test.json")
with open(config_path, "rt") as f:
result_dict = json.loads(f.read())
response = {"error": "", "result": result_dict}
return response
def test_method():
task.foo = foo
response = task.do_something()
assert not response['error']
However step by step debug still enters the original method rather than the replaced one. What I am missing here ? If it worked, how can I make that change lasts for the duration of the test method only ?