From 83b2cac1617e63fc05d1dac5f6446c0d248dd2d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jiri=20Dan=C4=9Bk?= Date: Sun, 6 Feb 2022 22:31:38 +0100 Subject: [PATCH] Fixed #49: Prefer use of unittest.TestCase#setUp instead of __init__ in tests This started as a workaround for Pytest 7.0.0 stacktrace normalization bug https://github.com/pytest-dev/pytest/issues/9610 --- tests/router_engine_test.py | 2 ++ tests/system_test.py | 8 +++-- tests/system_tests_autolinks.py | 4 +-- tests/system_tests_bad_configuration.py | 43 +++++++------------------ tests/system_tests_connector_status.py | 4 +-- tests/system_tests_distribution.py | 2 +- tests/system_tests_edge_router.py | 4 +-- tests/system_tests_handle_failover.py | 4 +-- tests/system_tests_two_routers.py | 4 +-- tests/test_command.py | 2 ++ 10 files changed, 33 insertions(+), 44 deletions(-) diff --git a/tests/router_engine_test.py b/tests/router_engine_test.py index d543bd253..e68034e46 100644 --- a/tests/router_engine_test.py +++ b/tests/router_engine_test.py @@ -146,6 +146,7 @@ def neighbor_refresh(self, node_id, ProtocolVersion, instance, link_id, cost, no self.neighbors[node_id] = (instance, link_id, cost, now) def setUp(self): + super().setUp() self.sent = [] self.neighbors = {} self.id = "R1" @@ -210,6 +211,7 @@ def test_establish_multiple_peers(self): class PathTest(unittest.TestCase): def setUp(self): + super().setUp() self.id = 'R1' self.engine = PathEngine(self) diff --git a/tests/system_test.py b/tests/system_test.py index 315b3f4b3..ec92d12f0 100755 --- a/tests/system_test.py +++ b/tests/system_test.py @@ -837,12 +837,13 @@ def advance(): class TestCase(unittest.TestCase, Tester): # pylint: disable=too-many-public-methods """A TestCase that sets up its own working directory and is also a Tester.""" - def __init__(self, test_method): - unittest.TestCase.__init__(self, test_method) + def __init__(self, methodName='runTest'): + unittest.TestCase.__init__(self, methodName) Tester.__init__(self, self.id()) @classmethod def setUpClass(cls): + super().setUpClass() cls.maxDiff = None cls.tester = Tester('.'.join([cls.__module__, cls.__name__, 'setUpClass'])) cls.tester.rmtree() @@ -853,12 +854,15 @@ def tearDownClass(cls): if hasattr(cls, 'tester'): cls.tester.teardown() del cls.tester + super().tearDownClass() def setUp(self): + super().setUp() Tester.setup(self) def tearDown(self): Tester.teardown(self) + super().tearDown() def assert_fair(self, seq): avg = sum(seq) / len(seq) diff --git a/tests/system_tests_autolinks.py b/tests/system_tests_autolinks.py index f66ce219a..effb547df 100644 --- a/tests/system_tests_autolinks.py +++ b/tests/system_tests_autolinks.py @@ -205,8 +205,8 @@ def setUpClass(cls): 'address': 'examples', 'direction': 'out'}), ]) - def __init__(self, test_method): - TestCase.__init__(self, test_method) + def setUp(self): + super().setUp() self.success = False self.timer_delay = 6 self.max_attempts = 2 diff --git a/tests/system_tests_bad_configuration.py b/tests/system_tests_bad_configuration.py index b80fc2032..5bccf4037 100644 --- a/tests/system_tests_bad_configuration.py +++ b/tests/system_tests_bad_configuration.py @@ -37,11 +37,8 @@ class RouterTestBadConfiguration(TestCase): process. """ @classmethod - def setUpClass(cls): - """ - Set up router instance configuration to be used for testing. - :return: - """ + def setUpClass(cls) -> None: + """Set up router instance configuration to be used for testing.""" super(RouterTestBadConfiguration, cls).setUpClass() cls.name = "test-router" cls.unresolvable_host_name = 'unresolvable.host.name' @@ -61,19 +58,23 @@ def setUpClass(cls): except OSError: pass - def __init__(self, test_method): - TestCase.__init__(self, test_method) + def setUp(self): + super().setUp() self.error_caught = False self.timer_delay = 0.2 self.max_attempts = 100 self.attempts_made = 0 + self.schedule_timer() - def schedule_timer(self): + # Wait till error is found or timed out waiting for it. + while self.waiting_for_error(): + pass + + def schedule_timer(self) -> None: """ Schedules a timer triggers wait_for_unresolvable_host after timer_delay has been elapsed. - :return: """ Timer(self.timer_delay, self.wait_for_unresolvable_host).start() @@ -85,23 +86,20 @@ def address(self): """ Returns the address that can be used along with qdmanage to query the running instance of the dispatch router. - :return: """ return self.router.addresses[0] - def waiting_for_error(self): + def waiting_for_error(self) -> bool: """ Returns True if max_attempts not yet reached and error is still not found. - :return: bool """ return not self.error_caught and self.attempts_made < self.max_attempts - def wait_for_unresolvable_host(self): + def wait_for_unresolvable_host(self) -> None: """ Wait for error to show up in the logs based on pre-defined max_attempts and timer_delay. If error is not caught within max_attempts * timer_delay then it stops scheduling new attempts. - :return: """ try: # mode 'r' and 't' are defaults @@ -125,22 +123,11 @@ def wait_for_unresolvable_host(self): self.attempts_made += 1 self.schedule_timer() - def setUp(self): - """ - Causes tests to wait till timer has found the expected error or - after it times out. - :return: - """ - # Wait till error is found or timed out waiting for it. - while self.waiting_for_error(): - pass - def test_unresolvable_host_caught(self): """ Validate if the error message stating host is unresolvable is printed to the router log. It expects that the error can be caught in the logs. - :return: """ self.assertTrue(self.error_caught) @@ -172,9 +159,6 @@ def setUpClass(cls): super(RouterTestIdFailCtrlChar, cls).setUpClass() cls.name = "test-router-ctrl-char" - def __init__(self, test_method): - TestCase.__init__(self, test_method) - @classmethod def tearDownClass(cls): super(RouterTestIdFailCtrlChar, cls).tearDownClass() @@ -214,9 +198,6 @@ def setUpClass(cls): super(RouterTestIdFailWhiteSpace, cls).setUpClass() cls.name = "test-router-ctrl-char" - def __init__(self, test_method): - TestCase.__init__(self, test_method) - @classmethod def tearDownClass(cls): super(RouterTestIdFailWhiteSpace, cls).tearDownClass() diff --git a/tests/system_tests_connector_status.py b/tests/system_tests_connector_status.py index 7c86de001..c0e983d67 100644 --- a/tests/system_tests_connector_status.py +++ b/tests/system_tests_connector_status.py @@ -62,8 +62,8 @@ def router(name, config): cls.routers[0].wait_ports() cls.routers[1].wait_ports() - def __init__(self, test_method): - TestCase.__init__(self, test_method) + def setUp(self): + super().setUp() self.success = False self.timer_delay = 2 self.max_attempts = 5 diff --git a/tests/system_tests_distribution.py b/tests/system_tests_distribution.py index 3915f6486..de6e39f09 100644 --- a/tests/system_tests_distribution.py +++ b/tests/system_tests_distribution.py @@ -118,7 +118,7 @@ class DistributionSkipMapper: # Setup # ================================================================ -class DistributionTests (TestCase): +class DistributionTests(TestCase): @classmethod def setUpClass(cls): diff --git a/tests/system_tests_edge_router.py b/tests/system_tests_edge_router.py index a50e79eff..ca31e4a12 100644 --- a/tests/system_tests_edge_router.py +++ b/tests/system_tests_edge_router.py @@ -97,8 +97,8 @@ def router(name, mode, connection, extra=None): cls.skip = {'test_01' : 0 } - def __init__(self, test_method): - TestCase.__init__(self, test_method) + def setUp(self): + super().setUp() self.success = False self.timer_delay = 2 self.max_attempts = 3 diff --git a/tests/system_tests_handle_failover.py b/tests/system_tests_handle_failover.py index 144933fd8..cacdcda8c 100644 --- a/tests/system_tests_handle_failover.py +++ b/tests/system_tests_handle_failover.py @@ -90,8 +90,8 @@ def setUpClass(cls): cls.routers[1].wait_router_connected('B') - def __init__(self, test_method): - TestCase.__init__(self, test_method) + def setUp(self): + super().setUp() self.success = False self.timer_delay = 2 self.max_attempts = 10 diff --git a/tests/system_tests_two_routers.py b/tests/system_tests_two_routers.py index da508a3b3..018c3af2e 100644 --- a/tests/system_tests_two_routers.py +++ b/tests/system_tests_two_routers.py @@ -1774,8 +1774,8 @@ def run(self): class TwoRouterConnection(TestCase): - def __init__(self, test_method): - TestCase.__init__(self, test_method) + def setUp(self): + super().setUp() self.success = False self.timer_delay = 4 self.max_attempts = 2 diff --git a/tests/test_command.py b/tests/test_command.py index 8b071d990..ef926ade2 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -62,6 +62,7 @@ def show_all(self): pass class TestParseArgsQdstat(unittest.TestCase): def setUp(self): + super().setUp() self.parser = _qdstat_parser(BusManager=FBM) def test_parse_args_qdstat_print_help(self): @@ -111,6 +112,7 @@ def test_parse_args_qdstat_limit(self): class TestParseArgsQdmanage(unittest.TestCase): def setUp(self): + super().setUp() self.operations = ["HERE", "SOME", "OPERATIONS"] self.parser = _qdmanage_parser(operations=self.operations)