Skip to content

Commit

Permalink
Simplify flaky dependency (#197)
Browse files Browse the repository at this point in the history
* Remove dependency on genty external package.

Whole parametrization of tests can be done now easily with subTest
from the standard library.

* External package mock is not needed anymore.
  • Loading branch information
mcepl authored Mar 4, 2024
1 parent 69c297e commit b61e722
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 73 deletions.
2 changes: 0 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
-rrequirements.txt
coveralls
genty
mock
ordereddict
pycodestyle
pylint
Expand Down
83 changes: 40 additions & 43 deletions test/test_flaky_plugin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
from collections import namedtuple
from io import StringIO
from unittest import TestCase

from flaky._flaky_plugin import _FlakyPlugin
from flaky.names import FlakyNames

from genty import genty, genty_dataset
TestCaseDataset = namedtuple("TestCaseDataset",
['max_runs', 'min_passes', 'current_runs', 'current_passes', 'expect_fail'])


@genty
class TestFlakyPlugin(TestCase):
_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),
"default_two_failures": TestCaseDataset(2, 1, 2, 0, True),
"default_one_failure_one_success": TestCaseDataset(2, 1, 2, 1, False),
"three_two_not_started": TestCaseDataset(3, 2, 0, 0, False),
"three_two_one_failure": TestCaseDataset(3, 2, 1, 0, False),
"three_two_one_success": TestCaseDataset(3, 2, 1, 1, False),
"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().setUp()
super(TestFlakyPlugin, self).setUp()
self._flaky_plugin = _FlakyPlugin()

def test_flaky_plugin_handles_non_ascii_byte_string_in_exception(self):
Expand All @@ -24,43 +38,26 @@ def test_flaky_plugin_handles_non_ascii_byte_string_in_exception(self):
mock_message,
)

@genty_dataset(
default_not_started=(2, 1, 0, 0, False),
default_one_failure=(2, 1, 1, 0, False),
default_one_success=(2, 1, 1, 1, False),
default_two_failures=(2, 1, 2, 0, True),
default_one_failure_one_success=(2, 1, 2, 1, False),
three_two_not_started=(3, 2, 0, 0, False),
three_two_one_failure=(3, 2, 1, 0, False),
three_two_one_success=(3, 2, 1, 1, False),
three_two_two_failures=(3, 2, 2, 0, True),
three_two_one_failure_one_success=(3, 2, 2, 1, False),
three_two_two_successes=(3, 2, 2, 2, False),
)
def test_flaky_plugin_identifies_failure(
self,
max_runs,
min_passes,
current_runs,
current_passes,
expect_fail,
):
flaky = {
FlakyNames.CURRENT_PASSES: current_passes,
FlakyNames.CURRENT_RUNS: current_runs,
FlakyNames.MAX_RUNS: max_runs,
FlakyNames.MIN_PASSES: min_passes,
}
# pylint:disable=protected-access
self.assertEqual(
self._flaky_plugin._has_flaky_test_failed(flaky),
expect_fail,
)
def test_flaky_plugin_identifies_failure(self):
for test in _test_dataset:
with self.subTest(test):
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,
}
# pylint:disable=protected-access
self.assertEqual(
self._flaky_plugin._has_flaky_test_failed(flaky),
_test_dataset[test].expect_fail,
)

@genty_dataset('ascii stuff', 'ńőń ȁŝćȉȉ ŝƭȕƒƒ')
def test_write_unicode_to_stream(self, message):
stream = StringIO()
stream.write('ascii stuff')
# pylint:disable=protected-access
self._flaky_plugin._stream.write(message)
self._flaky_plugin._add_flaky_report(stream)
def test_write_unicode_to_stream(self):
for message in ('ascii stuff', 'ńőń ȁŝćȉȉ ŝƭȕƒƒ'):
with self.subTest(message):
stream = StringIO()
stream.write('ascii stuff')
# pylint:disable=protected-access
self._flaky_plugin._stream.write(message)
self._flaky_plugin._add_flaky_report(stream)
49 changes: 21 additions & 28 deletions test/test_multiprocess_string_io.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from io import StringIO
from unittest import TestCase

from genty import genty, genty_dataset


@genty
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),
)
}

def setUp(self):
super().setUp()
Expand All @@ -17,29 +22,17 @@ def setUp(self):
del self._mp_string_io.proxy[:]
self._string_ios = (self._string_io, self._mp_string_io)

@genty_dataset(
no_writes=([], ''),
one_write=([_unicode_string], _unicode_string),
two_writes=(
[_unicode_string, _unicode_string_non_ascii],
'{}{}'.format(_unicode_string, _unicode_string_non_ascii),
)
)
def test_write_then_read(self, writes, expected_value):
for string_io in self._string_ios:
for item in writes:
string_io.write(item)
self.assertEqual(string_io.getvalue(), expected_value)
def test_write_then_read(self):
for name in _test_values:
with self.subTest(name):
for string_io in self._string_ios:
for item in _test_values[name][0]:
string_io.write(item)
self.assertEqual(string_io.getvalue(), _test_values[name][1])

@genty_dataset(
no_writes=([], ''),
one_write=([_unicode_string], _unicode_string),
two_writes=(
[_unicode_string, _unicode_string_non_ascii],
'{}{}'.format(_unicode_string, _unicode_string_non_ascii),
)
)
def test_writelines_then_read(self, lines, expected_value):
for string_io in self._string_ios:
string_io.writelines(lines)
self.assertEqual(string_io.getvalue(), expected_value)
def test_writelines_then_read(self):
for name in _test_values:
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])

0 comments on commit b61e722

Please sign in to comment.