'Unit testing for python function-using post requests and get requests
I am trying to test the below function using unit test mocks but i am not able to do it since the return type from request get/post is of type <class 'requests.models.Response'>. please let me know how to mock this and unit test this below function:
def response_check(base_url, headers, schedule_name, response):
"""
Function to get response from dataform API and check the job status
"""
run_url = base_url + "/" + response.json()["id"]
query_response = requests.get(run_url, headers=headers)
while query_response.json()["status"] == "RUNNING":
time.sleep(10)
print("Dataform job running")
query_response = requests.get(run_url, headers=headers)
if query_response.json()["status"] in ["FAILED", "CANCELLED", "TIMED_OUT"]:
raise AirflowException(
f'Dataform task {schedule_name} has been {response.json()["status"]} for reason {response.json()["runLogUrl"]}'
)
print(query_response.json())
return "Dataform job finished"
I need to check for different response status but i am not able to mock the return type of it
Solution 1:[1]
Please test using http module and the http codes. For e.g. success is http.HTTPStatus.OK
Not found is http.HTTPStatus.NOT_FOUND Bad input is http.HTTPStatus.BAD_REQUEST etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sreejith V |
