Colin-b/pytest_httpx

Question: How to mock only certain requests?

Closed this issue · 2 comments

Hi there,
Quick question. I'm using httpx for testing a webserver AND making external requests inside the webserver too.

I want requests to my webserver to go untouched. But I want to mock out all sub-requests made by my server.

Example:
Normal: http://testserver
Mocked: https://wikipedia...

I could inject, but I'm wondering if there is some functionality to disable mocking for certain urls. It seems like httpx_mock mocks everything.

Thanks in advance.
Phil

Hi @philwinder

Indeed this feature does not exists yet as the use case never occurred to me.

However it is possible to introduce it. Would the introduction of a fixture like the following be good enough for you?

@pytest.fixture
def non_mocked_hosts() -> list:
    return ["testserver"]

It would mean that every request to this host will actually be sent via httpx (no mocking) while every other hosts will be mocked the usual way

This fixture is now available. Refer to documentation if you want more details on how to use it.