1

I am trying to use the requests_mock as part of my unit test to test API Calls which run in a while loop so in order to end the while loop or to meet the condition of the while loop I need to send different response to my API. My URL remains the same but the param changes but requests_mock doesn't really cares about it.

My function goes like this :

def func():    
   response = requests.get(
       url=<url>
       params={"limit": 1000}
       headers=<headers>
    ).json()
    while "next" in response["info"].keys():
        response =  requests.get(
            url=<url>
            params={"limit": 1000, "info": response["info"]}
            headers=<headers>
        ).json()

My test looks like:

def test_url(requests_mock):
    requests_mock.get(url, json=<response_with_info_key>)

    func()
    data_request = requests_mock.request_history[0]
    assert data_request.query = "limit=1000"

What i want is my while loop to end with my second response to be without the "next" key. What I have already tried :

def test_url(requests_mock):
    requests_mock.get(url, json=[<response_with_info_key>, <response_without_info_key>])

    func()
    data_request = requests_mock.request_history[0]
    assert data_request.query = "limit=1000"

The simplest way to explain the whole question would be : How do i make requests-mock send two different response for the same API?

techdoodle
  • 85
  • 6
  • please show how you are setting up your requests_mock object – gold_cy Nov 22 '22 at 15:14
  • I use the default setting and I import the requests_mock as part of my test function as shown in the test. Then I use it to mock my request API – techdoodle Nov 23 '22 at 05:14
  • you need to configure it to return a list as shown --> https://requests-mock.readthedocs.io/en/latest/response.html#response-lists – gold_cy Nov 23 '22 at 12:21

0 Answers0