Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to specify a default rerun filter #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions flaky/_flaky_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ def add_force_flaky_options(add_option):
"least this many times (unless the test has its own flaky "
"decorator)."
)
add_option(
'--rerun-filter',
action='store',
dest='rerun_filter',
help='If --force-flaky is specified, use this as the rerun filter '
'function to determine if a test should be retried.')

def _add_flaky_report(self, stream):
"""
Expand Down Expand Up @@ -608,7 +614,7 @@ def _get_test_callable_name(test):
raise NotImplementedError # pragma: no cover

@classmethod
def _make_test_flaky(cls, test, max_runs, min_passes):
def _make_test_flaky(cls, test, max_runs, min_passes, rerun_filter=None):
"""
Make a given test flaky.

Expand All @@ -624,7 +630,13 @@ def _make_test_flaky(cls, test, max_runs, min_passes):
The value of the FlakyNames.MIN_PASSES attribute to use.
:type min_passes:
`int`
"""
attrib_dict = defaults.default_flaky_attributes(max_runs, min_passes)
:param rerun_filter:
Rerun filter function to use to determine if a test should
run again.
:type rerun_filter:
`function`
"""
attrib_dict = defaults.default_flaky_attributes(
max_runs, min_passes, rerun_filter=rerun_filter)
for attr, value in attrib_dict.items():
cls._set_flaky_attribute(test, attr, value)
8 changes: 8 additions & 0 deletions flaky/flaky_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from flaky._flaky_plugin import _FlakyPlugin

import sys


class FlakyXdist(object):

Expand Down Expand Up @@ -34,6 +36,7 @@ class FlakyPlugin(_FlakyPlugin):
max_runs = None
min_passes = None
config = None
rerun_filter = None
_call_infos = {}
_PYTEST_WHEN_CALL = 'call'
_PYTEST_OUTCOME_PASSED = 'passed'
Expand Down Expand Up @@ -71,6 +74,7 @@ def pytest_runtest_protocol(self, item, nextitem):
item,
self.max_runs,
self.min_passes,
rerun_filter=self.rerun_filter,
)
original_call_and_report = self.runner.call_and_report
self._call_infos[item] = {}
Expand Down Expand Up @@ -198,6 +202,10 @@ def pytest_configure(self, config):
self.max_runs = config.option.max_runs
self.min_passes = config.option.min_passes
self.runner = config.pluginmanager.getplugin("runner")
if config.option.rerun_filter:
module, attr = config.option.rerun_filter.rsplit('.', 1)
__import__(module)
self.rerun_filter = getattr(sys.modules[module], attr)
if config.pluginmanager.hasplugin('xdist'):
config.pluginmanager.register(FlakyXdist(self), name='flaky.xdist')
self.config = config
Expand Down