Skip to content

Conversation

Mantisus
Copy link
Collaborator

@Mantisus Mantisus commented Aug 6, 2025

  • Replace respx mocks with pytest-httpserver in tests

@Mantisus Mantisus marked this pull request as draft August 6, 2025 00:40
@Mantisus
Copy link
Collaborator Author

Mantisus commented Aug 6, 2025

I chose pytest-httpserver as a testing tool. This is because the tests are too specific and do not allow the use of httpbin without breaking the test logic. At the same time, there are not enough tests to use uvicorn with complex endpoint logic.

I haven't deleted respx yet, as the timeout tests are quite specific to httpx. They should be considered separately after switching to impit.

@Mantisus Mantisus marked this pull request as ready for review August 6, 2025 14:19
@Mantisus Mantisus requested a review from Pijukatel August 6, 2025 14:19
@Mantisus Mantisus had a problem deploying to fork-pr-integration-tests August 6, 2025 14:19 — with GitHub Actions Failure
@Mantisus Mantisus requested a review from janbuchar August 6, 2025 14:19
@Mantisus Mantisus self-assigned this Aug 6, 2025
Copy link
Contributor

@Pijukatel Pijukatel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is good, but I am curious about the impit and those test_specific_timeouts_... tests

@Mantisus
Copy link
Collaborator Author

Mantisus commented Aug 6, 2025

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_timeouts

I believe this approach is the most promising.

@Mantisus Mantisus had a problem deploying to fork-pr-integration-tests August 6, 2025 20:21 — with GitHub Actions Failure
@Mantisus Mantisus had a problem deploying to fork-pr-integration-tests August 8, 2025 13:50 — with GitHub Actions Failure
@Pijukatel Pijukatel merged commit 90ac9c1 into apify:master Aug 11, 2025
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants