Skip to content

Commit 01d9960

Browse files
committed
test: refactor test suite to use pytest
- Replace unittest with pytest for test execution - Use pytest fixtures for common test setup and teardown - Simplify test structure and remove unnecessary classes - Update test assertions to match pytest conventions - Add pytest decorators for skipping tests based on environment
1 parent 683bed4 commit 01d9960

File tree

8 files changed

+518
-332
lines changed

8 files changed

+518
-332
lines changed

tests/test_3rd_serializers.py

Lines changed: 199 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,232 @@
11
from datetime import datetime
22
from random import choice, randint, random
3-
from unittest import TestCase
43
from uuid import uuid4
54

5+
import pytest
6+
67
from ._catches import CACHES, MAXSIZE
78

89

910
def echo(x):
1011
return x
1112

1213

13-
class SerializerByStringParameterTest(TestCase):
14-
def setUp(self):
15-
for cache in CACHES.values():
16-
cache.policy.purge()
14+
@pytest.fixture(autouse=True)
15+
def clean_caches():
16+
"""自动清理缓存的夹具,在每个测试前后运行。"""
17+
# 测试前清理
18+
for cache in CACHES.values():
19+
cache.policy.purge()
20+
yield
21+
# 测试后清理
22+
for cache in CACHES.values():
23+
cache.policy.purge()
24+
25+
26+
def test_bson():
27+
def _test_bytes(f):
28+
for _ in range(randint(1, MAXSIZE * 2)):
29+
v0 = uuid4().bytes
30+
v1 = f(v0)
31+
assert v0 == v1
32+
33+
def _test_str(f):
34+
for _ in range(randint(1, MAXSIZE * 2)):
35+
v0 = uuid4().hex
36+
v1 = f(v0)
37+
assert v0 == v1
38+
39+
def _test_int(f):
40+
for _ in range(randint(1, MAXSIZE * 2)):
41+
v0 = randint(-(2**63), 2**63 - 1)
42+
v1 = f(v0)
43+
assert v0 == v1
44+
45+
def _test_float(f):
46+
for _ in range(randint(1, MAXSIZE * 2)):
47+
v0 = random()
48+
v1 = f(v0)
49+
assert v0 == v1
50+
51+
def _test_bool(f):
52+
for _ in range(randint(1, MAXSIZE * 2)):
53+
v0 = choice((True, False))
54+
v1 = f(v0)
55+
assert v0 == v1
56+
57+
def _test_none(f):
58+
for _ in range(randint(1, MAXSIZE * 2)):
59+
v0 = None
60+
v1 = f(v0)
61+
assert v0 == v1
62+
63+
def _test_datetime(f):
64+
for _ in range(randint(1, MAXSIZE * 2)):
65+
v0 = datetime.now().replace(microsecond=0)
66+
v1 = f(v0)
67+
assert v0 == v1
1768

18-
def _test_bytes(self, f):
69+
for cache in CACHES.values():
70+
fn = cache(echo, serializer="bson")
71+
_test_bytes(fn)
72+
_test_none(fn)
73+
_test_bool(fn)
74+
_test_str(fn)
75+
_test_int(fn)
76+
_test_float(fn)
77+
_test_datetime(fn)
78+
79+
80+
def test_msgpack():
81+
def _test_bytes(f):
1982
for _ in range(randint(1, MAXSIZE * 2)):
2083
v0 = uuid4().bytes
2184
v1 = f(v0)
22-
self.assertEqual(v0, v1)
85+
assert v0 == v1
2386

24-
def _test_str(self, f):
87+
def _test_str(f):
2588
for _ in range(randint(1, MAXSIZE * 2)):
2689
v0 = uuid4().hex
2790
v1 = f(v0)
28-
self.assertEqual(v0, v1)
91+
assert v0 == v1
2992

30-
def _test_int(self, f):
93+
def _test_int(f):
3194
for _ in range(randint(1, MAXSIZE * 2)):
32-
v0 = randint(-100 * MAXSIZE, MAXSIZE * 100)
95+
v0 = randint(-(2**63), 2**63 - 1)
3396
v1 = f(v0)
34-
self.assertEqual(v0, v1)
97+
assert v0 == v1
3598

36-
def _test_float(self, f):
99+
def _test_float(f):
37100
for _ in range(randint(1, MAXSIZE * 2)):
38101
v0 = random()
39102
v1 = f(v0)
40-
self.assertEqual(v0, v1)
103+
assert v0 == v1
41104

