I am trying to mock a method used in a global variable's format method in a module. The file I want to test is as follows:
do_work.py
from foo import bar
LINK = "foobar.com"
def generate_foobar():
return "visit {}".format(bar(LINK))
FOOBAR = generate_foobar()
The bar function is defined as follows:
foo.py
def bar(link):
return "<{}>".format(link)
Test is defined as follows:
test_foobar.py
import pytest
from do_work import LINK, generate_foobar
def test_foobar(mocker):
mocker.patch("do_work.bar", return_value=LINK)
from do_work import FOOBAR
expected_value = "visit {}".format(LINK)
assert generate_foobar() == expected_value # <- this assert passes
assert FOOBAR == expected_value # <- this assert fails
Even after patching the function bar()
the assert with GLOBAL VARIABLE fails. I am not sure why the method used in global variable is not mocked. Is there a way to mock this? Or is it something we can't patch as the function is executed in its own scope then the variable is imported as static value?