Skip to content

Commit

Permalink
test: if a test fails randomly, let it retry with @flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Feb 20, 2024
1 parent 65d686c commit 5d69334
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/helpers.py
Expand Up @@ -23,6 +23,7 @@
TypeVar, Union, cast,
)

import flaky
import pytest

from coverage import env
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions tests/test_concurrency.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d69334

Please sign in to comment.