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

added condition arg for flaky mark #15

Merged
merged 1 commit into from
Aug 22, 2023
Merged
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ def test_unreliable_service():
...
```

If you want some other generalized condition to control whether a test is retried, use the
`condition` argument. Any statement which results in a bool can be used here to add granularity
to your retries. The test will only be retried if `condition` is `True`. Note, there is no
matching command line option for `condition`, but if you need to globally apply this type of logic
to all of your tests, consider invoking the `pytest_collection_modifyitems` hook.

```
@pytest.mark.flaky(retries=2, condition=sys.platform.startswith('win32'))
def test_only_flaky_on_some_systems():
# This test will only be retried if sys.platform.startswith('win32') evaluates to `True`
```

Finally, there is a flaky mark argument for the test timing method, which can either
be `overwrite` (default) or `cumulative`. See **Command Line** > **Advanced Options**
for more information
Expand Down
16 changes: 10 additions & 6 deletions pytest_retry/retry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,21 @@ def pytest_runtest_makereport(
retry_manager.record_node_stats(original_report)
# Set dynamic outcome for each stage until runtest protocol has completed.
item.stash[outcome_key] = original_report.outcome

if not should_handle_retry(original_report):
return
flake_mark = item.get_closest_marker("flaky")

flake_mark = item.get_closest_marker("flaky")
if flake_mark is None:
return

condition = flake_mark.kwargs.get("condition")
if condition is False:
return

exception_filter = ExceptionFilter(
flake_mark.kwargs.get("only_on", []),
flake_mark.kwargs.get("exclude", []),
) or ExceptionFilter(Defaults.FILTERED_EXCEPTIONS, Defaults.EXCLUDED_EXCEPTIONS)

if not exception_filter(call.excinfo.type): # type: ignore
return

Expand Down Expand Up @@ -268,10 +271,11 @@ def pytest_report_teststatus(
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line(
"markers",
"flaky(retries=1, delay=0, only_on=..., exclude=...): indicate a flaky test which "
"will be retried the number of times specified with an (optional) specified "
"flaky(retries=1, delay=0, only_on=..., exclude=..., condition=...): indicate a flaky "
"test which will be retried the number of times specified with an (optional) specified "
"delay between each attempt. Collections of one or more exceptions can be passed so "
"that the test is retried only on those exceptions, or excluding those exceptions.",
"that the test is retried only on those exceptions, or excluding those exceptions. "
"Any statement which returns a bool can be used as a condition",
)
if config.getoption("verbose"):
# if pytest config has -v enabled, then don't limit traceback length
Expand Down
30 changes: 30 additions & 0 deletions tests/test_retry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,33 @@ def pytest_report_teststatus(report: pytest.TestReport):
result = testdir.runpytest("--retries", "2", "--cumulative-timing", "1")

assert_outcomes(result, passed=1, retried=1)


def test_conditional_flaky_marks_evaluate_correctly(testdir):
testdir.makepyfile(
"""
import pytest

a = []
b = []
c = []

@pytest.mark.flaky(retries=2, condition=True)
def test_eventually_passes():
a.append(1)
assert len(a) > 2

@pytest.mark.flaky(retries=2, condition=True)
def test_eventually_passes_again():
b.append(1)
assert len(b) > 2

@pytest.mark.flaky(retries=2, condition=False)
def test_eventually_passes_once_more():
c.append(1)
assert len(c) > 2
"""
)
result = testdir.runpytest()

assert_outcomes(result, passed=2, failed=1, retried=2)
Loading