Reputation: 11677
I have a module that imports another module like this:
#main-file.py
import src.FetchFunction.video_service.fields.urls as urls
def some_func():
return urls.fetch()
now I want to test this file like this:
import unittest
import src.FetchFunction.video_service.fields.urls as urls
from unittest.mock import MagicMock
class MainFileTest(unittest.TestCase):
def test_example(self):
urls.fetch = MagicMock(return_value='mocked_resp')
assertSomething()
this part works well and does what I want. BUT this affects other tests file... I mean I have other tests that use the "urls.fetch" and now instead of getting the proper flow they get the above mocked response.
Any idea?
pytest
to run my testsUpvotes: 1
Views: 116
Reputation: 66241
Use patch
in a context to define the scope where the mocked fetch
should be used. In the example below, outside the with
block the urls.fetch
is reverted to the original value:
import unittest
from unittest.mock import patch
class MainFileTest(unittest.TestCase):
def test_example(self):
with patch('urls.fetch', return_value='mocked_resp'):
# urls.fetch is mocked now
assertSomething()
# urls.fetch is not mocked anymore
Upvotes: 1