diff --git a/tests/test_tutorial/test_additional_responses/test_tutorial002.py b/tests/test_tutorial/test_additional_responses/test_tutorial002.py index 586779e44f..f1b53a96ce 100644 --- a/tests/test_tutorial/test_additional_responses/test_tutorial002.py +++ b/tests/test_tutorial/test_additional_responses/test_tutorial002.py @@ -6,7 +6,7 @@ import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot -from tests.utils import needs_py310 +from tests.utils import needs_py310, workdir_lock @pytest.fixture( @@ -29,6 +29,7 @@ def test_path_operation(client: TestClient): assert response.json() == {"id": "foo", "value": "there goes my hero"} +@workdir_lock def test_path_operation_img(client: TestClient): shutil.copy("./docs/en/docs/img/favicon.png", "./image.png") response = client.get("/items/foo?img=1") diff --git a/tests/test_tutorial/test_additional_responses/test_tutorial004.py b/tests/test_tutorial/test_additional_responses/test_tutorial004.py index fe56fbb8b5..c6d517f205 100644 --- a/tests/test_tutorial/test_additional_responses/test_tutorial004.py +++ b/tests/test_tutorial/test_additional_responses/test_tutorial004.py @@ -6,7 +6,7 @@ import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot -from tests.utils import needs_py310 +from tests.utils import needs_py310, workdir_lock @pytest.fixture( @@ -29,6 +29,7 @@ def test_path_operation(client: TestClient): assert response.json() == {"id": "foo", "value": "there goes my hero"} +@workdir_lock def test_path_operation_img(client: TestClient): shutil.copy("./docs/en/docs/img/favicon.png", "./image.png") response = client.get("/items/foo?img=1") diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial001.py b/tests/test_tutorial/test_background_tasks/test_tutorial001.py index 100583f77a..1fc919c399 100644 --- a/tests/test_tutorial/test_background_tasks/test_tutorial001.py +++ b/tests/test_tutorial/test_background_tasks/test_tutorial001.py @@ -4,10 +4,12 @@ from pathlib import Path from fastapi.testclient import TestClient from docs_src.background_tasks.tutorial001_py310 import app +from tests.utils import workdir_lock client = TestClient(app) +@workdir_lock def test(): log = Path("log.txt") if log.is_file(): diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial002.py b/tests/test_tutorial/test_background_tasks/test_tutorial002.py index f29402c833..31b58cde1f 100644 --- a/tests/test_tutorial/test_background_tasks/test_tutorial002.py +++ b/tests/test_tutorial/test_background_tasks/test_tutorial002.py @@ -5,7 +5,7 @@ from pathlib import Path import pytest from fastapi.testclient import TestClient -from ...utils import needs_py310 +from tests.utils import needs_py310, workdir_lock @pytest.fixture( @@ -22,6 +22,7 @@ def get_client(request: pytest.FixtureRequest): return client +@workdir_lock def test(client: TestClient): log = Path("log.txt") if log.is_file(): diff --git a/tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py b/tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py index f7eb7fd97d..ae25497f32 100644 --- a/tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py +++ b/tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py @@ -4,6 +4,8 @@ from pathlib import Path import pytest from fastapi.testclient import TestClient +from tests.utils import workdir_lock + @pytest.fixture(scope="module") def client(): @@ -17,6 +19,7 @@ def client(): static_dir.rmdir() +@workdir_lock def test_swagger_ui_html(client: TestClient): response = client.get("/docs") assert response.status_code == 200, response.text @@ -24,18 +27,21 @@ def test_swagger_ui_html(client: TestClient): assert "https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" in response.text +@workdir_lock def test_swagger_ui_oauth2_redirect_html(client: TestClient): response = client.get("/docs/oauth2-redirect") assert response.status_code == 200, response.text assert "window.opener.swaggerUIRedirectOauth2" in response.text +@workdir_lock def test_redoc_html(client: TestClient): response = client.get("/redoc") assert response.status_code == 200, response.text assert "https://unpkg.com/redoc@2/bundles/redoc.standalone.js" in response.text +@workdir_lock def test_api(client: TestClient): response = client.get("/users/john") assert response.status_code == 200, response.text diff --git a/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py b/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py index 28945d8387..ac67170d74 100644 --- a/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py +++ b/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py @@ -4,6 +4,8 @@ from pathlib import Path import pytest from fastapi.testclient import TestClient +from tests.utils import workdir_lock + @pytest.fixture(scope="module") def client(): @@ -17,6 +19,7 @@ def client(): static_dir.rmdir() +@workdir_lock def test_swagger_ui_html(client: TestClient): response = client.get("/docs") assert response.status_code == 200, response.text @@ -24,18 +27,21 @@ def test_swagger_ui_html(client: TestClient): assert "/static/swagger-ui.css" in response.text +@workdir_lock def test_swagger_ui_oauth2_redirect_html(client: TestClient): response = client.get("/docs/oauth2-redirect") assert response.status_code == 200, response.text assert "window.opener.swaggerUIRedirectOauth2" in response.text +@workdir_lock def test_redoc_html(client: TestClient): response = client.get("/redoc") assert response.status_code == 200, response.text assert "/static/redoc.standalone.js" in response.text +@workdir_lock def test_api(client: TestClient): response = client.get("/users/john") assert response.status_code == 200, response.text diff --git a/tests/test_tutorial/test_events/test_tutorial002.py b/tests/test_tutorial/test_events/test_tutorial002.py index 47af7a9527..262d936f98 100644 --- a/tests/test_tutorial/test_events/test_tutorial002.py +++ b/tests/test_tutorial/test_events/test_tutorial002.py @@ -3,6 +3,8 @@ from fastapi import FastAPI from fastapi.testclient import TestClient from inline_snapshot import snapshot +from tests.utils import workdir_lock + @pytest.fixture(name="app", scope="module") def get_app(): @@ -11,6 +13,7 @@ def get_app(): yield app +@workdir_lock def test_events(app: FastAPI): with TestClient(app) as client: response = client.get("/items/") @@ -20,6 +23,7 @@ def test_events(app: FastAPI): assert "Application shutdown" in log.read() +@workdir_lock def test_openapi_schema(app: FastAPI): with TestClient(app) as client: response = client.get("/openapi.json") diff --git a/tests/test_tutorial/test_static_files/test_tutorial001.py b/tests/test_tutorial/test_static_files/test_tutorial001.py index 1d57310f30..d2b120fbb9 100644 --- a/tests/test_tutorial/test_static_files/test_tutorial001.py +++ b/tests/test_tutorial/test_static_files/test_tutorial001.py @@ -5,6 +5,8 @@ import pytest from fastapi.testclient import TestClient from inline_snapshot import snapshot +from tests.utils import workdir_lock + @pytest.fixture(scope="module") def client(): @@ -20,17 +22,20 @@ def client(): static_dir.rmdir() +@workdir_lock def test_static_files(client: TestClient): response = client.get("/static/sample.txt") assert response.status_code == 200, response.text assert response.text == "This is a sample static file." +@workdir_lock def test_static_files_not_found(client: TestClient): response = client.get("/static/non_existent_file.txt") assert response.status_code == 404, response.text +@workdir_lock def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text diff --git a/tests/test_tutorial/test_templates/test_tutorial001.py b/tests/test_tutorial/test_templates/test_tutorial001.py index 04bc3fce86..f0377e963c 100644 --- a/tests/test_tutorial/test_templates/test_tutorial001.py +++ b/tests/test_tutorial/test_templates/test_tutorial001.py @@ -3,7 +3,10 @@ import shutil from fastapi.testclient import TestClient +from tests.utils import workdir_lock + +@workdir_lock def test_main(): if os.path.isdir("./static"): # pragma: nocover shutil.rmtree("./static") diff --git a/tests/utils.py b/tests/utils.py index 09c4e13b00..fff7348b9c 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -9,6 +9,8 @@ needs_py314 = pytest.mark.skipif( sys.version_info < (3, 14), reason="requires python3.14+" ) +workdir_lock = pytest.mark.xdist_group("workdir_lock") + def skip_module_if_py_gte_314(): """Skip entire module on Python 3.14+ at import time."""