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

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import FastAPI 1abcde

2from fastapi.testclient import TestClient 1abcde

3from fastapi.websockets import WebSocket 1abcde

4 

5app = FastAPI() 1abcde

6 

7 

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

9async def read_main(): 1abcde

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

11 

12 

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

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

15 await websocket.accept() 1abcde

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

17 await websocket.close() 1abcde

18 

19 

20def test_read_main(): 1abcde

21 client = TestClient(app) 1abcde

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

23 assert response.status_code == 200 1abcde

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

25 

26 

27def test_websocket(): 1abcde

28 client = TestClient(app) 1abcde

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

30 data = websocket.receive_json() 1abcde

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