Open CarliJoy opened 2 weeks ago
Hello @CarliJoy ,
I am not sure to follow, why can't this be achieved with the should_mock
option set on the full test suite?
Hi colin-b,
Got it, we were using an older version with only the non_mocked_hosts
option. Thanks for the heads-up!
We still need to patch a specific client for our use case. We provide a pytest fixture as a plugin for our service, and users often want to mock additional services with pytest_httpx
.
However, using a global mocking state creates issues:
should_mock
is globally set, users can’t mock additional services.should_mock
, it overrides our fixture, leaving service A
unmocked.Additionally, different service tests might require varying options for assert_all_responses_were_requested
, assert_all_requests_were_expected
, or can_send_already_matched_responses
. Without the ability to mock specific httpx
clients, handling these cases is challenging.
Your should_mock
callable could rely on some shared variable (a list of urls, hosts, headers, etc... that you would base yourself upon to know if you should mock or not) that your clients would be free to update (thus keeping the previous setup you want to use as common). The value is evaluated at request time so this should work just fine as in the following:
shared_mocked_hosts = ["default_mocked_host"]
def should_mock(request: httpx.Request) -> bool:
return request.url.host in shared_mocked_hosts
The other options are evaluated only for mocked requests so it should be fine as well I assume?
In certain use cases,
pytest_httpx
is used only to mock a single httpx client, specifically one responsible for connecting to a cluster of servers for serviceA
. However, while mocking this specific client, we still want the flexibility to make normal, non-mocked requests to other services without impacting the entire environment.Previously, we initialized
HTTPXMock
directly, but with recent changes, this is no longer supported. While the focus on clean interfaces is understandable, this change limits the ability to easily create single mocked clients while keeping other requests unaltered.To address this, I propose adding support for a MockTransport, as outlined below:
The
MockedTransport
class extends integrate smoothly withHTTPXMock
, allowing targeted client mocking while enabling other clients to make live requests as usual.