Replies: 6 comments 8 replies
-
Yup, that makes sense. I'd do something similar too.
The pattern I'd suggest is configuring the client based on an environment variable. (Part of the "12 Factor Apps" design patterns). Make sure that when you run the tests, then an environment variable such as When you're initialising the client, switch between using a live client or using one with a mocked transport, based on the environment variable. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure that RESPX should handle mocking of the actual HTTPX client features, need to think that through though. Anyway, I answered your question @adriangb, but with a non-FastAPI solution, but with similar feel, using plain RESPX routes and side effects. |
Beta Was this translation helpful? Give feedback.
-
I think what I'm going to end up using here is dependency injection, be it manually (just passing the transport to an app factory function), via a DI framework or via FastAPIs own DI (although this may require some work to support singletons). I'll try to post a clean example if I can come up with one. |
Beta Was this translation helpful? Give feedback.
-
Managed to get a poc working @adriangb with RESPX and I'll add this feature to RESPX, just need to clean and tidy up the code first, before opening a PR. |
Beta Was this translation helpful? Give feedback.
-
Managed to get a PR working for |
Beta Was this translation helpful? Give feedback.
-
FYI, |
Beta Was this translation helpful? Give feedback.
-
Use case: I use httpx as a client within FastAPI apps.
For end-to-end/integration testing, I like to build my mocks as FastAPI apps themselves and attach them as via the
app
init parameter, instead of patching/mocking httpx calls (althoughrespx
is awesome when needed).However, a problem arises since the initialization of
httpx.AsyncClient
is happening as part of app startup/setup, and there is no easy way to inject an extraapp
parameter at that point.Proposal: add an
attatch_app
or a more genericset_transport
method that can dynamically attach anapp
.Example:
Beta Was this translation helpful? Give feedback.
All reactions