diff --git a/tests/test_default_response_class.py b/tests/test_default_response_class.py index 042fb1dea9..88498e5601 100644 --- a/tests/test_default_response_class.py +++ b/tests/test_default_response_class.py @@ -1,15 +1,18 @@ from typing import Any -import orjson from fastapi import APIRouter, FastAPI from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse from fastapi.testclient import TestClient +from tests.utils import needs_orjson + class ORJSONResponse(JSONResponse): media_type = "application/x-orjson" def render(self, content: Any) -> bytes: + import orjson + return orjson.dumps(content) @@ -118,6 +121,7 @@ html_type = "text/html; charset=utf-8" override_type = "application/x-override" +@needs_orjson def test_app(): with client: response = client.get("/") @@ -132,6 +136,7 @@ def test_app_override(): assert response.headers["content-type"] == text_type +@needs_orjson def test_router_a(): with client: response = client.get("/a") @@ -146,6 +151,7 @@ def test_router_a_override(): assert response.headers["content-type"] == text_type +@needs_orjson def test_router_a_a(): with client: response = client.get("/a/a") diff --git a/tests/test_deprecated_responses.py b/tests/test_deprecated_responses.py index eff5792717..b370d405bd 100644 --- a/tests/test_deprecated_responses.py +++ b/tests/test_deprecated_responses.py @@ -7,6 +7,8 @@ from fastapi.responses import ORJSONResponse, UJSONResponse from fastapi.testclient import TestClient from pydantic import BaseModel +from tests.utils import needs_orjson + class Item(BaseModel): name: str @@ -28,6 +30,7 @@ def _make_orjson_app() -> FastAPI: return app +@needs_orjson def test_orjson_response_returns_correct_data(): app = _make_orjson_app() client = TestClient(app) @@ -38,6 +41,7 @@ def test_orjson_response_returns_correct_data(): assert response.json() == {"name": "widget", "price": 9.99} +@needs_orjson def test_orjson_response_emits_deprecation_warning(): with pytest.warns(FastAPIDeprecationWarning, match="ORJSONResponse is deprecated"): ORJSONResponse(content={"hello": "world"}) diff --git a/tests/test_orjson_response_class.py b/tests/test_orjson_response_class.py index 63ea054d1f..83940a368c 100644 --- a/tests/test_orjson_response_class.py +++ b/tests/test_orjson_response_class.py @@ -1,11 +1,14 @@ import warnings +import pytest from fastapi import FastAPI from fastapi.exceptions import FastAPIDeprecationWarning from fastapi.responses import ORJSONResponse from fastapi.testclient import TestClient from sqlalchemy.sql.elements import quoted_name +pytest.importorskip("orjson") + with warnings.catch_warnings(): warnings.simplefilter("ignore", FastAPIDeprecationWarning) app = FastAPI(default_response_class=ORJSONResponse) diff --git a/tests/test_tutorial/test_custom_response/test_tutorial001b.py b/tests/test_tutorial/test_custom_response/test_tutorial001b.py index 11ce813b76..f783e563cb 100644 --- a/tests/test_tutorial/test_custom_response/test_tutorial001b.py +++ b/tests/test_tutorial/test_custom_response/test_tutorial001b.py @@ -11,6 +11,8 @@ with warnings.catch_warnings(): client = TestClient(app) +pytest.importorskip("orjson") + @pytest.mark.filterwarnings("ignore::fastapi.exceptions.FastAPIDeprecationWarning") def test_get_custom_response(): diff --git a/tests/test_tutorial/test_custom_response/test_tutorial009c.py b/tests/test_tutorial/test_custom_response/test_tutorial009c.py index 7a1b713079..bc41ca0c25 100644 --- a/tests/test_tutorial/test_custom_response/test_tutorial009c.py +++ b/tests/test_tutorial/test_custom_response/test_tutorial009c.py @@ -1,5 +1,8 @@ +import pytest from fastapi.testclient import TestClient +pytest.importorskip("orjson") + from docs_src.custom_response.tutorial009c_py310 import app client = TestClient(app) diff --git a/tests/utils.py b/tests/utils.py index fff7348b9c..e1a8930e10 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,3 +1,4 @@ +import importlib import sys import pytest @@ -9,6 +10,11 @@ needs_py314 = pytest.mark.skipif( sys.version_info < (3, 14), reason="requires python3.14+" ) +needs_orjson = pytest.mark.skipif( + importlib.util.find_spec("orjson") is None, + reason="requires orjson", +) + workdir_lock = pytest.mark.xdist_group("workdir_lock")