I am unit-testing a function which uses variable declared by other function.
def first_fun():
global file_path
file_path = get_file_path()
.
.
def second_fun():
with open(file_path, "r") as flz:
.
.
How do I mock file_path while testing second_fun()? I tried this but doesn't work.
@patch.object(source_module, 'file_path')
def test_second_fun(self):
source_module.second_fun()
I keep on getting ... does not have the attribute 'file_path'
Two things to consider:
First:
Explicit is better than implicit
in this case, the file_path should be a function parameter.
Second: for your test to be an unit test, you should patch open(), not a path, because you shouldn't depend on a external ressource.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.