I am trying to understand the mock/patch framework, but have a problem. Here are my simplified codes:
file_a.py
class A:
def f(self):
b = B()
b.g()
b.h()
file_b.py
class B:
def g(self):
return network_requests(...)
def h(self):
return "This is should not be mocked."
file_test.py
class SomeTests:
def setUp(self):
with patch('file_b.B', autospec=True) as mock:
mock.g.return_value = "Mocked value"
mock.side_effect = lambda : B()
self.a = A()
def test(self):
self.a.f()
Essentially I want to mock only B.g
inside the test, but not B.h
. I got some idea from https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking, but B.g
is still not mocked.
Thank you!