]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for background_tasks (#13166)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Wed, 8 Jan 2025 19:23:42 +0000 (19:23 +0000)
committerGitHub <noreply@github.com>
Wed, 8 Jan 2025 19:23:42 +0000 (20:23 +0100)
tests/test_tutorial/test_background_tasks/test_tutorial002.py
tests/test_tutorial/test_background_tasks/test_tutorial002_an.py [deleted file]
tests/test_tutorial/test_background_tasks/test_tutorial002_an_py310.py [deleted file]
tests/test_tutorial/test_background_tasks/test_tutorial002_an_py39.py [deleted file]
tests/test_tutorial/test_background_tasks/test_tutorial002_py310.py [deleted file]

index 74de1314bc465c680108760941cbe6bf510b317e..d5ef51ee2b8dab14ad6949dbccf6760fdad8c705 100644 (file)
@@ -1,14 +1,31 @@
+import importlib
 import os
 from pathlib import Path
 
+import pytest
 from fastapi.testclient import TestClient
 
-from docs_src.background_tasks.tutorial002 import app
+from ...utils import needs_py39, needs_py310
 
-client = TestClient(app)
 
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial002",
+        pytest.param("tutorial002_py310", marks=needs_py310),
+        "tutorial002_an",
+        pytest.param("tutorial002_an_py39", marks=needs_py39),
+        pytest.param("tutorial002_an_py310", marks=needs_py310),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.background_tasks.{request.param}")
 
-def test():
+    client = TestClient(mod.app)
+    return client
+
+
+def test(client: TestClient):
     log = Path("log.txt")
     if log.is_file():
         os.remove(log)  # pragma: no cover
diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial002_an.py b/tests/test_tutorial/test_background_tasks/test_tutorial002_an.py
deleted file mode 100644 (file)
index af682ec..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-import os
-from pathlib import Path
-
-from fastapi.testclient import TestClient
-
-from docs_src.background_tasks.tutorial002_an import app
-
-client = TestClient(app)
-
-
-def test():
-    log = Path("log.txt")
-    if log.is_file():
-        os.remove(log)  # pragma: no cover
-    response = client.post("/send-notification/foo@example.com?q=some-query")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"message": "Message sent"}
-    with open("./log.txt") as f:
-        assert "found query: some-query\nmessage to foo@example.com" in f.read()
diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial002_an_py310.py b/tests/test_tutorial/test_background_tasks/test_tutorial002_an_py310.py
deleted file mode 100644 (file)
index 067b278..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-import os
-from pathlib import Path
-
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@needs_py310
-def test():
-    from docs_src.background_tasks.tutorial002_an_py310 import app
-
-    client = TestClient(app)
-    log = Path("log.txt")
-    if log.is_file():
-        os.remove(log)  # pragma: no cover
-    response = client.post("/send-notification/foo@example.com?q=some-query")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"message": "Message sent"}
-    with open("./log.txt") as f:
-        assert "found query: some-query\nmessage to foo@example.com" in f.read()
diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial002_an_py39.py b/tests/test_tutorial/test_background_tasks/test_tutorial002_an_py39.py
deleted file mode 100644 (file)
index 06b5a2f..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-import os
-from pathlib import Path
-
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@needs_py39
-def test():
-    from docs_src.background_tasks.tutorial002_an_py39 import app
-
-    client = TestClient(app)
-    log = Path("log.txt")
-    if log.is_file():
-        os.remove(log)  # pragma: no cover
-    response = client.post("/send-notification/foo@example.com?q=some-query")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"message": "Message sent"}
-    with open("./log.txt") as f:
-        assert "found query: some-query\nmessage to foo@example.com" in f.read()
diff --git a/tests/test_tutorial/test_background_tasks/test_tutorial002_py310.py b/tests/test_tutorial/test_background_tasks/test_tutorial002_py310.py
deleted file mode 100644 (file)
index 6937c8f..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-import os
-from pathlib import Path
-
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@needs_py310
-def test():
-    from docs_src.background_tasks.tutorial002_py310 import app
-
-    client = TestClient(app)
-    log = Path("log.txt")
-    if log.is_file():
-        os.remove(log)  # pragma: no cover
-    response = client.post("/send-notification/foo@example.com?q=some-query")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"message": "Message sent"}
-    with open("./log.txt") as f:
-        assert "found query: some-query\nmessage to foo@example.com" in f.read()