chore: replace respx mocks with pytest-httpserver in tests#454
chore: replace respx mocks with pytest-httpserver in tests#454Pijukatel merged 9 commits intoapify:masterfrom
respx mocks with pytest-httpserver in tests#454Conversation
|
I chose I haven't deleted |
Pijukatel
left a comment
There was a problem hiding this comment.
I think it is good, but I am curious about the impit and those test_specific_timeouts_... tests
It's still just an idea. But I think mocking the ‘request’ method for ‘impit’ clients will be something like: from collections.abc import Iterator
import impit
import pytest
class EndOfTestError(Exception):
"""Custom exception that is raised after the relevant part of the code is executed to stop the test."""
@pytest.fixture
def patch_request(monkeypatch: pytest.MonkeyPatch) -> Iterator[list]:
captured_calls = []
def mock_request(*args, **kwargs):
captured_calls.append({
'args': args,
'kwargs': kwargs,
'timeout': kwargs.get('timeout')
})
raise EndOfTestError
monkeypatch.setattr('impit.Client.request', mock_request)
yield captured_calls
monkeypatch.undo()
def test_dynamic_timeout_sync_client(patch_request: None) -> None:
captured_calls = patch_request
client = impit.Client()
expected_timeouts = [1, 2, 4, 5]
for timeout in expected_timeouts:
with pytest.raises(EndOfTestError):
client.request('GET', url="http://example.com", timeout=timeout)
actual_timeouts = [call['timeout'] for call in captured_calls]
assert actual_timeouts == expected_timeoutsI believe this approach is the most promising. |
respxmocks withpytest-httpserverin tests