Skip to content

Commit

Permalink
Fix incompatibilities with newer dependencies (#201)
Browse files Browse the repository at this point in the history
This commit fixes an incompatibility with pytest 8.1.1,
and  some with python 3.12.

This commit also fixes up some pycodestyle and
pylint problems that snuck in with some earlier commits.

Fixes #199 and #198
---------

Co-authored-by: Jonathan Karlsen <JONAK@equinor.com>
  • Loading branch information
Jeff-Meadows and jonathan-eq authored Mar 10, 2024
1 parent b61e722 commit 2bb1514
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,4 @@ int-import-graph=

# Exceptions that will emit a warning when being caught. Defaults to
# "Exception"
overgeneral-exceptions=Exception
overgeneral-exceptions=builtins.Exception
18 changes: 17 additions & 1 deletion flaky/flaky_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,23 @@ def call_and_report(self, item, when, log=True, **kwds):
:type log:
`bool`
"""
call = runner.call_runtest_hook(item, when, **kwds)
def _call_runtest_hook(item, when, **kwds):
if when == "setup":
ihook = item.ihook.pytest_runtest_setup
elif when == "call":
ihook = item.ihook.pytest_runtest_call
elif when == "teardown":
ihook = item.ihook.pytest_runtest_teardown
else:
assert False, f"Unhandled runtest hook case: {when}"
reraise = (runner.Exit,)
if not item.config.getoption("usepdb", False):
reraise += (KeyboardInterrupt,)
return runner.CallInfo.from_call(
lambda: ihook(item=item, **kwds), when=when, reraise=reraise
)

call = _call_runtest_hook(item, when, **kwds)
self._call_infos[item][when] = call
hook = item.ihook
report = hook.pytest_runtest_makereport(item=item, call=call)
Expand Down
6 changes: 3 additions & 3 deletions test/test_flaky_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def test_something():
}

self.assertIsNotNone(flaky_attribute)
self.assertDictContainsSubset(
self.assertLessEqual(
{
FlakyNames.MIN_PASSES: min_passes,
FlakyNames.MAX_RUNS: max_runs,
FlakyNames.CURRENT_PASSES: 0,
FlakyNames.CURRENT_RUNS: 0,
FlakyNames.CURRENT_ERRORS: None
},
flaky_attribute
}.items(),
flaky_attribute.items()
)
27 changes: 15 additions & 12 deletions test/test_flaky_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
from flaky._flaky_plugin import _FlakyPlugin
from flaky.names import FlakyNames

TestCaseDataset = namedtuple("TestCaseDataset",
['max_runs', 'min_passes', 'current_runs', 'current_passes', 'expect_fail'])
TestCaseDataset = namedtuple(
"TestCaseDataset",
['max_runs', 'min_passes', 'current_runs', 'current_passes', 'expect_fail'],
)


class TestFlakyPlugin(TestCase):
_test_dataset = (
_test_dataset = {
"default_not_started": TestCaseDataset(2, 1, 0, 0, False),
"default_one_failure": TestCaseDataset(2, 1, 1, 0, False),
"default_one_success": TestCaseDataset(2, 1, 1, 1, False),
Expand All @@ -21,10 +24,10 @@ class TestFlakyPlugin(TestCase):
"three_two_two_failures": TestCaseDataset(3, 2, 2, 0, True),
"three_two_one_failure_one_success": TestCaseDataset(3, 2, 2, 1, False),
"three_two_two_successes": TestCaseDataset(3, 2, 2, 2, False),
)
}

def setUp(self):
super(TestFlakyPlugin, self).setUp()
super().setUp()
self._flaky_plugin = _FlakyPlugin()

def test_flaky_plugin_handles_non_ascii_byte_string_in_exception(self):
Expand All @@ -39,18 +42,18 @@ def test_flaky_plugin_handles_non_ascii_byte_string_in_exception(self):
)

def test_flaky_plugin_identifies_failure(self):
for test in _test_dataset:
with self.subTest(test):
for name, test in self._test_dataset:
with self.subTest(name):
flaky = {
FlakyNames.CURRENT_PASSES: _test_dataset[test].current_passes,
FlakyNames.CURRENT_RUNS: _test_dataset[test].current_runs,
FlakyNames.MAX_RUNS: _test_dataset[test].max_runs,
FlakyNames.MIN_PASSES: _test_dataset[test].min_passes,
FlakyNames.CURRENT_PASSES: test.current_passes,
FlakyNames.CURRENT_RUNS: test.current_runs,
FlakyNames.MAX_RUNS: test.max_runs,
FlakyNames.MIN_PASSES: test.min_passes,
}
# pylint:disable=protected-access
self.assertEqual(
self._flaky_plugin._has_flaky_test_failed(flaky),
_test_dataset[test].expect_fail,
test.expect_fail,
)

def test_write_unicode_to_stream(self):
Expand Down
24 changes: 12 additions & 12 deletions test/test_multiprocess_string_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class TestMultiprocessStringIO(TestCase):
_unicode_string = 'Plain Hello'
_unicode_string_non_ascii = 'ńőń ȁŝćȉȉ ŝƭȕƒƒ'
_test_values = {
"no_writes": ([], ''),
"one_write": ([_unicode_string], _unicode_string),
"two_writes": (
[_unicode_string, _unicode_string_non_ascii],
'{}{}'.format(_unicode_string, _unicode_string_non_ascii),
)
"no_writes": ([], ''),
"one_write": ([_unicode_string], _unicode_string),
"two_writes": (
[_unicode_string, _unicode_string_non_ascii],
'{}{}'.format(_unicode_string, _unicode_string_non_ascii),
),
}

def setUp(self):
Expand All @@ -23,16 +23,16 @@ def setUp(self):
self._string_ios = (self._string_io, self._mp_string_io)

def test_write_then_read(self):
for name in _test_values:
for name, value in self._test_values.items():
with self.subTest(name):
for string_io in self._string_ios:
for item in _test_values[name][0]:
for item in value[0]:
string_io.write(item)
self.assertEqual(string_io.getvalue(), _test_values[name][1])
self.assertEqual(string_io.getvalue(), value[1])

def test_writelines_then_read(self):
for name in _test_values:
for name, value in self._test_values.items():
with self.subTest(name):
for string_io in self._string_ios:
string_io.writelines(_test_values[name][0])
self.assertEqual(string_io.getvalue(), _test_values[name][1])
string_io.writelines(value[0])
self.assertEqual(string_io.getvalue(), value[1])
2 changes: 1 addition & 1 deletion test/test_pytest/test_flaky_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def error_raising_setup_function(item):
flaky_test.ihook = Mock()
flaky_test.ihook.pytest_runtest_setup = error_raising_setup_function
flaky_plugin._call_infos[flaky_test] = {} # pylint:disable=protected-access
call_info = runner.call_runtest_hook(flaky_test, 'setup')
call_info = runner.CallInfo.from_call(lambda: flaky_test.ihook.pytest_runtest_setup(flaky_test), when='setup')
assert flaky_test.ran_setup
assert string_io.getvalue() == mock_io.getvalue()
assert call_info.excinfo.type is ZeroDivisionError
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ envlist =
py39,
py310,
py311,
py312,
pypy310,
pycodestyle,
pylint,
Expand Down Expand Up @@ -31,6 +32,7 @@ commands =

[testenv:coverage]
commands =
pip install setuptools
python setup.py develop
pytest -k 'example and not options' --doctest-modules --no-flaky-report --cov flaky --cov-report term-missing test/test_pytest/
pytest -p no:flaky --cov flaky --cov-report term-missing test/test_pytest/test_flaky_pytest_plugin.py
Expand Down

0 comments on commit 2bb1514

Please sign in to comment.