Cannot use attach_mock with an autospec function mock
问题 Library module: # mod.py def foo(): bar1("arg1") bar2("arg2x", "arg2y") def bar1(x): pass def bar2(x, y): pass Test module: # test_mod.py from mod import foo def test_foo(mocker): mock = mocker.MagicMock() mock.attach_mock(mocker.patch("mod.bar1"), "b1") mock.attach_mock(mocker.patch("mod.bar2", autospec=True), "b2") foo() mock.assert_has_calls( [ mocker.call.b1("arg1"), mocker.call.b2("arg2x", "arg2y"), ] ) The mocker fixture is from pytest-mock plugin. Execute the MCVE with python -m pytest