From cb57435e1b921192ba61acdb661c77684194aba7 Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Thu, 18 Dec 2025 16:06:37 +0100 Subject: [PATCH] Add tests for `docs_src.custom_response.tutorial002` and `docs_src.custom_response.tutorial003` --- ...st_tutorial002_tutorial003_tutorial004.py} | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) rename tests/test_tutorial/test_custom_response/{test_tutorial004.py => test_tutorial002_tutorial003_tutorial004.py} (54%) diff --git a/tests/test_tutorial/test_custom_response/test_tutorial004.py b/tests/test_tutorial/test_custom_response/test_tutorial002_tutorial003_tutorial004.py similarity index 54% rename from tests/test_tutorial/test_custom_response/test_tutorial004.py rename to tests/test_tutorial/test_custom_response/test_tutorial002_tutorial003_tutorial004.py index 0e7d69791..22e2e0254 100644 --- a/tests/test_tutorial/test_custom_response/test_tutorial004.py +++ b/tests/test_tutorial/test_custom_response/test_tutorial002_tutorial003_tutorial004.py @@ -1,8 +1,25 @@ +import importlib + +import pytest from fastapi.testclient import TestClient -from docs_src.custom_response.tutorial004_py39 import app -client = TestClient(app) +@pytest.fixture( + name="mod_name", + params=[ + pytest.param("tutorial002_py39"), + pytest.param("tutorial003_py39"), + pytest.param("tutorial004_py39"), + ], +) +def get_mod_name(request: pytest.FixtureRequest) -> str: + return request.param + + +@pytest.fixture(name="client") +def get_client(mod_name: str) -> TestClient: + mod = importlib.import_module(f"docs_src.custom_response.{mod_name}") + return TestClient(mod.app) html_contents = """ @@ -17,13 +34,18 @@ html_contents = """ """ -def test_get_custom_response(): +def test_get_custom_response(client: TestClient): response = client.get("/items/") assert response.status_code == 200, response.text assert response.text == html_contents -def test_openapi_schema(): +def test_openapi_schema(client: TestClient, mod_name: str): + if mod_name.startswith("tutorial003"): + response_content = {"application/json": {"schema": {}}} + else: + response_content = {"text/html": {"schema": {"type": "string"}}} + response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { @@ -35,7 +57,7 @@ def test_openapi_schema(): "responses": { "200": { "description": "Successful Response", - "content": {"text/html": {"schema": {"type": "string"}}}, + "content": response_content, } }, "summary": "Read Items",