Package: pytest-dynamicrerun
pytest-dynamicrerun is apytestplugin to rerun tests dynamically based off of test outcome and output.Supported versionsThis plugin is tested against the following Python and pytest versions. Each Python version is tested against all pytest versions. Pleasefile an issueto request additional targets.Python Versions:python 3.5,
python 3.6,
python 3.7,
python 3.8,
pypy3Pytest Versions:5.4.0,
5.4.1,
5.4.2,
5.4.3,
6.0.0,
6.0.1InstallationInstall this plugin fromPyPIby running the following:$ pip install pytest-dynamicrerunUsageSpecifying how many times to rerunBy default, one rerun attempt is made. You can set the amount of times to attempt a rerun by passing the--dynamic-rerun-attemptsflag when invoking pytest or including thedynamic_rerun_attemptsINI key.To pass the flag:python3 -m pytest --dynamic-rerun-attempts=3To set the INI key add the following to your config file’s[pytest]section:[pytest]
dynamic_rerun_attempts = 123Passing a non positive integer value will set the number of rerun attempts to the default.Specifying what to rerun onBy default, all failed tests are rerun. You can change this behavior by either passing the--dynamic-rerun-triggersflag when invokingpytestor including thedynamic_rerun_triggersINI key. Note that regular expressions are allowed.To pass the flag:python3 -m pytest --dynamic-rerun-triggers="a triggering trace"To set the INI key add the following to your config file’s[pytest]section:[pytest]
dynamic_rerun_triggers = a triggering traceYou can accumulate values by either providing the flag multiple times or appending to the INI key. For example, the below two snippets would cause this plugin to trigger on bothfooandbar:python3 -m pytest --dynamic-rerun-triggers="foo" --dynamic-rerun-triggers="bar"

[pytest]
dynamic_rerun_triggers = foo
    barNote that at this time onlystdout,stderr, and exceptions are checked.Specifying a rerun intervalYou can specify an interval to rerun tests on by either passing the--dynamic-rerun-scheduleflag to python when invokingpytestor including thedyanmic_rerun_scheduleINI key.Internally, this plugin usescroniterto schedule wait times. Because of this, we are able to schedule wait times with second level granularity. Visit the croniter repositoryREADMEto find out more information on this.To pass the flag:python3 -m pytest --dynamic-rerun-schedule="* * * * * *"To set the INI key add the following to your config file’s[pytest]section:[pytest]
dynamic_rerun_schedule = * * * * * *Note that any valid cron schedule is accepted. If this flag is not passed or set in the INI file, this plugin will not take effect. Passing an invalid value will force the interval to default to* * * * * *( every second ).Ignoring this pluginYou can ignore this plugin by passing the--dynamic-rerun-disabledflag to python when invokingpytestor including thedynamic_rerun_disabledINI key.To pass the flag:python3 -m pytest --dynamic-rerun-disabled="True"To set the INI key add the following to your config file’s[pytest]section:[pytest]
dynamic_rerun_disabled = TrueNote that if this flag is omitted, we do not disable the plugin ( so it is equivalent to passing--dynamic-rerun-disabled=False)Using markers to rerun testsWe can achieve the above functionality through markers as well. This plugin defines thedynamicrerunmark, which can be used as follows:@pytest.mark.dynamicrerun(attempts=10, disabled=False, schedule="* * * * * *", triggers="foo")
def test_print_foo():
    print("foo")Mark arguments correspond to INI keys as follows:attemptscorresponds todynamic_rerun_attemptsdisabledcorresponds todynanic_rerun_disabledschedulecorresponds todynamic_rerun_scheduletriggerscorresponds todynamic_rerun_triggersTo pass multiple values to thetriggersargument, provide a list as so:@pytest.mark.dynamicrerun(attempts=10, disabled=False, schedule="* * * * * *", triggers=["foo", "bar", "baz"])
def test_print_foo():
    print("foo")In the above example, reruns will be triggered onfoo,bar, andbaz.Argument precedenceNote that first we check for arguments in markers, then command line switches, and only then do we check in INI files. Values found at lower levels ( those checked first ) take precedence over values defined at a higher level ( those checked later ).For example, if we define the number of rerun attempts as 10 when invokingpytestfrom the command line, but later in a marker define the number of rerun attempts as 3, 3 would take precedence over 10 since we check markers before we check command line flags.Developing against this pluginThis plugin exposes the following attributes on theitemobject:dynamic_rerun_run_times ( list ): The list of times this item was run by the plugin. Note this includes the original non dynamically rerun run.dynamic_rerun_schedule(string): The schedule to rerun this item on. See the sectionSpecifying a rerun intervalabove for more details.dynamic_rerun_sleep_times (list): A list oftimedelta objectsrepresenting the time slept in between reruns for the itemdynamic_rerun_triggers (list): The rerun triggers for this specific item. See the sectionSpecifying what to rerun onabove for more details.max_allowed_dynamic_rerun_attempts(int): The maximum amount of times we are allowed to rerun this item. See the sectionSpecifying how many times to rerunabove for more details.num_dynamic_reruns_kicked_off (int): The amount of reruns launched at the moment of inspection for this item.This plugin exposes the following attributes on thesessionobject:dynamic_rerun_items (list): The list of items that are set to be dynamically rerun on the next iterationContributingContributions are always welcome. Tests can be run withtox.Please remember to add achangelogentry when adding a non-trivial feature.pre-commitis used to ensure basic checks pass.LicenseDistributed under the terms of theMITlicense, “pytest-dynamicrerun” is free and open source softwareIssuesIf you encounter any problems, pleasefile an issuealong with a detailed description.