Skip to content

Commit 97ea276

Browse files
authored
Fixed #49: Prefer use of unittest.TestCase#setUp instead of __init__ in tests (#49)
This started as a workaround for Pytest 7.0.0 stacktrace normalization bug pytest-dev/pytest#9610
1 parent 84f664b commit 97ea276

11 files changed

+44
-45
lines changed

tests/http1_tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ def get_request(self) -> Tuple[socket.socket, Any]:
148148
self._client_sockets.append(sock)
149149
return sock, addr
150150

151-
def shutdown_request(self, request: socket.socket) -> None:
151+
def shutdown_request(self, request) -> None:
152152
"""Forget the client socket"""
153+
assert isinstance(request, socket.socket)
154+
153155
self._client_sockets.remove(request)
154156
super().shutdown_request(request) # type: ignore[misc] # error: "shutdown_request" undefined in superclass
155157

tests/router_engine_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def neighbor_refresh(self, node_id, ProtocolVersion, instance, link_id, cost, no
146146
self.neighbors[node_id] = (instance, link_id, cost, now)
147147

148148
def setUp(self):
149+
super().setUp()
149150
self.sent = []
150151
self.neighbors = {}
151152
self.id = "R1"
@@ -210,6 +211,7 @@ def test_establish_multiple_peers(self):
210211

211212
class PathTest(unittest.TestCase):
212213
def setUp(self):
214+
super().setUp()
213215
self.id = 'R1'
214216
self.engine = PathEngine(self)
215217

tests/system_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,13 @@ class TestCase(unittest.TestCase, Tester): # pylint: disable=too-many-public-me
877877

878878
tester: Tester
879879

880-
def __init__(self, test_method):
881-
unittest.TestCase.__init__(self, test_method)
880+
def __init__(self, methodName='runTest'):
881+
unittest.TestCase.__init__(self, methodName)
882882
Tester.__init__(self, self.id())
883883

884884
@classmethod
885885
def setUpClass(cls):
886+
super().setUpClass()
886887
cls.maxDiff = None
887888
cls.tester = Tester('.'.join([cls.__module__, cls.__name__, 'setUpClass']))
888889
cls.tester.rmtree()
@@ -893,12 +894,15 @@ def tearDownClass(cls):
893894
if hasattr(cls, 'tester'):
894895
cls.tester.teardown()
895896
del cls.tester
897+
super().tearDownClass()
896898

897899
def setUp(self):
900+
super().setUp()
898901
Tester.setup(self)
899902

900903
def tearDown(self):
901904
Tester.teardown(self)
905+
super().tearDown()
902906

903907
def assert_fair(self, seq):
904908
avg = sum(seq) / len(seq)

tests/system_tests_autolinks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ def setUpClass(cls):
205205
'address': 'examples', 'direction': 'out'}),
206206
])
207207

208-
def __init__(self, test_method):
209-
TestCase.__init__(self, test_method)
208+
def setUp(self):
209+
super().setUp()
210210
self.success = False
211211
self.timer_delay = 6
212212
self.max_attempts = 2

tests/system_tests_bad_configuration.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import os
2727
from threading import Timer
2828
from subprocess import PIPE, STDOUT
29+
from typing import ClassVar
30+
2931
from system_test import TestCase, Qdrouterd, TIMEOUT, Process
3032

3133

