Skip to content

Does not work with FastAPI/starlette's TestClient #47

@nikolarankovic-tomtom

Description

@nikolarankovic-tomtom

Hi, thank you for this excellent plugin. While it works fine with Flask's test client, the plugin blocks starlette's TestClient (used by FastAPI). Here's a self-contained example that I'd expect to pass with pytest:

from pytest_socket import disable_socket
from starlette.responses import HTMLResponse
from starlette.testclient import TestClient


def pytest_runtest_setup():
    disable_socket()


async def app(scope, receive, send):
    assert scope['type'] == 'http'
    response = HTMLResponse('<html><body>Hello, world!</body></html>')
    await response(scope, receive, send)


def test_app():
    client = TestClient(app)
    response = client.get('/')
    assert response.status_code == 200

The above returns a:

E       pytest_socket.SocketBlockedError: A test tried to use socket.socket

Would it be possible to make this work with FastAPI/Starlette?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions