Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
A tip for debugging pytest-httpx#
I use pytest-httpx in a bunch of my projects. Occasionally I run into test failures like this one, which can sometimes be really hard to figure out:
1E AssertionError: The following responses are mocked but not requested:2E - Match POST request on https://api.mistral.ai/v1/chat/completions3E4E If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested5E assert not [<pytest_httpx._request_matcher._RequestMatcher object at 0x107d4e560>]Today I figured out a pattern for investigating these that works really well.
Drop a variant of this decorator onto your failing test:
1def intercept(request):2 from pprint import pprint3 import json4
5 print(request.url)6 pprint(json.loads(request.content))7 breakpoint()8 return True9
10@pytest.mark.httpx_mock(should_mock=intercept)11def test_tools_stream(mocked_tool_stream):12 model = llm.get_model("mistral/mistral-medium")13 ...The intercept() function will then be called for every request that pytest-httpx has the chance to intercept. In my function here I’m printing out the URL and pretty-printing the JSON body (I was debugging a Mistral API call) but you can leave those out entirely and just rely on the breakpoint().
When you run pytest your tests will pause at every instance of that function call, and you can then inspect the request object and figure out what’s going on.