@@ -36,12 +38,15 @@ class RouterTestBadConfiguration(TestCase):
3638
well defined, but are not supposed to cause a crash to the router
3739
process.
3840
"""
41+
42+
config: ClassVar[Qdrouterd.Config]
43+
name: ClassVar[str]
44+
unresolvable_host_name: ClassVar[str]
45+
router: ClassVar[Qdrouterd]
46+
3947
@classmethod
40-
def setUpClass(cls):
41-
"""
42-
Set up router instance configuration to be used for testing.
43-
:return:
44-
"""
48+
def setUpClass(cls) -> None:
49+
"""Set up router instance configuration to be used for testing."""
4550
super(RouterTestBadConfiguration, cls).setUpClass()
4651
cls.name = "test-router"
4752
cls.unresolvable_host_name = 'unresolvable.host.name'
@@ -61,19 +66,23 @@ def setUpClass(cls):
6166
except OSError:
6267
pass
6368

64-
def __init__(self, test_method):
65-
TestCase.__init__(self, test_method)
69+
def setUp(self):
70+
super().setUp()
6671
self.error_caught = False
6772
self.timer_delay = 0.2
6873
self.max_attempts = 100
6974
self.attempts_made = 0
75+
7076
self.schedule_timer()
7177

72-
def schedule_timer(self):
78+
# Wait till error is found or timed out waiting for it.
79+
while self.waiting_for_error():
80+
pass
81+
82+
def schedule_timer(self) -> None:
7383
"""
7484
Schedules a timer triggers wait_for_unresolvable_host after
7585
timer_delay has been elapsed.
76-
:return:
7786
"""
7887
Timer(self.timer_delay, self.wait_for_unresolvable_host).start()
7988

@@ -85,23 +94,20 @@ def address(self):
8594
"""
8695
Returns the address that can be used along with qdmanage
8796
to query the running instance of the dispatch router.
88-
:return:
8997
"""
9098
return self.router.addresses[0]
9199

92-
def waiting_for_error(self):
100+
def waiting_for_error(self) -> bool:
93101
"""
94102
Returns True if max_attempts not yet reached and error is still not found.
95-
:return: bool
96103
"""
97104
return not self.error_caught and self.attempts_made < self.max_attempts
98105

99-
def wait_for_unresolvable_host(self):
106+
def wait_for_unresolvable_host(self) -> None:
100107
"""
101108
Wait for error to show up in the logs based on pre-defined max_attempts
102109
and timer_delay. If error is not caught within max_attempts * timer_delay
103110
then it stops scheduling new attempts.
104-
:return:
105111
"""
106112
try:
107113
# mode 'r' and 't' are defaults
@@ -125,22 +131,11 @@ def wait_for_unresolvable_host(self):
125131
self.attempts_made += 1
126132
self.schedule_timer()
127133

128-
def setUp(self):
129-
"""
130-
Causes tests to wait till timer has found the expected error or
131-
after it times out.
132-
:return:
133-
"""
134-
# Wait till error is found or timed out waiting for it.
135-
while self.waiting_for_error():
136-
pass
137-
138134
def test_unresolvable_host_caught(self):
139135
"""
140136
Validate if the error message stating host is unresolvable is printed
141137
to the router log.
142138
It expects that the error can be caught in the logs.
143-
:return:
144139
"""
145140
self.assertTrue(self.error_caught)
146141

@@ -172,9 +167,6 @@ def setUpClass(cls):
172167
super(RouterTestIdFailCtrlChar, cls).setUpClass()
173168
cls.name = "test-router-ctrl-char"
174169

175-
def __init__(self, test_method):
176-
TestCase.__init__(self, test_method)
177-
178170
@classmethod
179171
def tearDownClass(cls):
180172
super(RouterTestIdFailCtrlChar, cls).tearDownClass()
@@ -214,9 +206,6 @@ def setUpClass(cls):
214206
super(RouterTestIdFailWhiteSpace, cls).setUpClass()
215207
cls.name = "test-router-ctrl-char"
216208

217-
def __init__(self, test_method):
218-
TestCase.__init__(self, test_method)
219-
220209
@classmethod
221210
def tearDownClass(cls):
222211
super(RouterTestIdFailWhiteSpace, cls).tearDownClass()

tests/system_tests_connector_status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def router(name, config):
6262
cls.routers[0].wait_ports()
6363
cls.routers[1].wait_ports()
6464

65-
def __init__(self, test_method):
66-
TestCase.__init__(self, test_method)
65+
def setUp(self):
66+
super().setUp()
6767
self.success = False
6868
self.timer_delay = 2
6969
self.max_attempts = 5

tests/system_tests_distribution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class DistributionSkipMapper:
118118
# Setup
119119
# ================================================================
120120

121-
class DistributionTests (TestCase):
121+
class DistributionTests(TestCase):
122122

123123
@classmethod
124124
def setUpClass(cls):

tests/system_tests_edge_router.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ def router(name, mode, connection, extra=None):
9797
cls.skip = {'test_01' : 0
9898
}
9999

100-
def __init__(self, test_method):
101-
TestCase.__init__(self, test_method)
100+
def setUp(self):
101+
super().setUp()
102102
self.success = False
103103
self.timer_delay = 2
104104
self.max_attempts = 3

tests/system_tests_handle_failover.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def setUpClass(cls):
9090

9191
cls.routers[1].wait_router_connected('B')
9292

93-
def __init__(self, test_method):
94-
TestCase.__init__(self, test_method)
93+
def setUp(self):
94+
super().setUp()
9595
self.success = False
9696
self.timer_delay = 2
9797
self.max_attempts = 10

tests/system_tests_two_routers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,8 +1774,8 @@ def run(self):
17741774

17751775

17761776
class TwoRouterConnection(TestCase):
1777-
def __init__(self, test_method):
1778-
TestCase.__init__(self, test_method)
1777+
def setUp(self):
1778+
super().setUp()
17791779
self.success = False
17801780
self.timer_delay = 4
17811781
self.max_attempts = 2

0 commit comments

Comments
 (0)