From 825e2290f3cfe08337203eada7eea343970fabc6 Mon Sep 17 00:00:00 2001 From: alv2017 Date: Wed, 26 Mar 2025 14:49:20 +0200 Subject: [PATCH 1/2] update tutorial tests: tests/test_tutorial/test_background_tasks --- tests/test_tutorial/test_background_tasks/conftest.py | 11 +++++++++++ .../test_background_tasks/test_tutorial001.py | 8 +------- .../test_background_tasks/test_tutorial002.py | 7 +------ 3 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 tests/test_tutorial/test_background_tasks/conftest.py diff --git a/tests/test_tutorial/test_background_tasks/conftest.py b/tests/test_tutorial/test_background_tasks/conftest.py new file mode 100644 index 000000000..5106d7e04 --- /dev/null +++ b/tests/test_tutorial/test_background_tasks/conftest.py @@ -0,0 +1,11 @@ +import os +import pytest +from pathlib import Path + + +@pytest.fixture(name="path_to_log_file") +def log_path(): + log = Path("log.txt") + yield log + if log.is_file(): + os.remove(log) # pragma: no cover \ No newline at end of file diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial001.py b/tests/test_tutorial/test_background_tasks/test_tutorial001.py index 0602cd8aa..87e1c39d9 100644 --- a/tests/test_tutorial/test_background_tasks/test_tutorial001.py +++ b/tests/test_tutorial/test_background_tasks/test_tutorial001.py @@ -1,6 +1,3 @@ -import os -from pathlib import Path - from fastapi.testclient import TestClient from docs_src.background_tasks.tutorial001 import app @@ -8,10 +5,7 @@ from docs_src.background_tasks.tutorial001 import app client = TestClient(app) -def test(): - log = Path("log.txt") - if log.is_file(): - os.remove(log) # pragma: no cover +def test(path_to_log_file): response = client.post("/send-notification/foo@example.com") assert response.status_code == 200, response.text assert response.json() == {"message": "Notification sent in the background"} diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial002.py b/tests/test_tutorial/test_background_tasks/test_tutorial002.py index d5ef51ee2..ebdebda34 100644 --- a/tests/test_tutorial/test_background_tasks/test_tutorial002.py +++ b/tests/test_tutorial/test_background_tasks/test_tutorial002.py @@ -1,6 +1,4 @@ import importlib -import os -from pathlib import Path import pytest from fastapi.testclient import TestClient @@ -25,10 +23,7 @@ def get_client(request: pytest.FixtureRequest): return client -def test(client: TestClient): - log = Path("log.txt") - if log.is_file(): - os.remove(log) # pragma: no cover +def test(client: TestClient, path_to_log_file): response = client.post("/send-notification/foo@example.com?q=some-query") assert response.status_code == 200, response.text assert response.json() == {"message": "Message sent"} From 6b8e66c725289be147d7e38c465babbe36000667 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Mar 2025 13:01:31 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_tutorial/test_background_tasks/conftest.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_tutorial/test_background_tasks/conftest.py b/tests/test_tutorial/test_background_tasks/conftest.py index 5106d7e04..88d9b4b72 100644 --- a/tests/test_tutorial/test_background_tasks/conftest.py +++ b/tests/test_tutorial/test_background_tasks/conftest.py @@ -1,11 +1,12 @@ import os -import pytest from pathlib import Path +import pytest + @pytest.fixture(name="path_to_log_file") def log_path(): log = Path("log.txt") yield log if log.is_file(): - os.remove(log) # pragma: no cover \ No newline at end of file + os.remove(log) # pragma: no cover