'How to mock multiple calls of a function in a Pytest

I have already wrote some tests in Pytest using Mock and Patch functionalities, but now I ran into issue that I can't seem to figure out. I have a function I am testing and in that function I am calling another function twice and I want to mock return value of those 2 calls, each being different. Posting example code down below:

def func(x):
    return x + 2


def main_func():
    first_call = func(2)
    second_call = func(3)

    result = first_call + second_call
    return result

#tests

from unittest.mock import patch
from app import main_func


@patch('app.func')
def test_main_func(mock_func_first, mock_func_second):
    mock_func_first.return_value = 1
    mock_func_second.return_value = 3
    assert(main_func()) == 4

As you can see, here in test_main_func I want to mock those 2 calls, but don't understand how to go about it. Could someone help me out?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source