From 0d881322db9ca13bcd1f2a21b60277a1b42c9462 Mon Sep 17 00:00:00 2001 From: Valentyn Druzhynin Date: Wed, 19 Mar 2025 23:14:18 -0400 Subject: [PATCH 1/2] Simplify tests for settings tutorial #13504 --- .../test_settings/test_tutorial001.py | 24 +++++++++++++++---- .../test_settings/test_tutorial001_pv1.py | 19 --------------- 2 files changed, 20 insertions(+), 23 deletions(-) delete mode 100644 tests/test_tutorial/test_settings/test_tutorial001_pv1.py diff --git a/tests/test_tutorial/test_settings/test_tutorial001.py b/tests/test_tutorial/test_settings/test_tutorial001.py index eb30dbcee..4f73cd1bd 100644 --- a/tests/test_tutorial/test_settings/test_tutorial001.py +++ b/tests/test_tutorial/test_settings/test_tutorial001.py @@ -1,14 +1,30 @@ +import importlib + +import pytest from fastapi.testclient import TestClient from pytest import MonkeyPatch -from ...utils import needs_pydanticv2 +from ...utils import needs_pydanticv1, needs_pydanticv2 + + +@pytest.fixture( + params=[ + pytest.param("tutorial001", marks=needs_pydanticv2), + pytest.param("tutorial001_pv1", marks=needs_pydanticv1), + ], +) +def get_app(request: pytest.FixtureRequest): + def app_wrapper(): + mod = importlib.import_module(f"docs_src.settings.{request.param}") + return mod.app + + return app_wrapper -@needs_pydanticv2 -def test_settings(monkeypatch: MonkeyPatch): +def test_settings(get_app, monkeypatch: MonkeyPatch): monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com") - from docs_src.settings.tutorial001 import app + app = get_app() client = TestClient(app) response = client.get("/info") assert response.status_code == 200, response.text diff --git a/tests/test_tutorial/test_settings/test_tutorial001_pv1.py b/tests/test_tutorial/test_settings/test_tutorial001_pv1.py deleted file mode 100644 index e4659de66..000000000 --- a/tests/test_tutorial/test_settings/test_tutorial001_pv1.py +++ /dev/null @@ -1,19 +0,0 @@ -from fastapi.testclient import TestClient -from pytest import MonkeyPatch - -from ...utils import needs_pydanticv1 - - -@needs_pydanticv1 -def test_settings(monkeypatch: MonkeyPatch): - monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com") - from docs_src.settings.tutorial001_pv1 import app - - client = TestClient(app) - response = client.get("/info") - assert response.status_code == 200, response.text - assert response.json() == { - "app_name": "Awesome API", - "admin_email": "admin@example.com", - "items_per_user": 50, - } From a6d187025d8406dcb84e9524eed9a1f3cb80eb11 Mon Sep 17 00:00:00 2001 From: Valentyn Druzhynin Date: Thu, 20 Mar 2025 08:45:06 -0400 Subject: [PATCH 2/2] Fixes after review --- .../test_settings/test_tutorial001.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/test_tutorial/test_settings/test_tutorial001.py b/tests/test_tutorial/test_settings/test_tutorial001.py index 4f73cd1bd..92a5782d4 100644 --- a/tests/test_tutorial/test_settings/test_tutorial001.py +++ b/tests/test_tutorial/test_settings/test_tutorial001.py @@ -8,23 +8,19 @@ from ...utils import needs_pydanticv1, needs_pydanticv2 @pytest.fixture( + name="app", params=[ pytest.param("tutorial001", marks=needs_pydanticv2), pytest.param("tutorial001_pv1", marks=needs_pydanticv1), ], ) -def get_app(request: pytest.FixtureRequest): - def app_wrapper(): - mod = importlib.import_module(f"docs_src.settings.{request.param}") - return mod.app - - return app_wrapper - - -def test_settings(get_app, monkeypatch: MonkeyPatch): +def get_app(request: pytest.FixtureRequest, monkeypatch: MonkeyPatch): monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com") + mod = importlib.import_module(f"docs_src.settings.{request.param}") + return mod.app + - app = get_app() +def test_settings(app): client = TestClient(app) response = client.get("/info") assert response.status_code == 200, response.text