mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-25 00:37:44 -04:00
⚡️ Speed up test suite via caching and fixture scopes to make it ~24% faster (#13583)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -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
|
||||
):
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user