[Python] Create tests to cover cases where the response from the third-party API is not the...

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have an API on Flask (Python) that interacts with a third-party API, for example, creating documents.
    My code:

    def my_function(data):
    response = requests.post("https://some_api.com", json=data)
    json_response = response.json()
    if json_response["message"] == "Successfully created":
    return "Successfully created"
    elif json_response["message"] == "Already exists":
    return "Already exists"
    elif json_response["message"] == "Wrong data":
    return "Wrong data"
    else:
    return json_response["message"]


    I have already created tests to cover cases where the third-party API works as expected(where "message" equals to "Successfully created", "Already exists" or "Wrong data").
    However, I also want to cover case where the third-party API does not work as expected. I want to create a test that covers the 'else' branch, but I cannot reach it while the third-party API is working properly.
    Is there a way to obtain a response that the third-party API would not normally provide?
    Can anyone suggest how I can achieve this?

    Thank you in advance.

    Continue reading...

Compartilhe esta Página