Skip to content

Commit 6b7b383

Browse files
committed
Ensure async test methods work in test classes.
1 parent add20d9 commit 6b7b383

File tree

3 files changed

+76
-23
lines changed

3 files changed

+76
-23
lines changed

main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@
3030

3131
expected_results = {
3232
"result_all": {
33-
"passes": 11,
34-
"fails": 9,
35-
"skipped": 6,
33+
"passes": 14,
34+
"fails": 12,
35+
"skipped": 8,
3636
},
3737
"result_random": {
38-
"passes": 11,
39-
"fails": 9,
40-
"skipped": 6,
38+
"passes": 14,
39+
"fails": 12,
40+
"skipped": 8,
4141
},
4242
"result_module": {
43-
"passes": 10,
44-
"fails": 9,
45-
"skipped": 6,
43+
"passes": 13,
44+
"fails": 12,
45+
"skipped": 8,
4646
},
4747
"result_class": {
4848
"passes": 3,

tests/test_core_functionality.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,37 @@ class TestClass:
9696
"""
9797

9898
@upytest.skip("This test will be skipped")
99-
def test_skipped(self):
99+
def test_in_class_skipped(self):
100100
assert False # This will not be executed.
101101

102102
@upytest.skip(
103103
"This test will be skipped with a skip_when condition", skip_when=True
104104
)
105-
def test_when_skipped(self):
105+
def test_in_class_when_skipped(self):
106106
assert False # This will not be executed.
107107

108108
@upytest.skip(
109109
"This test will NOT be skipped with a False-y skip_when",
110110
skip_when=False,
111111
)
112-
def test_when_not_skipped_passes(self):
112+
def test_in_class_when_not_skipped_passes(self):
113113
assert True, "This test passes"
114114

115-
def test_passes(self):
115+
def test_in_class_passes(self):
116116
assert True, "This test passes"
117117

118-
def test_fails(self):
118+
def test_in_class_fails(self):
119119
assert False, "This test will fail"
120120

121-
def test_raises_exception_passes(self):
121+
def test_in_class_raises_exception_passes(self):
122122
with upytest.raises(ValueError):
123123
raise ValueError("This is a ValueError")
124124

125-
def test_does_not_raise_exception_fails(self):
125+
def test_in_class_does_not_raise_exception_fails(self):
126126
with upytest.raises(ValueError):
127127
pass
128128

129-
def test_does_not_raise_expected_exception_fails(self):
129+
def test_in_class_does_not_raise_expected_exception_fails(self):
130130
with upytest.raises(ValueError, AssertionError):
131131
raise TypeError("This is a TypeError")
132132

@@ -175,3 +175,44 @@ async def test_async_does_not_raise_exception_fails():
175175
async def test_async_does_not_raise_expected_exception_fails():
176176
with upytest.raises(ValueError, AssertionError):
177177
raise TypeError("This is a TypeError")
178+
179+
180+
class TestAsyncClass:
181+
"""
182+
An asynchronous class based version of the above tests.
183+
"""
184+
185+
@upytest.skip("This test will be skipped")
186+
async def test_async_in_class_skipped(self):
187+
assert False # This will not be executed.
188+
189+
@upytest.skip(
190+
"This test will be skipped with a skip_when condition", skip_when=True
191+
)
192+
async def test_async_in_class_when_skipped(self):
193+
assert False # This will not be executed.
194+
195+
@upytest.skip(
196+
"This test will NOT be skipped with a False-y skip_when",
197+
skip_when=False,
198+
)
199+
async def test_async_in_class_when_not_skipped_passes(self):
200+
assert True, "This test passes"
201+
202+
async def test_async_in_class_passes(self):
203+
assert True, "This test passes"
204+
205+
async def test_async_in_class_fails(self):
206+
assert False, "This test will fail"
207+
208+
async def test_async_in_class_raises_exception_passes(self):
209+
with upytest.raises(ValueError):
210+
raise ValueError("This is a ValueError")
211+
212+
async def test_async_in_class_does_not_raise_exception_fails(self):
213+
with upytest.raises(ValueError):
214+
pass
215+
216+
async def test_async_in_class_does_not_raise_expected_exception_fails(self):
217+
with upytest.raises(ValueError, AssertionError):
218+
raise TypeError("This is a TypeError")

upytest.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,23 @@ def parse_traceback_from_exception(ex):
123123

124124
def shuffle(a_list):
125125
"""
126-
Shuffle a list, in place.
127-
126+
Shuffle a list, in place.
127+
128128
This function is needed because MicroPython does not have a random.shuffle
129129
function.
130130
131131
It falls back to random.shuffle if using CPython, otherwise it uses a
132132
simple implementation of the Fisher-Yates in-place shuffle algorithm.
133133
134-
Context:
135-
134+
Context:
135+
136136
https://stackoverflow.com/questions/73143243/are-there-any-alternatives-for-the-python-module-randoms-shuffle-function-in
137137
"""
138138
if hasattr(random, "shuffle"):
139139
random.shuffle(a_list)
140140
else:
141141
for i in range(len(a_list) - 1, 0, -1):
142-
j = random.randrange(i+1)
142+
j = random.randrange(i + 1)
143143
a_list[i], a_list[j] = a_list[j], a_list[i]
144144

145145

@@ -230,8 +230,20 @@ def __init__(self, path, module, setup=None, teardown=None):
230230
for method_name, method in item.__dict__.items():
231231
if callable(method) or is_awaitable(method):
232232
if method_name.startswith("test"):
233+
if is_awaitable(method):
234+
235+
async def method_wrapper(
236+
instance=instance,
237+
method_name=method_name,
238+
):
239+
await getattr(instance, method_name)()
240+
241+
else:
242+
method_wrapper = getattr(
243+
instance, method_name
244+
)
233245
t = TestCase(
234-
getattr(instance, method_name),
246+
method_wrapper,
235247
self.path,
236248
f"{name}.{method_name}",
237249
id(method),

0 commit comments

Comments
 (0)