respx icon indicating copy to clipboard operation
respx copied to clipboard

Add `assert_called_*_with()` helpers

Open SebCorbin opened this issue 2 months ago • 3 comments

Currently calling respx.calls.assert_called_once_with does not work, even though it's defined through inheritance class CallList(list, mock.NonCallableMock)

It would be nice to have a way of checking how httpx has been called (with which parameters).

SebCorbin avatar Oct 05 '25 10:10 SebCorbin

You're right, it probably doesn't work .. but if it would, then the call is the actual request + optional response, which may not be what you want to assert on.

Do you have an example of what you'd like to assert on, i.e. what you consider a call-comparison to look like?

Since each call has the request, one can currently assert request manually, e.g. using dirty-equals ..

assert respx.calls == IsList(
    HasAttributes(
        request=HasAttributes(
            content=IsJson(
                # body
            ),
        ),
    ),
    # more calls
)

.. or last request ..

assert respx.calls.last.request == HasAttributes(content=IsJson(...))

I'm actually planning on adding optional contrib helpers for dirty-equals to respx for easier request assertions, e.g. IsRequest(json=..., params=...) or alike.

Though, assert_called_*_with() would probably be suited to assert on httpx.Request properties as well 🤔 .. is that what you mean?

lundberg avatar Nov 17 '25 13:11 lundberg

Given respx is in charge of returning the correct response, checking that would not be pertinent. assert_called_*_with() here would just check, as stated in the description, how httpx would have been called (with which parameters) so it would check the request before being passed to transport

SebCorbin avatar Nov 25 '25 13:11 SebCorbin

I don't think it really matters how httpx was called, but rather what request, and response, was produced, e.g. the same request could be triggerd by either ..

  • httpx.get(...)
  • httpx.request("GET", ...)
  • httpx.Client().send(httpx.Request("GET", ...))

.. hence my suggestion of passed params to assert_called_*_with() maybe should assert match on request attributes, e.g. method, params, json etc. .. just a thought.

lundberg avatar Nov 25 '25 14:11 lundberg