I'm creating some unit tests using the pytest module and requests-mock for mocking the Response object for requests. I have the following pytest fixture
@pytest.fixture(scope="function")
def mock_response(requests_mock):
test_url = "https://dummy/"
test_json = [{"Name": "TheName"}, {"Name": "TheOtherName"}]
requests_mock.get(test_url, json=test_json, status_code=200)
resp = requests.get(test_url)
return resp
and the following unit test
def test_get_product_list(mocker, mock_response):
with requests_mock.Mocker() as m:
ret_val = mock_response
mocker.patch("path_to_function.function_with_request",
return_value=ret_val)
val = function_with_request(123)
assert val == ["TheName", "TheOtherName"]
The function_with_request
makes an API call and then parses the Response
to make a list of values with the Name
key
I want to run this test with a few different values for test_json
. I looked into parameterized fixtures, but none of the examples I saw seemed to match what I'm looking for.