小编ked*_*ked的帖子

如何断言在pytest中调用了猴子补丁?

考虑以下:

class MockResponse:
    status_code = 200

    @staticmethod
    def json():
        return {'key': 'value'}
                                  # where api_session is a fixture
def test_api_session_get(monkeypatch, api_session) -> None:
    def mock_get(*args, **kwargs):
        return MockResponse()

    monkeypatch.setattr(requests.Session, 'get', mock_get)
    response = api_session.get('endpoint/') # My wrapper around requests.Session
    assert response.status_code == 200
    assert response.json() == {'key': 'value'}
    monkeypatch.assert_called_with(
        'endpoint/',
        headers={
            'user-agent': 'blah',
        },
    )
Run Code Online (Sandbox Code Playgroud)

我如何断言get我正在修补的被调用'/endpoint'headers?当我现在运行测试时,我收到以下失败消息:

FAILED test/utility/test_api_session.py::test_api_session_get - AttributeError: 'MonkeyPatch' object has no attribute 'assert_called_with'

我在这里做错了什么?感谢所有提前回复的人。

monkeypatching mocking pytest python-3.x python-requests

5
推荐指数
2
解决办法
995
查看次数