0

I have one function where I am calling API to post get the resource. Since my function does not return anything then its tough to write unit test for failure scenario. Here I want to force request.get() to return different HTTP status code.

Is there anyway to mock my function to return desired status code?

foo.py

def getData():
    response = requests.get(run_task_status_url, headers=iics_job_header)
    logging.debug(f"Activity Monitor API response: {response.json()}")

    if 200 == response.status_code:
        print("success")
    else 401  == response.status_code:
        print("401")
Cheche
  • 1,456
  • 10
  • 27
user1591156
  • 1,945
  • 4
  • 18
  • 31
  • 1
    see https://requests-mock.readthedocs.io/en/latest/response.html – balderman Oct 11 '21 at 19:43
  • your function logic is also faulty, you log `response.json` but that is not guaranteed to be returned for anything other than a `200` – gold_cy Oct 11 '21 at 19:45
  • Does this answer your question? [How can I mock requests and the response?](https://stackoverflow.com/questions/15753390/how-can-i-mock-requests-and-the-response) – Tzane Oct 12 '21 at 08:09
  • If you are using pytest frame work there is an example in the docs: https://docs.pytest.org/en/6.2.x/monkeypatch.html#monkeypatching-returned-objects-building-mock-classes – Tzane Oct 12 '21 at 08:10

0 Answers0