Skip to content

Commit

Permalink
Automatically retry flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Nov 7, 2023
1 parent 1375c03 commit a6cdef8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,26 @@ def decorated(self, *args, **kwargs):
return decorated


def flaky(note=''):
def flaky(note='', retries=3):
assert not callable(note)

if EMTEST_SKIP_FLAKY:
return unittest.skip(note)
return lambda f: f

def decorated(f):
@wraps(f)
def modified(*args, **kwargs):
for i in range(retries):
try:
return f(*args, **kwargs)
except AssertionError as exc:
preserved_exc = exc
logging.info(f'Retrying flaky test (attempt {i}/{retries} failed): {exc}')
raise AssertionError('Flaky test has failed too many times') from preserved_exc

return modified

return decorated


def disabled(note=''):
Expand Down Expand Up @@ -288,6 +303,7 @@ def with_env_modify(updates):
assert not callable(updates)

def decorated(f):
@wraps(f)
def modified(self, *args, **kwargs):
with env_modify(updates):
return f(self, *args, **kwargs)
Expand Down

0 comments on commit a6cdef8

Please sign in to comment.