Coverage for tests / test_tutorial / test_debugging / test_tutorial001.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
2import runpy 1abcd
3import sys 1abcd
4import unittest 1abcd
6import pytest 1abcd
7from fastapi.testclient import TestClient 1abcd
8from inline_snapshot import snapshot 1abcd
10MOD_NAME = "docs_src.debugging.tutorial001_py310" 1abcd
13@pytest.fixture(name="client") 1abcd
14def get_client(): 1abcd
15 mod = importlib.import_module(MOD_NAME) 1mnopqrs
16 client = TestClient(mod.app) 1mnopqrs
17 return client 1mnopqrs
20def test_uvicorn_run_is_not_called_on_import(): 1abcd
21 if sys.modules.get(MOD_NAME): 1ijkl
22 del sys.modules[MOD_NAME] # pragma: no cover
23 with unittest.mock.patch("uvicorn.run") as uvicorn_run_mock: 1ijkl
24 importlib.import_module(MOD_NAME) 1ijkl
25 uvicorn_run_mock.assert_not_called() 1ijkl
28def test_get_root(client: TestClient): 1abcd
29 response = client.get("/") 1tuvw
30 assert response.status_code == 200 1tuvw
31 assert response.json() == {"hello world": "ba"} 1tuvw
34def test_uvicorn_run_called_when_run_as_main(): # Just for coverage 1abcd
35 if sys.modules.get(MOD_NAME): 1efgh
36 del sys.modules[MOD_NAME] 1efgh
37 with unittest.mock.patch("uvicorn.run") as uvicorn_run_mock: 1efgh
38 runpy.run_module(MOD_NAME, run_name="__main__") 1efgh
40 uvicorn_run_mock.assert_called_once_with( 1efgh
41 unittest.mock.ANY, host="0.0.0.0", port=8000
42 )
45def test_openapi_schema(client: TestClient): 1abcd
46 response = client.get("/openapi.json") 1xyzA
47 assert response.status_code == 200 1xyzA
48 assert response.json() == snapshot( 1xyzA
49 {
50 "openapi": "3.1.0",
51 "info": {"title": "FastAPI", "version": "0.1.0"},
52 "paths": {
53 "/": {
54 "get": {
55 "summary": "Root",
56 "operationId": "root__get",
57 "responses": {
58 "200": {
59 "description": "Successful Response",
60 "content": {"application/json": {"schema": {}}},
61 },
62 },
63 }
64 }
65 },
66 }
67 )