[英]Partially mock a method in unittest `setUp`
我试图了解模拟/补丁框架,但是有问题。 这是我的简化代码:
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()
本质上,我只想模拟测试中的Bg
,而不模拟Bh
。 我从https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking Bg
了一些想法,但是仍然没有嘲笑Bg
。
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.