Coverage for tests / test_tutorial / test_events / test_tutorial002.py: 100%

21 statements  

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

1import pytest 1abdc

2from fastapi import FastAPI 1abdc

3from fastapi.testclient import TestClient 1abdc

4from inline_snapshot import snapshot 1abdc

5 

6 

7@pytest.fixture(name="app", scope="module") 1abdc

8def get_app(): 1abdc

9 with pytest.warns(DeprecationWarning): 1abc

10 from docs_src.events.tutorial002_py310 import app 1abc

11 yield app 1abc

12 

13 

14def test_events(app: FastAPI): 1abdc

15 with TestClient(app) as client: 1efg

16 response = client.get("/items/") 1efg

17 assert response.status_code == 200, response.text 1efg

18 assert response.json() == [{"name": "Foo"}] 1efg

19 with open("log.txt") as log: 1efg

20 assert "Application shutdown" in log.read() 1efg

21 

22 

23def test_openapi_schema(app: FastAPI): 1abdc

24 with TestClient(app) as client: 1hij

25 response = client.get("/openapi.json") 1hij

26 assert response.status_code == 200, response.text 1hij

27 assert response.json() == snapshot( 1hij

28 { 

29 "openapi": "3.1.0", 

30 "info": {"title": "FastAPI", "version": "0.1.0"}, 

31 "paths": { 

32 "/items/": { 

33 "get": { 

34 "responses": { 

35 "200": { 

36 "description": "Successful Response", 

37 "content": {"application/json": {"schema": {}}}, 

38 } 

39 }, 

40 "summary": "Read Items", 

41 "operationId": "read_items_items__get", 

42 } 

43 } 

44 }, 

45 } 

46 )