Coverage for tests / test_tutorial / test_debugging / test_tutorial001.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
2import runpy 1abdc
3import sys 1abdc
4import unittest 1abdc
6import pytest 1abdc
7from fastapi.testclient import TestClient 1abdc
8from inline_snapshot import snapshot 1abdc
10MOD_NAME = "docs_src.debugging.tutorial001_py310" 1abdc
13@pytest.fixture(name="client") 1abdc
14def get_client(): 1abdc
15 mod = importlib.import_module(MOD_NAME) 1abc
16 client = TestClient(mod.app) 1abc
17 return client 1abc
20def test_uvicorn_run_is_not_called_on_import(): 1abdc
21 if sys.modules.get(MOD_NAME): 1hij
22 del sys.modules[MOD_NAME] # pragma: no cover
23 with unittest.mock.patch("uvicorn.run") as uvicorn_run_mock: 1hij
24 importlib.import_module(MOD_NAME) 1hij
25 uvicorn_run_mock.assert_not_called() 1hij
28def test_get_root(client: TestClient): 1abdc
29 response = client.get("/") 1klm
30 assert response.status_code == 200 1klm
31 assert response.json() == {"hello world": "ba"} 1klm
34def test_uvicorn_run_called_when_run_as_main(): # Just for coverage 1abdc
35 if sys.modules.get(MOD_NAME): 1efg
36 del sys.modules[MOD_NAME] 1efg
37 with unittest.mock.patch("uvicorn.run") as uvicorn_run_mock: 1efg
38 runpy.run_module(MOD_NAME, run_name="__main__") 1efg
40 uvicorn_run_mock.assert_called_once_with( 1efg
41 unittest.mock.ANY, host="0.0.0.0", port=8000
42 )
45def test_openapi_schema(client: TestClient): 1abdc
46 response = client.get("/openapi.json") 1nop
47 assert response.status_code == 200 1nop
48 assert response.json() == snapshot( 1nop
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 )