我想你想修补一下
Client.execute
在里面
A
,并且夹具应返回类型为的对象
A
有了补丁。首先要确保修补程序仍处于活动状态:
@pytest.fixture
def mocked_a(self):
with patch('path_to_a.Client.execute'):
yield A()
在您的示例中,对象在修补范围之后返回,这意味着修补已在该点结束。还请注意,您必须修补
Client
在中导入的模块
a
模块(假设是
A
定义)。
def test_successful_execution(mocked_a):
mocked_a.call()
mocked_a.Client.execute.assert_called_once()
或者,您可以在fixture中返回mock本身:
@pytest.fixture
def mocked_execute(self):
with patch('path_to_a.Client.execute') as patched:
yield patched
def test_successful_execution(mocked_execute):
a = A()
a.call()
mocked_execute.assert_called_once()
这样你就可以明确地在你的测试中创建这个对象,在我看来,测试更加清晰。
(我想不通,所以可能有错误…)