简体   繁体   English

在pytest中的函数内为同一调用返回不同的模拟结果

[英]Return different mock results for the same call within a function in pytest

I have a test function where I successfully patched a function call to return a mock result.我有一个测试函数,我成功地修补了一个函数调用以返回模拟结果。 It works for the one time the patched function is called:它只在一次调用修补函数时起作用:

@patch(‘db.get_session’)
def test_some_function(mock_session):
    mock_session.return_value.query.return_value = MockResult(1)
    assert some_function()

However, I'm trying to test a function that uses get_session().query() multiple times and should return a different result each time.但是,我正在尝试测试一个多次使用 get_session().query() 并且每次都应该返回不同结果的函数。 I'm not sure how to mock this.我不知道如何嘲笑这个。

@patch(‘db.get_session’)
def test_some_other_function(mock_session):
    # First call should return one
    mock_session.return_value.query.return_value = MockResult(1)
    # Second call should return no results:
    mock_session.return_value.query.return_value = MockResult(0)
    assert some_other_function()

def some_other_function():
    session = get_session()
    one = session.query('some_query')
    zero = session.query('some_other_query')
    if one == 1 and zero == 0:
        return True

I've taken a look at side_effect but I'm not sure how to set it up since I'm not changing the straight return_value but rather the return_value of the return_value (if that makes sense).我看过side_effect但我不确定如何设置它,因为我没有改变直接的 return_value 而是 return_value 的 return_value (如果有道理的话)。

the side_effect attribute of a mock can contain an iterable -- each item in the iterable will be returned in order:模拟的side_effect属性可以包含一个可迭代对象——可迭代对象中的每个项目都将按顺序返回:

>>> m = mock.Mock(side_effect=(1, 2))
>>> m()
1
>>> m()
2

combining this with your example:将此与您的示例相结合:

    mock_session.return_value.query.side_effect = (MockResult(1), MockResult(0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pytest 在 class 的 __init__ 中模拟 function 调用 - pytest mock a function call in __init__ of a class 在python函数中模拟具有不同返回值的相同方法两次 - Mock same method with different return value called twice in a function in python Python模拟补丁使用pytest在另一个函数内的coroutine函数? - Python mock patch a coroutine function within another function using pytest? 如何使用 pytest 在其他 function 中模拟 function 调用? - How to mock function call inside other function using pytest? 从 Pytest 夹具中调用自定义函数 - Call custom function from within Pytest fixture PYTEST:在相同的函数/api 调用中通过传入 monkeypatch 的不同 arg 获得不同的响应 - PYTEST: Get different response with different arg passing in monkeypatch on same function/api call Pytest:模拟具有不同 side_effect 的相同方法的多次调用 - Pytest: Mock multiple calls of same method with different side_effect 如何使用 pytest-mock 指定模拟 function 的返回值? - How to specify return value of mocked function with pytest-mock? 测试 Except 块,模拟 function 的返回是一个异常 python pytest - Test the Except Block, Mock the return of a function be an exception python pytest Pytest 模拟 fastapi。取决于直接 function 调用 - Pytest mock fastapi.Depends upon direct function call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM