Assume I have a function defined in a module:
module_a.py
def foo():
return 10
And I want to create an API to patch the function:
patcher.py
import mock
class Patcher(object):
def __enter__(self):
self.patcher = mock.patch('module_a.foo',
mock.Mock(return_value=15))
self.patcher.start()
def __exit__(self, *args):
self.patcher.stop()
The thing is, I don't know what is the name of the module that will use my API. so a test looking like this:
test1.py
from patcher import Patcher
import module_a
with Patcher():
assert module_a.foo() == 15
will work. But a test written like this:
test2.py
from patcher import Patcher
from module_a import foo
with Patcher():
assert foo() == 15
will Fail.
Is there anyway not making the API user to write it's tests and modules(!) like the first option?