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
Using pytest, I've tried to create a mock fixture for an app written with FastAPI/HTTPX (FastAPI is an interface microservice receiving requests, and forwarding onto a backend service using an HTTPX Async client).
The fixture and unit test
fromhttpx_gssapiimportHTTPSPNEGOAuthfrompytest_httpximportHTTPXMockfromunittest.mockimportMockclassTestClient(object):
@ pytest.fixturedeftest_client(self, test_settings):
"""Returns a test-configured client to use in tests"""test_client=MyClient(test_settings)
test_client._auth=Mock(spec=HTTPSPNEGOAuth)
returntest_client@pytest.mark.asyncioasyncdeftest_client_get_id(self, test_app, test_client, httpx_mock: HTTPXMock):
# Mockshttpx_mock.add_response(
url="http://test/id?path=foo&selectedFields=Id",
json={"Id": "TEST"}
)
asyncwithAsyncClient(app=test_app, base_url="http://test") asac:
id=awaittest_client._get_id(ac, "foo")
assertid=='TEST'
MyCllient has the auth set as a member, using the following prod defaults, which I thought the mock replacing in the fixture would be enough for things to "just work".
I treid swapping the Mock for an AsyncMock in the fixture:
@ pytest.fixturedeftest_client(self, test_settings):
"""Returns a test-configured client to use in tests"""test_client=MyClient(test_settings)
test_client._auth=AsyncMock(spec=HTTPSPNEGOAuth) # AsyncMock usedreturntest_client
Sorry for the slow response, I was traveling. The issue is AsyncMock not handling async generators properly. It is making the asend() and aclose() methods synchronous for some reason:
In [1]: fromhttpx_gssapiimportHTTPSPNEGOAuthIn [2]: fromunittest.mockimportAsyncMockIn [3]: auth=AsyncMock(spec=HTTPSPNEGOAuth)
In [4]: auth_flow=auth.async_auth_flow(...)
In [5]: auth_flowOut[5]: <MagicMockname='mock.async_auth_flow()'id='140057769280368'>In [6]: auth_flow.__anext__() # Correctly a coroutineOut[6]: <coroutineobjectAsyncMockMixin._execute_mock_callat0x7f61bd9731c0>In [7]: auth_flow.asendOut[7]: <MagicMockname='mock.async_auth_flow().asend'id='140057769692224'>In [8]: auth_flow.asend() # Not a coroutineOut[8]: <MagicMockname='mock.async_auth_flow().asend()'id='140057769324208'>In [9]: awaitauth_flow.asend()
---------------------------------------------------------------------------TypeErrorTraceback (mostrecentcalllast)
<ipython-input-9-4d8d419a061f>in<module>---->1awaitauth_flow.asend()
TypeError: objectMagicMockcan't be used in 'await' expression
To be honest, I'm not sure how to handle this simply, without mocking out some of the internal HTTPX auth flow details themselves. A better approach may be to use K5TEST like we do in the end_to_end tests and setup a fake Kerberos environment instead of mocking the authentication.
Using pytest, I've tried to create a mock fixture for an app written with FastAPI/HTTPX (FastAPI is an interface microservice receiving requests, and forwarding onto a backend service using an HTTPX Async client).
The fixture and unit test
MyCllient
has theauth
set as a member, using the following prod defaults, which I thought the mock replacing in the fixture would be enough for things to "just work".Within the function being tested, a get request is being made using a
HTTPSPNEGOAuth
auth object, with anhttpx.AsyncClient
instanceI receive the error:
I treid swapping the
Mock
for anAsyncMock
in the fixture:Which gives a similar error still:
I'm not sure where to take it from here to get the auth object mocked out correctly for use with pytest.
The text was updated successfully, but these errors were encountered: