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

Fix flaky tests with asyncio #179

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions flaky/flaky_pytest_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from _pytest import runner # pylint:disable=import-error

from flaky._flaky_plugin import _FlakyPlugin
Expand Down Expand Up @@ -130,6 +132,7 @@ def call_and_report(self, item, when, log=True, **kwds):
:type log:
`bool`
"""
self._reset_test_event_loop(item)
call = runner.call_runtest_hook(item, when, **kwds)
self._call_infos[item][when] = call
hook = item.ihook
Expand Down Expand Up @@ -399,6 +402,12 @@ def _log_test_failure(self, test_callable_name, err, message):
'\n',
])

@staticmethod
def _reset_test_event_loop(item):
if 'asyncio' in item.keywords and 'event_loop' in item.funcargs:
# always use new loops for every run
item.funcargs['event_loop'] = asyncio.new_event_loop()


PLUGIN = FlakyPlugin()
# pytest only processes hooks defined on the module
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ordereddict
pycodestyle
pylint
pytest
pytest-asyncio
pytest-cov
pytest-xdist
tox
11 changes: 11 additions & 0 deletions test/test_pytest/test_pytest_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ def test_flaky_thing_that_succeeds_then_fails_then_succeeds(self):
TestExample._threshold += 1
assert TestExample._threshold != 1

@pytest.mark.asyncio
@flaky(3, 2)
async def test_flaky_asyncio_thing_that_fails_then_succeeds(self):
"""
Flaky will run this test 3 times.
It will fail once and then succeed twice.
"""
# pylint:disable=no-self-use
TestExample._threshold += 1
assert TestExample._threshold >= 1

@flaky(2, 2)
def test_flaky_thing_that_always_passes(self):
"""Flaky will run this test twice. Both will succeed."""
Expand Down