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

20 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import pytest 1abcdefg

2from fastapi import FastAPI 1abcdefg

3from fastapi.testclient import TestClient 1abcdefg

4 

5 

6@pytest.fixture(name="app", scope="module") 1abcdefg

7def get_app(): 1abcdefg

8 with pytest.warns(DeprecationWarning): 1abcdefg

9 from docs_src.events.tutorial002 import app 1abcdefg

10 yield app 1abcdefg

11 

12 

13def test_events(app: FastAPI): 1abcdefg

14 with TestClient(app) as client: 1hijklmn

15 response = client.get("/items/") 1hijklmn

16 assert response.status_code == 200, response.text 1hijklmn

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

18 with open("log.txt") as log: 1hijklmn

19 assert "Application shutdown" in log.read() 1hijklmn

20 

21 

22def test_openapi_schema(app: FastAPI): 1abcdefg

23 with TestClient(app) as client: 1opqrstu

24 response = client.get("/openapi.json") 1opqrstu

25 assert response.status_code == 200, response.text 1opqrstu

26 assert response.json() == { 1opqrstu

27 "openapi": "3.1.0", 

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

29 "paths": { 

30 "/items/": { 

31 "get": { 

32 "responses": { 

33 "200": { 

34 "description": "Successful Response", 

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

36 } 

37 }, 

38 "summary": "Read Items", 

39 "operationId": "read_items_items__get", 

40 } 

41 } 

42 }, 

43 }