Skip to content

Commit abf336e

Browse files
Removed dependency on unittest.TestCase base class
1 parent 46c6a70 commit abf336e

17 files changed

+60
-79
lines changed

tests/async/test_admin.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import threading
33
import time
44
from unittest import mock
5-
import unittest
65
import pytest
76
try:
87
from engineio.async_socket import AsyncSocket as EngineIOSocket
@@ -38,13 +37,13 @@ def connect(sid, environ, auth):
3837
pass
3938

4039
async def shutdown():
41-
await instrumented_server.shutdown()
40+
await self.isvr.shutdown()
4241
await sio.shutdown()
4342

4443
if 'server_stats_interval' not in ikwargs:
4544
ikwargs['server_stats_interval'] = 0.25
4645

47-
instrumented_server = sio.instrument(auth=auth, **ikwargs)
46+
self.isvr = sio.instrument(auth=auth, **ikwargs)
4847
server = SocketIOWebServer(sio, on_shutdown=shutdown)
4948
server.start()
5049

@@ -56,10 +55,11 @@ async def shutdown():
5655
EngineIOSocket.schedule_ping = mock.MagicMock()
5756

5857
try:
59-
ret = f(self, instrumented_server, *args, **kwargs)
58+
ret = f(self, *args, **kwargs)
6059
finally:
6160
server.stop()
62-
instrumented_server.uninstrument()
61+
self.isvr.uninstrument()
62+
self.isvr = None
6363

6464
EngineIOSocket.schedule_ping = original_schedule_ping
6565

@@ -80,12 +80,12 @@ async def _async_custom_auth(auth):
8080
return auth == {'foo': 'bar'}
8181

8282

83-
class TestAsyncAdmin(unittest.TestCase):
84-
def setUp(self):
83+
class TestAsyncAdmin:
84+
def setup_method(self):
8585
print('threads at start:', threading.enumerate())
8686
self.thread_count = threading.active_count()
8787

88-
def tearDown(self):
88+
def teardown_method(self):
8989
print('threads at end:', threading.enumerate())
9090
assert self.thread_count == threading.active_count()
9191

@@ -107,15 +107,15 @@ def test_missing_auth(self):
107107
sio.instrument()
108108

109109
@with_instrumented_server(auth=False)
110-
def test_admin_connect_with_no_auth(self, isvr):
110+
def test_admin_connect_with_no_auth(self):
111111
with socketio.SimpleClient() as admin_client:
112112
admin_client.connect('http://localhost:8900', namespace='/admin')
113113
with socketio.SimpleClient() as admin_client:
114114
admin_client.connect('http://localhost:8900', namespace='/admin',
115115
auth={'foo': 'bar'})
116116

117117
@with_instrumented_server(auth={'foo': 'bar'})
118-
def test_admin_connect_with_dict_auth(self, isvr):
118+
def test_admin_connect_with_dict_auth(self):
119119
with socketio.SimpleClient() as admin_client:
120120
admin_client.connect('http://localhost:8900', namespace='/admin',
121121
auth={'foo': 'bar'})
@@ -131,7 +131,7 @@ def test_admin_connect_with_dict_auth(self, isvr):
131131

132132
@with_instrumented_server(auth=[{'foo': 'bar'},
133133
{'u': 'admin', 'p': 'secret'}])
134-
def test_admin_connect_with_list_auth(self, isvr):
134+
def test_admin_connect_with_list_auth(self):
135135
with socketio.SimpleClient() as admin_client:
136136
admin_client.connect('http://localhost:8900', namespace='/admin',
137137
auth={'foo': 'bar'})
@@ -148,7 +148,7 @@ def test_admin_connect_with_list_auth(self, isvr):
148148
namespace='/admin')
149149

150150
@with_instrumented_server(auth=_custom_auth)
151-
def test_admin_connect_with_function_auth(self, isvr):
151+
def test_admin_connect_with_function_auth(self):
152152
with socketio.SimpleClient() as admin_client:
153153
admin_client.connect('http://localhost:8900', namespace='/admin',
154154
auth={'foo': 'bar'})
@@ -162,7 +162,7 @@ def test_admin_connect_with_function_auth(self, isvr):
162162
namespace='/admin')
163163

