You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# test_views.pyimporthttpx, pytest
...
# The `client` parameter is the fixture of web appdeftest_view_test(client, monkeypatch):
asyncdefreturn_mock_response(*args, **kwargs):
returnhttpx.Response(200, content=b'{"response": "response"}')
monkeypatch.setattr(httpx.AsyncClient, 'get', return_mock_response)
_, response=client.test_client.get('/test')
assertresponse.json== {'response': 'response', 'foo': 0}
assertresponse.status_code==200
Here, we replace our request _, response = client.test_client.get('/test') with the patched one. Which makes us customize the patch by excluding the host address.
The text was updated successfully, but these errors were encountered:
Sanic-testing
useshttpx
under the hood, which makes monkeypatching httpx requests hard to implement.For example if we have and endpoint which makes external requests, and if we monkeypatch this request, then it patches an actual request to endpoint:
Test function:
Here, we replace our request
_, response = client.test_client.get('/test')
with the patched one. Which makes us customize the patch by excluding the host address.The text was updated successfully, but these errors were encountered: