Mocking REST API calls?

Hi folks.

I’m working on a largish Python project that has a REST API in Flask, and an asynchronous backend using Celery.

Sometimes a REST call endpoint will call another, to get needed data.

If I want create automated tests, where I’m using Flask’s app.test_client() like:

with app.test_client() as client:
    client.post(first_url, whatever)

That seems to work pretty straightforwardly.

But what if there is something at the code implementing first_url that wants to call second_url? Is there a good way, using a general tool or something Flask-specific, that will allow me to mock second_url’s post/get/whatever?

Thanks!

In theory you could mock out whatever is being called inside the post route. If that still isn’t exactly what you’d like, take a look at responses · PyPI.

I’ve seen this used to mock responses (as the other end of requests).

I’m not getting how to mock piece of code C, when I can’t (?) get control of execution somewhere above C. What am I missing?

It seems like since the REST API’s execution of second_url is controlled by Flask, I can’t mock something inside it. I could mock first_url, that I get. But how would I mock something inside second_url?