42-
def _test_bool(self, f):
105+
def _test_bool(f):
43106
for _ in range(randint(1, MAXSIZE * 2)):
44-
v0 = choice([False, True])
107+
v0 = choice((True, False))
45108
v1 = f(v0)
46-
self.assertEqual(v0, v1)
109+
assert v0 == v1
47110

48-
def _test_none(self, f):
111+
def _test_none(f):
49112
for _ in range(randint(1, MAXSIZE * 2)):
50113
v0 = None
51114
v1 = f(v0)
52-
self.assertEqual(v0, v1)
53-
54-
def _test_datetime(self, f):
55-
for _ in range(randint(1, MAXSIZE * 2)):
56-
v0 = datetime(
57-
randint(1900, 2100),
58-
randint(1, 12),
59-
randint(1, 28),
60-
randint(0, 23),
61-
randint(0, 59),
62-
randint(0, 59),
63-
randint(0, 999999),
64-
tzinfo=None,
65-
)
66-
v1 = f(v0)
67-
self.assertEqual(v0, v1)
68-
69-
def test_bson(self):
70-
for cache in CACHES.values():
71-
fn = cache(echo, serializer="bson")
72-
self._test_bytes(fn)
73-
self._test_none(fn)
74-
self._test_bool(fn)
75-
self._test_str(fn)
76-
self._test_int(fn)
77-
self._test_float(fn)
78-
self._test_datetime(fn)
79-
80-
def test_msgpack(self):
81-
for cache in CACHES.values():
82-
fn = cache(echo, serializer="msgpack")
83-
self._test_bytes(fn)
84-
self._test_none(fn)
85-
self._test_bool(fn)
86-
self._test_str(fn)
87-
self._test_int(fn)
88-
self._test_float(fn)
89-
90-
def test_yaml(self):
91-
for cache in CACHES.values():
92-
fn = cache(echo, serializer="yaml")
93-
self._test_bytes(fn)
94-
self._test_none(fn)
95-
self._test_bool(fn)
96-
self._test_str(fn)
97-
self._test_int(fn)
98-
self._test_float(fn)
99-
self._test_datetime(fn)
100-
101-
def test_cloudpickle(self):
102-
for cache in CACHES.values():
103-
fn = cache(echo, serializer="cloudpickle")
104-
self._test_bytes(fn)
105-
self._test_none(fn)
106-
self._test_bool(fn)
107-
self._test_str(fn)
108-
self._test_int(fn)
109-
self._test_float(fn)
110-
self._test_datetime(fn)
115+
assert v0 == v1
116+
117+
for cache in CACHES.values():
118+
fn = cache(echo, serializer="msgpack")
119+
_test_bytes(fn)
120+
_test_none(fn)
121+
_test_bool(fn)
122+
_test_str(fn)
123+
_test_int(fn)
124+
_test_float(fn)
125+
126+
127+
def test_yaml():
128+
def _test_bytes(f):
129+
for _ in range(randint(1, MAXSIZE * 2)):
130+
v0 = uuid4().bytes
131+
v1 = f(v0)
132+
assert v0 == v1
133+
134+
def _test_str(f):
135+
for _ in range(randint(1, MAXSIZE * 2)):
136+
v0 = uuid4().hex
137+
v1 = f(v0)
138+
assert v0 == v1
139+
140+
def _test_int(f):
141+
for _ in range(randint(1, MAXSIZE * 2)):
142+
v0 = randint(-(2**63), 2**63 - 1)
143+
v1 = f(v0)
144+
assert v0 == v1
145+
146+
def _test_float(f):
147+
for _ in range(randint(1, MAXSIZE * 2)):
148+
v0 = random()
149+
v1 = f(v0)
150+
assert v0 == v1
151+
152+
def _test_bool(f):
153+
for _ in range(randint(1, MAXSIZE * 2)):
154+
v0 = choice((True, False))
155+
v1 = f(v0)
156+
assert v0 == v1
157+
158+
def _test_none(f):
159+
for _ in range(randint(1, MAXSIZE * 2)):
160+
v0 = None
161+
v1 = f(v0)
162+
assert v0 == v1
163+
164+
def _test_datetime(f):
165+
for _ in range(randint(1, MAXSIZE * 2)):
166+
v0 = datetime.now().replace(microsecond=0)
167+
v1 = f(v0)
168+
assert v0 == v1
169+
170+
for cache in CACHES.values():
171+
fn = cache(echo, serializer="yaml")
172+
_test_bytes(fn)
173+
_test_none(fn)
174+
_test_bool(fn)
175+
_test_str(fn)
176+
_test_int(fn)
177+
_test_float(fn)
178+
_test_datetime(fn)
179+
180+
181+
def test_cloudpickle():
182+
def _test_bytes(f):
183+
for _ in range(randint(1, MAXSIZE * 2)):
184+
v0 = uuid4().bytes
185+
v1 = f(v0)
186+
assert v0 == v1
187+
188+
def _test_str(f):
189+
for _ in range(randint(1, MAXSIZE * 2)):
190+
v0 = uuid4().hex
191+
v1 = f(v0)
192+
assert v0 == v1
193+
194+
def _test_int(f):
195+
for _ in range(randint(1, MAXSIZE * 2)):
196+
v0 = randint(-(2**63), 2**63 - 1)
197+
v1 = f(v0)
198+
assert v0 == v1
199+
200+
def _test_float(f):
201+
for _ in range(randint(1, MAXSIZE * 2)):
202+
v0 = random()
203+
v1 = f(v0)
204+
assert v0 == v1
205+
206+
def _test_bool(f):
207+
for _ in range(randint(1, MAXSIZE * 2)):
208+
v0 = choice((True, False))
209+
v1 = f(v0)
210+
assert v0 == v1
211+
212+
def _test_none(f):
213+
for _ in range(randint(1, MAXSIZE * 2)):
214+
v0 = None
215+
v1 = f(v0)
216+
assert v0 == v1
217+
218+
def _test_datetime(f):
219+
for _ in range(randint(1, MAXSIZE * 2)):
220+
v0 = datetime.now().replace(microsecond=0)
221+
v1 = f(v0)
222+
assert v0 == v1
223+
224+
for cache in CACHES.values():
225+
fn = cache(echo, serializer="cloudpickle")
226+
_test_bytes(fn)
227+
_test_none(fn)
228+
_test_bool(fn)
229+
_test_str(fn)
230+
_test_int(fn)
231+
_test_float(fn)
232+
_test_datetime(fn)

