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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
appinit parameter, instead of patching/mocking httpx calls (althoughrespxis awesome when needed).However, a problem arises since the initialization of
httpx.AsyncClientis happening as part of app startup/setup, and there is no easy way to inject an extraappparameter at that point.Proposal: add an
attatch_appor a more genericset_transportmethod that can dynamically attach anapp.Example:
Beta Was this translation helpful? Give feedback.
All reactions