0

I have the file structure as follow:

In /packageA/__init__.py:

from my_mako_stuff import mako_lookup

...
def func_in_package():
    ...
    template = mako_lookup(...)

In /packageA/mas.py:

from . import func_in_package

...# do some stuff with `func_in_package`

In /packageA/mas_test.py:

from .mas import *

mocked_templatelookup = MagicMock(get_template=Mock())
with patch.object(.mas.parent, # this fails because the reference doesn't work
                  'mako_lookup',
                   new=mocked_templatelookup,
                   create=True):
  ...

How to properly reference the mako_lookup global variable defined in the packageA's __init__.py module, from the mas_test.py which itself is within that package?

EDITTING NOTES: As requested, I run this code as is and get a SyntaxError as follow:

File "mas_test.py", line 57
    with patch.object(.mas.parent, # this fails because the reference doesn't work
                      ^
SyntaxError: invalid syntax
Jinghui Niu
  • 990
  • 11
  • 28

1 Answers1

0

Just patch the name packageA.mako_lookup, since that is what func_in_package uses.

with patch('packageA.mako_lookup', new=mocked_templatelookup, create=True):
    ...
chepner
  • 497,756
  • 71
  • 530
  • 681