I'm going to mock a Python function in unit tests.
This is the main function.
from api import get_users_from_api
def get_users(user_ids):
for user_id in user_ids:
res = get_users_from_api(user_id)
I'm trying to mock get_users_from_api function in the unit test because it's calling the 3rd party api endpoint.
This is the test script.
@patch("api.get_users_from_api")
def test_get_users(self, mock_get_users)
user_ids = [1, 2, 3]
mock_get_users.return_value = {
id: 1,
first_name: "John",
last_name: "Doe",
...
} # mock response
get_users(user_ids) # call main function
The issue is that I'm getting the same result for all users because I only used one mock as the return value of get_users_from_api.
I want to mock different values for every user. How can I do this?
from api import get_users_from_api, so you should be mockingfoo.get_users_from_api, wherefoois whatever module definesget_users.