Coverage for tests / test_tutorial / test_websockets / test_tutorial003.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
2from types import ModuleType 1abdc
4import pytest 1abdc
5from fastapi.testclient import TestClient 1abdc
8@pytest.fixture( 1abdc
9 name="mod",
10 params=[
11 pytest.param("tutorial003_py310"),
12 ],
13)
14def get_mod(request: pytest.FixtureRequest): 1abdc
15 mod = importlib.import_module(f"docs_src.websockets.{request.param}") 1abc
17 return mod 1abc
20@pytest.fixture(name="html") 1abdc
21def get_html(mod: ModuleType): 1abdc
22 return mod.html 1abc
25@pytest.fixture(name="client") 1abdc
26def get_client(mod: ModuleType): 1abdc
27 client = TestClient(mod.app) 1abc
29 return client 1abc
32def test_get(client: TestClient, html: str): 1abdc
33 response = client.get("/") 1hij
34 assert response.text == html 1hij
37def test_websocket_handle_disconnection(client: TestClient): 1abdc
38 with ( 1e
39 client.websocket_connect("/ws/1234") as connection,
40 client.websocket_connect("/ws/5678") as connection_two,
41 ):
42 connection.send_text("Hello from 1234") 1fge
43 data1 = connection.receive_text() 1fge
44 assert data1 == "You wrote: Hello from 1234" 1fge
45 data2 = connection_two.receive_text() 1fge
46 client1_says = "Client #1234 says: Hello from 1234" 1fge
47 assert data2 == client1_says 1fge
48 data1 = connection.receive_text() 1fge
49 assert data1 == client1_says 1fge
50 connection_two.close() 1fge
51 data1 = connection.receive_text() 1fge
52 assert data1 == "Client #5678 left the chat" 1fge