I am trying to patch/mock a method (AsyncHTTPClient.fetch) that is being called in two different places in my program: Firstly in tornado_openapi3.testing and secondly in my_file. The problem is that the method is being patched in the first location, which breaks the functionality of my tests.
my_file.py:
import tornado
class Handler(tornado.web.RequestHandler, ABC):
def initialize(self):
<some_code>
async def get(self, id):
<some_code>
client = AsyncHTTPClient()
response = await client.fetch(<some_path>)
<some_code>
test_handler.py:
from tornado_openapi3.testing import AsyncOpenAPITestCase
class HandlerTestCase(AsyncOpenAPITestCase):
def get_app(self) -> Application:
return <some_app>
def test_my_handler(self):
with patch.object(my_file.AsyncHTTPClient, 'fetch') as mock_fetch:
f = asyncio.Future()
f.set_result('some_result_for_testing')
mock_fetch.return_value = f
self.fetch(<some_path>)
From what I understood from various mocking tutorials (e.g. https://docs.python.org/3/library/unittest.mock.html), fetch should only be patched/mocked in my_file. How can I make sure that is the case?