Skip to content

Commit 77430b9

Browse files
committed
refactor: get rid of unittest.TestCase
replace `setUp`/`tearDown` methods with pytest fixtures Replaced `asyncSetUp`/`asyncTearDown` in test cases with `@pytest.fixture` for improved pytest integration. Added `pytest-asyncio` as a dependency. Updated version bump to `2.1.3`.
1 parent 9005b4e commit 77430b9

28 files changed

+166
-113
lines changed

conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
# Configure pytest-asyncio
4+
pytest_plugins = ('pytest_asyncio',)
5+

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ crypto = ["pycryptodome"]
4242
vcdiff = ["vcdiff-decoder>=0.1.0,<0.2.0"]
4343
dev = [
4444
"pytest>=7.1,<8.0",
45+
"pytest-asyncio>=0.21.0,<0.23.0; python_version=='3.7'",
46+
"pytest-asyncio>=0.23.0,<1.0.0; python_version>='3.8'",
4547
"mock>=4.0.3,<5.0.0",
4648
"pytest-cov>=2.4,<3.0",
4749
"ruff>=0.14.0,<1.0.0",
@@ -91,6 +93,7 @@ packages = ["ably"]
9193

9294
[tool.pytest.ini_options]
9395
timeout = 30
96+
asyncio_mode = "auto"
9497

9598
[[tool.uv.index]]
9699
name = "experimental"

test/ably/conftest.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import asyncio
2-
3-
import pytest
1+
import pytest_asyncio
42

53
from test.ably.testapp import TestApp
64

75

8-
@pytest.fixture(scope='session', autouse=True)
9-
def event_loop():
10-
loop = asyncio.get_event_loop_policy().new_event_loop()
11-
loop.run_until_complete(TestApp.get_test_vars())
12-
yield loop
13-
loop.run_until_complete(TestApp.clear_test_vars())
6+
@pytest_asyncio.fixture(scope='session', autouse=True)
7+
async def test_app_setup():
8+
await TestApp.get_test_vars()
9+
yield
10+
await TestApp.clear_test_vars()

test/ably/realtime/eventemitter_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import asyncio
22

3+
import pytest
4+
35
from ably.realtime.connection import ConnectionState
46
from test.ably.testapp import TestApp
57
from test.ably.utils import BaseAsyncTestCase
68

79

810
class TestEventEmitter(BaseAsyncTestCase):
9-
async def asyncSetUp(self):
11+
@pytest.fixture(autouse=True)
12+
async def setup(self):
1013
self.test_vars = await TestApp.get_test_vars()
1114

1215
async def test_event_listener_error(self):

test/ably/realtime/realtimechannel_publish_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
class TestRealtimeChannelPublish(BaseAsyncTestCase):
1515
"""Tests for RTN7 spec - Message acknowledgment"""
1616

17-
async def asyncSetUp(self):
17+
@pytest.fixture(autouse=True)
18+
async def setup(self):
1819
self.test_vars = await TestApp.get_test_vars()
1920

2021
# RTN7a - Basic ACK/NACK functionality

test/ably/realtime/realtimechannel_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313

1414
class TestRealtimeChannel(BaseAsyncTestCase):
15-
async def asyncSetUp(self):
15+
@pytest.fixture(autouse=True)
16+
async def setup(self):
1617
self.test_vars = await TestApp.get_test_vars()
1718
self.valid_key_format = "api:key"
1819

test/ably/realtime/realtimechannel_vcdiff_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import json
33

4+
import pytest
5+
46
from ably import AblyVCDiffDecoder
57
from ably.realtime.connection import ConnectionState
68
from ably.realtime.realtime_channel import ChannelOptions
@@ -31,7 +33,8 @@ def decode(self, delta: bytes, base: bytes) -> bytes:
3133

3234

3335
class TestRealtimeChannelVCDiff(BaseAsyncTestCase):
34-
async def asyncSetUp(self):
36+
@pytest.fixture(autouse=True)
37+
async def setup(self):
3538
self.test_vars = await TestApp.get_test_vars()
3639
self.valid_key_format = "api:key"
3740

test/ably/realtime/realtimeconnection_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212

1313
class TestRealtimeConnection(BaseAsyncTestCase):
14-
async def asyncSetUp(self):
14+
@pytest.fixture(autouse=True)
15+
async def setup(self):
1516
self.test_vars = await TestApp.get_test_vars()
1617
self.valid_key_format = "api:key"
1718

test/ably/realtime/realtimeinit_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111

1212
class TestRealtimeInit(BaseAsyncTestCase):
13-
async def asyncSetUp(self):
13+
@pytest.fixture(autouse=True)
14+
async def setup(self):
1415
self.test_vars = await TestApp.get_test_vars()
1516
self.valid_key_format = "api:key"
1617

test/ably/realtime/realtimeresume_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
import pytest
4+
35
from ably.realtime.connection import ConnectionState
46
from ably.realtime.realtime_channel import ChannelState
57
from ably.transport.websockettransport import ProtocolMessageAction
@@ -22,7 +24,8 @@ def on_message(_):
2224

2325

2426
class TestRealtimeResume(BaseAsyncTestCase):
25-
async def asyncSetUp(self):
27+
@pytest.fixture(autouse=True)
28+
async def setup(self):
2629
self.test_vars = await TestApp.get_test_vars()
2730
self.valid_key_format = "api:key"
2831

0 commit comments

Comments
 (0)