164164
@with_instrumented_server(auth=_async_custom_auth)
165-
def test_admin_connect_with_async_function_auth(self, isvr):
165+
def test_admin_connect_with_async_function_auth(self):
166166
with socketio.SimpleClient() as admin_client:
167167
admin_client.connect('http://localhost:8900', namespace='/admin',
168168
auth={'foo': 'bar'})
@@ -176,7 +176,7 @@ def test_admin_connect_with_async_function_auth(self, isvr):
176176
namespace='/admin')
177177

178178
@with_instrumented_server()
179-
def test_admin_connect_only_admin(self, isvr):
179+
def test_admin_connect_only_admin(self):
180180
with socketio.SimpleClient() as admin_client:
181181
admin_client.connect('http://localhost:8900', namespace='/admin')
182182
sid = admin_client.sid
@@ -201,7 +201,7 @@ def test_admin_connect_only_admin(self, isvr):
201201
events['server_stats']['namespaces']
202202

203203
@with_instrumented_server()
204-
def test_admin_connect_with_others(self, isvr):
204+
def test_admin_connect_with_others(self):
205205
with socketio.SimpleClient() as client1, \
206206
socketio.SimpleClient() as client2, \
207207
socketio.SimpleClient() as client3, \
@@ -210,12 +210,12 @@ def test_admin_connect_with_others(self, isvr):
210210
client1.emit('enter_room', 'room')
211211
sid1 = client1.sid
212212

213-
saved_check_for_upgrade = isvr._check_for_upgrade
214-
isvr._check_for_upgrade = AsyncMock()
213+
saved_check_for_upgrade = self.isvr._check_for_upgrade
214+
self.isvr._check_for_upgrade = AsyncMock()
215215
client2.connect('http://localhost:8900', namespace='/foo',
216216
transports=['polling'])
217217
sid2 = client2.sid
218-
isvr._check_for_upgrade = saved_check_for_upgrade
218+
self.isvr._check_for_upgrade = saved_check_for_upgrade
219219

220220
client3.connect('http://localhost:8900', namespace='/admin')
221221
sid3 = client3.sid
@@ -251,7 +251,7 @@ def test_admin_connect_with_others(self, isvr):
251251
assert socket['rooms'] == [sid3]
252252

