]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
⚡️ Speed up test suite via caching and fixture scopes to make it ~24% faster (#13583)
authorRoman Postnov <59239573+dikos1337@users.noreply.github.com>
Sat, 23 May 2026 15:32:48 +0000 (18:32 +0300)
committerGitHub <noreply@github.com>
Sat, 23 May 2026 15:32:48 +0000 (17:32 +0200)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
tests/test_tutorial/test_security/test_tutorial005.py
tests/test_tutorial/test_sql_databases/test_tutorial001.py
tests/test_tutorial/test_sql_databases/test_tutorial002.py

index c6e0e78e09aa176ddc51e85d5c8e45d6fc67d808..0d25a1d2419b074c7b3968237dbb1bd703ab8c16 100644 (file)
@@ -1,4 +1,5 @@
 import importlib
+from functools import lru_cache
 from types import ModuleType
 
 import pytest
@@ -14,6 +15,7 @@ from ...utils import needs_py310
         pytest.param("tutorial005_py310", marks=needs_py310),
         pytest.param("tutorial005_an_py310", marks=needs_py310),
     ],
+    scope="module",
 )
 def get_mod(request: pytest.FixtureRequest):
     mod = importlib.import_module(f"docs_src.security.{request.param}")
@@ -21,6 +23,20 @@ def get_mod(request: pytest.FixtureRequest):
     return mod
 
 
+@pytest.fixture(scope="module", autouse=True)
+def cache_verify_password(mod: ModuleType):
+    assert hasattr(mod, "verify_password"), (
+        f"Module {mod.__name__} does not have attribute 'verify_password'"
+    )
+
+    original_func = mod.verify_password
+    cached_func = lru_cache()(original_func)
+
+    mod.verify_password = cached_func
+    yield
+    mod.verify_password = original_func
+
+
 def get_access_token(
     *, username="johndoe", password="secret", scope=None, client: TestClient
 ):
index 3407ec758873fa32d5f114becade7948cfba5283..cd6b3b11cfe5d7e0726bd347c0df70afc1028a09 100644 (file)
@@ -25,6 +25,7 @@ def clear_sqlmodel():
         pytest.param("tutorial001_py310", marks=needs_py310),
         pytest.param("tutorial001_an_py310", marks=needs_py310),
     ],
+    scope="module",
 )
 def get_client(request: pytest.FixtureRequest):
     clear_sqlmodel()
@@ -44,6 +45,8 @@ def get_client(request: pytest.FixtureRequest):
     # Clean up connection explicitly to avoid resource warning
     mod.engine.dispose()
 
+    mod.engine.dispose()
+
 
 def test_crud_app(client: TestClient):
     # TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor
index 51a36b6c793f36e34e87c0e7f1eee2e2010fcaa8..8037d886fb8bdace9c8b0dae30592899cb2ab9d7 100644 (file)
@@ -25,6 +25,7 @@ def clear_sqlmodel():
         pytest.param("tutorial002_py310", marks=needs_py310),
         pytest.param("tutorial002_an_py310", marks=needs_py310),
     ],
+    scope="module",
 )
 def get_client(request: pytest.FixtureRequest):
     clear_sqlmodel()
@@ -44,6 +45,8 @@ def get_client(request: pytest.FixtureRequest):
     # Clean up connection explicitly to avoid resource warning
     mod.engine.dispose()
 
+    mod.engine.dispose()
+
 
 def test_crud_app(client: TestClient):
     # TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor