mardi 11 août 2020

Pyhton unit test - mock response from nested function

I am testing my execute method of a lambda, from that function we call another function which returns a response, something like this.

def get_response(id):
    url = f"{os.environ['SOME_URL']}/api/{id}/blah/"
    headers = {
        "Subscription-Key": os.environ["MY_KEY"],
        "Content-Type": "application/json",
    }

    response = requests.get(url, headers=headers)
    response.raise_for_status()
    response_content = json.loads(response.content)
    return response_content[0]

My test so far is something simple like this

    def test_execute_success(self):

        event = get_mock_event()
        response = execute(event, None)
        self.assertEqual("""{ "statusCode": "200" }""", response["body"])

How can I call the execute function and set a mock response from my get_response function when execute function tries to call it?

Thanks

Aucun commentaire:

Enregistrer un commentaire