Coverage for docs_src / app_testing / tutorial002_py310.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from fastapi.websockets import WebSocket 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8@app.get("/") 1abcd

9async def read_main(): 1abcd

10 return {"msg": "Hello World"} 1klmnop

11 

12 

13@app.websocket("/ws") 1abcd

14async def websocket(websocket: WebSocket): 1abcd

15 await websocket.accept() 1efghij

16 await websocket.send_json({"msg": "Hello WebSocket"}) 1efghij

17 await websocket.close() 1efghij

18 

19 

20def test_read_main(): 1abcd

21 client = TestClient(app) 1klmnop

22 response = client.get("/") 1klmnop

23 assert response.status_code == 200 1klmnop

24 assert response.json() == {"msg": "Hello World"} 1klmnop

25 

26 

27def test_websocket(): 1abcd

28 client = TestClient(app) 1efghij

29 with client.websocket_connect("/ws") as websocket: 1efghij

30 data = websocket.receive_json() 1efghij

31 assert data == {"msg": "Hello WebSocket"} 1efghij