253253
@with_instrumented_server(mode='production', read_only=True)
254-
def test_admin_connect_production(self, isvr):
254+
def test_admin_connect_production(self):
255255
with socketio.SimpleClient() as admin_client:
256256
admin_client.connect('http://localhost:8900', namespace='/admin')
257257
events = self._expect({'config': 1, 'server_stats': 2},
@@ -272,7 +272,7 @@ def test_admin_connect_production(self, isvr):
272272
events['server_stats']['namespaces']
273273

274274
@with_instrumented_server()
275-
def test_admin_features(self, isvr):
275+
def test_admin_features(self):
276276
with socketio.SimpleClient() as client1, \
277277
socketio.SimpleClient() as client2, \
278278
socketio.SimpleClient() as admin_client:

tests/async/test_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import unittest
32
from unittest import mock
43

54
import pytest
@@ -12,7 +11,7 @@
1211
from .helpers import AsyncMock, _run
1312

1413

15-
class TestAsyncClient(unittest.TestCase):
14+
class TestAsyncClient:
1615
def test_is_asyncio_based(self):
1716
c = async_client.AsyncClient()
1817
assert c.is_asyncio_based()

tests/async/test_manager.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import unittest
21
from unittest import mock
32

43
from socketio import async_manager
54
from socketio import packet
65
from .helpers import AsyncMock, _run
76

87

9-
class TestAsyncManager(unittest.TestCase):
10-
def setUp(self):
8+
class TestAsyncManager:
9+
def setup_method(self):
1110
id = 0
1211

1312
def generate_id():

tests/async/test_namespace.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import sys
2-
import unittest
31
from unittest import mock
42

53
from socketio import async_namespace
64
from .helpers import AsyncMock, _run
75

86

9-
@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')
10-
class TestAsyncNamespace(unittest.TestCase):
7+
class TestAsyncNamespace:
118
def test_connect_event(self):
129
result = {}
1310

tests/async/test_pubsub_manager.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import functools
3-
import unittest
43
from unittest import mock
54

65
import pytest
@@ -11,8 +10,8 @@
1110
from .helpers import AsyncMock, _run
1211

1312

14-
class TestAsyncPubSubManager(unittest.TestCase):
15-
def setUp(self):
13+
class TestAsyncPubSubManager:
14+
def setup_method(self):
1615
id = 0
1716

1817
def generate_id():

tests/async/test_server.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import logging
3-
import unittest
43
from unittest import mock
54

65
from engineio import json
@@ -18,8 +17,8 @@
1817
@mock.patch('socketio.server.engineio.AsyncServer', **{
1918
'return_value.generate_id.side_effect': [str(i) for i in range(1, 10)],
2019
'return_value.send_packet': AsyncMock()})
21-
class TestAsyncServer(unittest.TestCase):
22-
def tearDown(self):
20+
class TestAsyncServer:
21+
def teardown_method(self):
2322
# restore JSON encoder, in case a test changed it
2423
packet.Packet.json = json
2524

tests/async/test_simple_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import unittest
32
from unittest import mock
43
import pytest
54

@@ -8,7 +7,7 @@
87
from .helpers import AsyncMock, _run
98

109

11-
class TestAsyncAsyncSimpleClient(unittest.TestCase):
10+
class TestAsyncAsyncSimpleClient:
1211
def test_constructor(self):
1312
client = AsyncSimpleClient(1, '2', a='3', b=4)
1413
assert client.client_args == (1, '2')

tests/common/test_admin.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import threading
33
import time
44
from unittest import mock
5-
import unittest
65
import pytest
76
from engineio.socket import Socket as EngineIOSocket
87
import socketio
@@ -36,7 +35,7 @@ def connect(sid, environ, auth):
3635
if 'server_stats_interval' not in ikwargs:
3736
ikwargs['server_stats_interval'] = 0.25
3837

39-
instrumented_server = sio.instrument(auth=auth, **ikwargs)
38+
self.isvr = sio.instrument(auth=auth, **ikwargs)
4039
server = SocketIOWebServer(sio)
4140
server.start()
4241

@@ -48,11 +47,12 @@ def connect(sid, environ, auth):
4847
EngineIOSocket.schedule_ping = mock.MagicMock()
4948

5049
try:
51-
ret = f(self, instrumented_server, *args, **kwargs)
50+
ret = f(self, *args, **kwargs)
5251
finally:
5352
server.stop()
54-
instrumented_server.shutdown()
55-
instrumented_server.uninstrument()
53+
self.isvr.shutdown()
54+
self.isvr.uninstrument()
55+
self.isvr = None
5656

5757
EngineIOSocket.schedule_ping = original_schedule_ping
5858

@@ -69,12 +69,12 @@ def _custom_auth(auth):
6969
return auth == {'foo': 'bar'}
7070

7171

72-
class TestAdmin(unittest.TestCase):
73-
def setUp(self):
72+
class TestAdmin:
73+
def setup_method(self):
7474
print('threads at start:', threading.enumerate())
7575
self.thread_count = threading.active_count()
7676

77-
def tearDown(self):
77+
def teardown_method(self):
7878
print('threads at end:', threading.enumerate())
7979
assert self.thread_count == threading.active_count()
8080

@@ -96,15 +96,15 @@ def test_missing_auth(self):
9696
sio.instrument()
9797

9898
@with_instrumented_server(auth=False)
99-
def test_admin_connect_with_no_auth(self, isvr):
99+
def test_admin_connect_with_no_auth(self):
100100
with socketio.SimpleClient() as admin_client:
101101
admin_client.connect('http://localhost:8900', namespace='/admin')
102102
with socketio.SimpleClient() as admin_client:
103103
admin_client.connect('http://localhost:8900', namespace='/admin',
104104
auth={'foo': 'bar'})
105105

106106
@with_instrumented_server(auth={'foo': 'bar'})
107-
def test_admin_connect_with_dict_auth(self, isvr):
107+
def test_admin_connect_with_dict_auth(self):
108108
with socketio.SimpleClient() as admin_client:
109109
admin_client.connect('http://localhost:8900', namespace='/admin',
110110
auth={'foo': 'bar'})
@@ -120,7 +120,7 @@ def test_admin_connect_with_dict_auth(self, isvr):
120120

121121
@with_instrumented_server(auth=[{'foo': 'bar'},
122122
{'u': 'admin', 'p': 'secret'}])
123-
def test_admin_connect_with_list_auth(self, isvr):
123+
def test_admin_connect_with_list_auth(self):
124124
with socketio.SimpleClient() as admin_client:
125125
admin_client.connect('http://localhost:8900', namespace='/admin',
126126
auth={'foo': 'bar'})
@@ -137,7 +137,7 @@ def test_admin_connect_with_list_auth(self, isvr):
137137
namespace='/admin')
138138

