Contract Testing with HTTPX - Part 2
This is a continuation of Contract Testing with HTTPX.
On the previous article, I used RESPX to call the service B from service A. Although it looks cool, we can actually achieve the same goal without using anything besides FastAPI itself.
Let's assume that we have similar two services as presented in the previous article.
The difference here is that we'll be creating a dependency called
is going to return a
httpx.AsyncClient instance that calls our service B.
- The port is 8001, not 8000, to avoid conflicts with the other service.
Now, let's call the
- The HTTP client used is called HTTPie, but you can use [
curl], or just go to the browser, and access
The response should look like:
Since the only difference between this article, and the previous one is the creation of the dependency on service A, you might be guessing that we are going to override the dependency, and... You are right! (if you didn't, is fine as well )
We are going to use
app.dependency_overrides to override the
service_b_client dependency, and instead of calling the real service B,
we'll call the application itself, avoiding the network calls that would potentially slow down our test suite.
See more on the Starlette documentation.
Good! Now are able to avoid a lot of network calls, and speed up our test suite.