From: Daniele Varrazzo Date: Sun, 28 Feb 2021 14:11:13 +0000 (+0100) Subject: Add fixture to help retrying flaky tests X-Git-Tag: 3.0.dev0~99 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=944ca487bbd339e84eb33a28821aff795f104bd8;p=thirdparty%2Fpsycopg.git Add fixture to help retrying flaky tests Tried to use a couple of pytest plugins for retrying but they don't work well with asyncio. --- diff --git a/psycopg3/setup.py b/psycopg3/setup.py index 368cfedc4..eaed1903e 100644 --- a/psycopg3/setup.py +++ b/psycopg3/setup.py @@ -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", diff --git a/tests/conftest.py b/tests/conftest.py index 91b87b692..98d222858 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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) + )