]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add fixture to help retrying flaky tests
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 28 Feb 2021 14:11:13 +0000 (15:11 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 28 Feb 2021 14:11:13 +0000 (15:11 +0100)
Tried to use a couple of pytest plugins for retrying but they don't work
well with asyncio.

psycopg3/setup.py
tests/conftest.py

index 368cfedc465c1be7206ab1233eb91c7834baf7e6..eaed1903ef805ddcaa4015b60f5f6d3547a19e3e 100644 (file)
@@ -35,6 +35,7 @@ extras_require = {
         "pytest >= 6, < 6.1",
         "pytest-asyncio >= 0.14.0, < 0.15",
         "pytest-randomly >= 3.5, < 3.6",
+        "tenacity >= 6.3, < 6.4",
     ],
     "dev": [
         "black",
index 91b87b6924049d2b7328b486602575439f991eb6..98d222858f9d388e8aa770413e562f275a0319c0 100644 (file)
@@ -1,3 +1,7 @@
+import inspect
+
+import pytest
+
 pytest_plugins = (
     "tests.fix_db",
     "tests.fix_pq",
@@ -16,3 +20,18 @@ def pytest_configure(config):
     config.addinivalue_line(
         "markers", "subprocess: the test import psycopg3 after subprocess"
     )
+
+
+@pytest.fixture
+def retries(request):
+    """Retry a block in a test a few times before giving up."""
+    import tenacity
+
+    if inspect.iscoroutinefunction(request.function):
+        return tenacity.AsyncRetrying(
+            reraise=True, stop=tenacity.stop_after_attempt(3)
+        )
+    else:
+        return tenacity.Retrying(
+            reraise=True, stop=tenacity.stop_after_attempt(3)
+        )