box/flaky

allow flaky to be used as a context manager

ssbarnea opened this issue · 1 comments

Sometimes we may want to use retry only some blocks instead of entire methods and it would be great if flaky could be used as a context manager in addition to as a decorator.

This functionality is basically available using the rerun_filter.

Here's a quick example:

@contextmanager
def flaky_section():
    try:
        yield
    except Exception as ex:
        raise FlakySectionError from ex

def flaky_section_rerun_filter(err, name, test, plugin):
    return issubclass(err[0], FlakySectionError)

@flaky(rerun_filter=flaky_section_rerun_filter)
def test_something_that_fails_inside_the_flaky_section(dummy_list=[]):
    with flaky_section():
        dummy_list.append(0)
        assert len(dummy_list) > 1

@flaky(rerun_filter=flaky_section_rerun_filter)
def test_something_that_fails_outside_the_flaky_section(dummy_list=[]):
    dummy_list.append(0)
    assert len(dummy_list) > 1

test_something_that_fails_inside_the_flaky_section will pass when run with flaky. test_something_that_fails_outside_the_flaky_section will fail.