Skip to content

Commit 645e7f9

Browse files
committed
Doc updates. More verbose exception reporting.
1 parent b4af546 commit 645e7f9

File tree

5 files changed

+102
-38
lines changed

5 files changed

+102
-38
lines changed

CODE_OF_CONDUCT.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ are welcome here.
2222
Reciprocate! Welcome, embrace and learn from others
2323
(even in the face of apparent hostility or misunderstanding).
2424
25+
Thoughtful, careful and courteous contributions are a gift,
26+
and never knowingly diminish, dehumanize, harm or harass others.
27+
2528
To act against this spirit invites ostracism,
2629
a loss that diminishes us all.
2730

README.md

+68-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# uPyTest (MicroPytest) 🔬🐍
1+
# uPyTest (MicroPytest) 🔬🐍✔️
22

33
A small and very limited module for very simple [PyTest](https://pytest.org)
44
inspired tests to run in the [MicroPython](https://micropython.org/) runtime
@@ -58,8 +58,8 @@ will be very different.
5858

5959
To create a test suite ensure your test functions are contained in modules
6060
inside your test directory that start with `test_`. If you want to change this
61-
pattern for matching test modules pass in a `pattern` argument as a string
62-
(whose default is currently `pattern="test*.py"`).
61+
pattern for matching test modules pass in a `pattern` argument as a string to
62+
the `upytest.run` method (whose default is currently `pattern="test*.py"`).
6363

6464
Inside the test module, test functions are identified by having `test_`
6565
prepended to their name:
@@ -70,7 +70,7 @@ def test_something():
7070
```
7171

7272
Just like PyTest, use the `assert` statement to verify test expectations. As
73-
shown above, a string following a comma is used as the value for the resulting
73+
shown above, a string following a comma is used as the value for any resulting
7474
`AssertionError` should the `assert` fail.
7575

7676
Sometimes you need to skip existing tests. Simply use the `skip` decorator like
@@ -137,72 +137,104 @@ different sorts of information you may see:
137137

138138
```
139139
Using conftest.py for global setup and teardown.
140-
Found 2 test module[s]. Running 18 test[s].
140+
Found 2 test module[s]. Running 22 test[s].
141141
142-
.S.FFFF...F..SF.FF
142+
F.FF.SFF..FF.FS.FF..FF
143143
================================= FAILURES =================================
144144
145-
./tests/test_stuff.py::test_does_not_raise_exception
145+
./tests/test_with_setup_teardown.py::test_async_fails
146+
Traceback (most recent call last):
147+
File "upytest.py", line 89, in run
148+
File "test_with_setup_teardown.py", line 51, in test_async_fails
149+
AssertionError: This async test fails.
150+
151+
152+
./tests/test_with_setup_teardown.py::test_async_does_not_raise_exception
153+
Traceback (most recent call last):
154+
File "upytest.py", line 89, in run
155+
File "test_with_setup_teardown.py", line 61, in test_async_does_not_raise_exception
156+
File "upytest.py", line 238, in __exit__
157+
AssertionError: Did not raise expected exception. Expected ValueError; but got None.
158+
159+
160+
./tests/test_with_setup_teardown.py::test_async_does_not_raise_expected_exception
161+
Traceback (most recent call last):
162+
File "upytest.py", line 89, in run
163+
File "test_with_setup_teardown.py", line 66, in test_async_does_not_raise_expected_exception
164+
File "upytest.py", line 238, in __exit__
165+
AssertionError: Did not raise expected exception. Expected ValueError, AssertionError; but got TypeError.
166+
167+
168+
./tests/test_with_setup_teardown.py::test_does_not_raise_exception
146169
Traceback (most recent call last):
147170
File "upytest.py", line 91, in run
148-
File "test_stuff.py", line 24, in test_does_not_raise_exception
149-
File "upytest.py", line 229, in __exit__
150-
AssertionError: Did not raise expected exception.
171+
File "test_with_setup_teardown.py", line 33, in test_does_not_raise_exception
172+
File "upytest.py", line 238, in __exit__
173+
AssertionError: Did not raise expected exception. Expected ValueError; but got None.
151174
152175
153-
./tests/test_stuff.py::test_fails
176+
./tests/test_with_setup_teardown.py::test_does_not_raise_expected_exception
154177
Traceback (most recent call last):
155178
File "upytest.py", line 91, in run
156-
File "test_stuff.py", line 14, in test_fails
157-
AssertionError: This test fails
179+
File "test_with_setup_teardown.py", line 38, in test_does_not_raise_expected_exception
180+
File "upytest.py", line 238, in __exit__
181+
AssertionError: Did not raise expected exception. Expected ValueError, AssertionError; but got TypeError.
158182
159183
160-
./tests/test_stuff.py::test_async_fails
184+
./tests/test_with_setup_teardown.py::test_fails
161185
Traceback (most recent call last):
162-
File "upytest.py", line 89, in run
163-
File "test_stuff.py", line 37, in test_async_fails
164-
AssertionError: This async test fails.
186+
File "upytest.py", line 91, in run
187+
File "test_with_setup_teardown.py", line 23, in test_fails
188+
AssertionError: This test fails.
165189
166190
167-
./tests/test_stuff.py::test_async_does_not_raise_exception
191+
./tests/test_stuff.py::test_async_does_not_raise_expected_exception
168192
Traceback (most recent call last):
169193
File "upytest.py", line 89, in run
170-
File "test_stuff.py", line 47, in test_async_does_not_raise_exception
171-
File "upytest.py", line 229, in __exit__
172-
AssertionError: Did not raise expected exception.
194+
File "test_stuff.py", line 57, in test_async_does_not_raise_expected_exception
195+
File "upytest.py", line 238, in __exit__
196+
AssertionError: Did not raise expected exception. Expected ValueError, AssertionError; but got TypeError.
173197
174198
175-
./tests/test_with_setup_teardown.py::test_async_does_not_raise_exception
199+
./tests/test_stuff.py::test_async_fails
176200
Traceback (most recent call last):
177201
File "upytest.py", line 89, in run
178-
File "test_with_setup_teardown.py", line 56, in test_async_does_not_raise_exception
179-
File "upytest.py", line 229, in __exit__
180-
AssertionError: Did not raise expected exception.
202+
File "test_stuff.py", line 42, in test_async_fails
203+
AssertionError: This async test fails.
181204
182205
183-
./tests/test_with_setup_teardown.py::test_does_not_raise_exception
206+
./tests/test_stuff.py::test_does_not_raise_exception
184207
Traceback (most recent call last):
185208
File "upytest.py", line 91, in run
186-
File "test_with_setup_teardown.py", line 33, in test_does_not_raise_exception
187-
File "upytest.py", line 229, in __exit__
188-
AssertionError: Did not raise expected exception.
209+
File "test_stuff.py", line 24, in test_does_not_raise_exception
210+
File "upytest.py", line 238, in __exit__
211+
AssertionError: Did not raise expected exception. Expected ValueError; but got None.
189212
190213
191-
./tests/test_with_setup_teardown.py::test_async_fails
214+
./tests/test_stuff.py::test_does_not_raise_expected_exception
215+
Traceback (most recent call last):
216+
File "upytest.py", line 91, in run
217+
File "test_stuff.py", line 29, in test_does_not_raise_expected_exception
218+
File "upytest.py", line 238, in __exit__
219+
AssertionError: Did not raise expected exception. Expected ValueError, AssertionError; but got TypeError.
220+
221+
222+
./tests/test_stuff.py::test_async_does_not_raise_exception
192223
Traceback (most recent call last):
193224
File "upytest.py", line 89, in run
194-
File "test_with_setup_teardown.py", line 46, in test_async_fails
195-
AssertionError: This async test fails.
225+
File "test_stuff.py", line 52, in test_async_does_not_raise_exception
226+
File "upytest.py", line 238, in __exit__
227+
AssertionError: Did not raise expected exception. Expected ValueError; but got None.
196228
197229
198-
./tests/test_with_setup_teardown.py::test_fails
230+
./tests/test_stuff.py::test_fails
199231
Traceback (most recent call last):
200232
File "upytest.py", line 91, in run
201-
File "test_with_setup_teardown.py", line 23, in test_fails
202-
AssertionError: This test fails.
233+
File "test_stuff.py", line 14, in test_fails
234+
AssertionError: This test fails
203235
204236
========================= short test summary info ==========================
205-
8 failed, 2 skipped, 8 passed in 0.00 seconds
237+
12 failed, 2 skipped, 8 passed in 0.01 seconds
206238
```
207239

208240
## Developer setup

tests/test_stuff.py

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def test_does_not_raise_exception():
2424
pass
2525

2626

27+
def test_does_not_raise_expected_exception():
28+
with upytest.raises(ValueError, AssertionError):
29+
raise TypeError("This is a TypeError")
30+
31+
2732
@upytest.skip("This async test will be skipped")
2833
async def test_async_passes():
2934
assert False # This will not be executed.
@@ -45,3 +50,8 @@ async def test_async_raises_exception():
4550
async def test_async_does_not_raise_exception():
4651
with upytest.raises(ValueError):
4752
pass
53+
54+
55+
async def test_async_does_not_raise_expected_exception():
56+
with upytest.raises(ValueError, AssertionError):
57+
raise TypeError("This is a TypeError")

tests/test_with_setup_teardown.py

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def test_does_not_raise_exception():
3333
pass
3434

3535

36+
def test_does_not_raise_expected_exception():
37+
with upytest.raises(ValueError, AssertionError):
38+
raise TypeError("This is a TypeError")
39+
40+
3641
@upytest.skip("This async test will be skipped")
3742
async def test_async_passes():
3843
assert False # This will not be executed.
@@ -54,3 +59,8 @@ async def test_async_raises_exception():
5459
async def test_async_does_not_raise_exception():
5560
with upytest.raises(ValueError):
5661
pass
62+
63+
64+
async def test_async_does_not_raise_expected_exception():
65+
with upytest.raises(ValueError, AssertionError):
66+
raise TypeError("This is a TypeError")

upytest.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,17 @@ def __enter__(self):
225225
def __exit__(self, ex_type, ex_value, ex_tb):
226226
# Check the exception type.
227227
if ex_type not in self.expected_exceptions:
228-
# No such expected exception, so raise AssertionError.
229-
raise AssertionError("Did not raise expected exception.")
228+
# No such expected exception, so raise AssertionError with a
229+
# helpful message.
230+
message = "Did not raise expected exception."
231+
expected = ", ".join(
232+
[str(ex.__name__) for ex in self.expected_exceptions]
233+
)
234+
if ex_type:
235+
message += f" Expected {expected}; but got {ex_type.__name__}."
236+
else:
237+
message += f" Expected {expected}; but got None."
238+
raise AssertionError(message)
230239
self.exception = ex_value
231240
self.traceback = ex_tb
232241
return True # Suppress the expected exception.

0 commit comments

Comments
 (0)