From 5d6933451e4b2ba1294fdbd9b33473e6a254a19b Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 20 Feb 2024 12:56:11 -0500 Subject: [PATCH] test: if a test fails randomly, let it retry with @flaky --- tests/helpers.py | 10 ++++++++++ tests/test_concurrency.py | 2 ++ 2 files changed, 12 insertions(+) diff --git a/tests/helpers.py b/tests/helpers.py index 9e6e2e8de..5a4f6c66a 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -23,6 +23,7 @@ TypeVar, Union, cast, ) +import flaky import pytest from coverage import env @@ -397,3 +398,12 @@ def __init__(self, options: Iterable[str]) -> None: def get_output(self) -> str: """Get the output text from the `DebugControl`.""" return self.io.getvalue() + + +TestMethod = Callable[[Any], None] + +def flaky_method(max_runs: int) -> Callable[[TestMethod], TestMethod]: + """flaky.flaky, but with type annotations.""" + def _decorator(fn: TestMethod) -> TestMethod: + return cast(TestMethod, flaky.flaky(max_runs)(fn)) + return _decorator diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index db2856538..2bec5fb87 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -30,6 +30,7 @@ from tests import testenv from tests.coveragetest import CoverageTest +from tests.helpers import flaky_method # These libraries aren't always available, we'll skip tests if they aren't. @@ -317,6 +318,7 @@ def do(): """ self.try_some_code(BUG_330, "eventlet", eventlet, "0\n") + @flaky_method(max_runs=3) # Sometimes a test fails due to inherent randomness. Try more times. def test_threads_with_gevent(self) -> None: self.make_file("both.py", """\ import queue