tests/test_cluster.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
11
from random import randint
2-
from unittest import TestCase, skipUnless
2+
3+
import pytest
34

45
from ._catches import CLUSTER_CACHES, MAXSIZE, REDIS_CLUSTER_NODES
56

67

7-
@skipUnless(REDIS_CLUSTER_NODES, "REDIS_CLUSTER_NODES environment variable is not set")
8-
class ClusterTest(TestCase):
9-
def setUp(self):
10-
for cache in CLUSTER_CACHES.values():
11-
cache.policy.purge()
8+
@pytest.fixture(autouse=True)
9+
def clean_caches():
10+
"""自动清理缓存的夹具,在每个测试前后运行。"""
11+
# 测试前清理
12+
for cache in CLUSTER_CACHES.values():
13+
cache.policy.purge()
14+
yield
15+
# 测试后清理
16+
for cache in CLUSTER_CACHES.values():
17+
cache.policy.purge()
18+
19+
20+
@pytest.mark.skipif(not REDIS_CLUSTER_NODES, reason="REDIS_CLUSTER_NODES environment variable is not set")
21+
def test_cluster_int():
22+
for cache in CLUSTER_CACHES.values():
1223

13-
def test_int(self):
14-
for cache in CLUSTER_CACHES.values():
24+
@cache
25+
def echo(x):
26+
return x
1527

16-
@cache
17-
def echo(x):
18-
return x
28+
for i in range(randint(1, MAXSIZE * 2)):
29+
assert i == echo(i)
30+
assert i == echo(i)
1931

20-
for i in range(randint(1, MAXSIZE * 2)):
21-
self.assertEqual(i, echo(i))
22-
self.assertEqual(i, echo(i))
2332

24-
def test_two_functions(self):
25-
for cache in CLUSTER_CACHES.values():
33+
@pytest.mark.skipif(not REDIS_CLUSTER_NODES, reason="REDIS_CLUSTER_NODES environment variable is not set")
34+
def test_cluster_two_functions():
35+
for cache in CLUSTER_CACHES.values():
2636

27-
@cache
28-
def echo1(x):
29-
return x
37+
@cache
38+
def echo1(x):
39+
return x
3040

31-
@cache
32-
def echo2(x):
33-
return x
41+
@cache
42+
def echo2(x):
43+
return x
3444

35-
for i in range(randint(MAXSIZE, MAXSIZE * 2)):
36-
self.assertEqual(i, echo1(i))
37-
self.assertEqual(i, echo2(i))
45+
for i in range(randint(1, MAXSIZE * 2)): # 修改为统一使用相同的随机范围
46+
assert i == echo1(i)
47+
assert i == echo2(i)
48+
assert i == echo1(i)
49+
assert i == echo2(i)

0 commit comments

Comments
 (0)