139139
@with_instrumented_server(auth=_custom_auth)
140-
def test_admin_connect_with_function_auth(self, isvr):
140+
def test_admin_connect_with_function_auth(self):
141141
with socketio.SimpleClient() as admin_client:
142142
admin_client.connect('http://localhost:8900', namespace='/admin',
143143
auth={'foo': 'bar'})
@@ -151,7 +151,7 @@ def test_admin_connect_with_function_auth(self, isvr):
151151
namespace='/admin')
152152

153153
@with_instrumented_server()
154-
def test_admin_connect_only_admin(self, isvr):
154+
def test_admin_connect_only_admin(self):
155155
with socketio.SimpleClient() as admin_client:
156156
admin_client.connect('http://localhost:8900', namespace='/admin')
157157
sid = admin_client.sid
@@ -176,7 +176,7 @@ def test_admin_connect_only_admin(self, isvr):
176176
events['server_stats']['namespaces']
177177

178178
@with_instrumented_server()
179-
def test_admin_connect_with_others(self, isvr):
179+
def test_admin_connect_with_others(self):
180180
with socketio.SimpleClient() as client1, \
181181
socketio.SimpleClient() as client2, \
182182
socketio.SimpleClient() as client3, \
@@ -185,12 +185,12 @@ def test_admin_connect_with_others(self, isvr):
185185
client1.emit('enter_room', 'room')
186186
sid1 = client1.sid
187187

188-
saved_check_for_upgrade = isvr._check_for_upgrade
189-
isvr._check_for_upgrade = mock.MagicMock()
188+
saved_check_for_upgrade = self.isvr._check_for_upgrade
189+
self.isvr._check_for_upgrade = mock.MagicMock()
190190
client2.connect('http://localhost:8900', namespace='/foo',
191191
transports=['polling'])
192192
sid2 = client2.sid
193-
isvr._check_for_upgrade = saved_check_for_upgrade
193+
self.isvr._check_for_upgrade = saved_check_for_upgrade
194194

195195
client3.connect('http://localhost:8900', namespace='/admin')
196196
sid3 = client3.sid
@@ -226,7 +226,7 @@ def test_admin_connect_with_others(self, isvr):
226226
assert socket['rooms'] == [sid3]
227227

228228
@with_instrumented_server(mode='production', read_only=True)
229-
def test_admin_connect_production(self, isvr):
229+
def test_admin_connect_production(self):
230230
with socketio.SimpleClient() as admin_client:
231231
admin_client.connect('http://localhost:8900', namespace='/admin')
232232
events = self._expect({'config': 1, 'server_stats': 2},
@@ -247,7 +247,7 @@ def test_admin_connect_production(self, isvr):
247247
events['server_stats']['namespaces']
248248

249249
@with_instrumented_server()
250-
def test_admin_features(self, isvr):
250+
def test_admin_features(self):
251251
with socketio.SimpleClient() as client1, \
252252
socketio.SimpleClient() as client2, \
253253
socketio.SimpleClient() as admin_client:

0 commit comments

Comments
 (0)