As we learned in Chapter 3, ;Coding, Testing and Documenting - the Virtuous Cycle, the biggest challenge when writing functional tests for a service that calls other services is to isolate all network calls.
In this section, we'll see how we can mock synchronous calls made with Requests, and asynchronous calls for Celery workers and other asynchronous processes.
If you are using Requests to perform all the calls--or you are using a library that is based on Requests and that does not customize it too much, this isolation work is easier to do, thanks to the transport adapters we saw earlier in this chapter.
The requests-mock project (https://requests-mock.readthedocs.io) implements an adapter that will let you mock network calls in your tests.
Earlier in this chapter, we saw an example of a Flask app that was an HTTP endpoint to serve some content on its /api
endpoint.
That application used a Request
session that was created by a setup_connector()
function and retrieved...