Skip to content

Commit 67980d4

Browse files
committed
Add running_in_worker flag to output, flush print in Pyodide.
1 parent 4a15962 commit 67980d4

File tree

2 files changed

+41
-53
lines changed

2 files changed

+41
-53
lines changed

README.md

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ those who use PyTest, when using PyScript.
7171
modules.
7272
7. The `result` of awaiting `upytest.run` is a Python dictionary containing
7373
lists of tests bucketed under the keys: `"passes"`, `"fails"` and
74-
`"skipped"`. These results are JSON serializable and can be used for further
75-
processing and analysis (again, see `main.py` for an example of this in
76-
action.)
74+
`"skipped"`. The result also provides information about the Python
75+
interpreter used to run the tests, long with a boolean flag to indicate if
76+
the tests were running in a web worker. These results are JSON serializable
77+
and can be used for further processing and analysis (again, see `main.py`
78+
for an example of this in action.)
7779
8. In your `index.html` make sure you use the `terminal` attribute
7880
when referencing your Python script (as in the `index.html` file in
7981
this repository):
@@ -83,7 +85,7 @@ those who use PyTest, when using PyScript.
8385
You should be able to use the `type` attribute of `"mpy"` (for MicroPython)
8486
and `"py"` (for Pyodide) interchangeably.
8587

86-
Finally, point your browser at your `index.html` and your should see the test
88+
Finally, point your browser at your `index.html` and you should see the test
8789
suite run.
8890

8991
### Writing tests
@@ -208,68 +210,41 @@ The output for the test suite for this module is a good example of all the
208210
different sorts of information you may see:
209211

210212
```
211-
Python interpreter: webassembly 3.4.0; MicroPython v1.24.0-preview.114.g77bd8fe5b on 2024-07-19
212-
Using ./tests/conftest.py for global setup and teardown in ./tests.
213-
Using local setup and teardown for ./tests/test_with_setup_teardown.py.
214-
Found 2 test module[s]. Running 18 test[s].
213+
Python interpreter: webassembly 3.4.0; MicroPython v1.24.0-preview.114.g77bd8fe5b on 2024-07-19
214+
Running in worker: False
215+
Using tests/conftest.py for global setup and teardown in tests/test_core_functionality.py::TestClass.
216+
Found 1 test module[s]. Running 8 test[s].
215217
216-
S.F..FFFS..S.F.SF.
218+
F.FSSF..
217219
================================= FAILURES =================================
218-
Failed: ./tests/test_core_functionality.py::test_does_not_raise_exception_fails
220+
Failed: tests/test_core_functionality.py::TestClass.test_does_not_raise_exception_fails
219221
Traceback (most recent call last):
220-
File "upytest.py", line 147, in run
221-
File "tests/test_core_functionality.py", line 77, in test_does_not_raise_exception_fails
222+
File "upytest.py", line 156, in run
223+
File "tests/test_core_functionality.py", line 127, in test_does_not_raise_exception_fails
222224
AssertionError: Did not raise expected exception. Expected ValueError; but got None.
223225
224226
225-
Failed: ./tests/test_core_functionality.py::test_fails
227+
Failed: tests/test_core_functionality.py::TestClass.test_fails
226228
Traceback (most recent call last):
227-
File "upytest.py", line 147, in run
228-
File "tests/test_core_functionality.py", line 46, in test_fails
229+
File "upytest.py", line 156, in run
230+
File "tests/test_core_functionality.py", line 119, in test_fails
229231
AssertionError: This test will fail
230232
231233
232-
Failed: ./tests/test_core_functionality.py::test_async_does_not_raise_exception_fails
233-
Traceback (most recent call last):
234-
File "upytest.py", line 145, in run
235-
File "tests/test_core_functionality.py", line 121, in test_async_does_not_raise_exception_fails
236-
AssertionError: Did not raise expected exception. Expected ValueError; but got None.
237-
238-
239-
Failed: ./tests/test_core_functionality.py::test_async_does_not_raise_expected_exception_fails
240-
Traceback (most recent call last):
241-
File "upytest.py", line 145, in run
242-
File "tests/test_core_functionality.py", line 126, in test_async_does_not_raise_expected_exception_fails
243-
AssertionError: Did not raise expected exception. Expected ValueError, AssertionError; but got TypeError.
244-
245-
246-
Failed: ./tests/test_core_functionality.py::test_does_not_raise_expected_exception_fails
234+
Failed: tests/test_core_functionality.py::TestClass.test_does_not_raise_expected_exception_fails
247235
Traceback (most recent call last):
248-
File "upytest.py", line 147, in run
249-
File "tests/test_core_functionality.py", line 85, in test_does_not_raise_expected_exception_fails
236+
File "upytest.py", line 156, in run
237+
File "tests/test_core_functionality.py", line 131, in test_does_not_raise_expected_exception_fails
250238
AssertionError: Did not raise expected exception. Expected ValueError, AssertionError; but got TypeError.
251239
252-
253-
Failed: ./tests/test_core_functionality.py::test_async_fails
254-
Traceback (most recent call last):
255-
File "upytest.py", line 145, in run
256-
File "tests/test_core_functionality.py", line 111, in test_async_fails
257-
AssertionError: This async test fails.
258-
259240
================================= SKIPPED ==================================
260-
Skipped: ./tests/test_core_functionality.py::test_when_skipped
261-
Reason: This test will be skipped with a when condition
262-
263-
Skipped: ./tests/test_core_functionality.py::test_async_when_skipped
264-
Reason: This test will be skipped with a when condition
265-
266-
Skipped: ./tests/test_core_functionality.py::test_async_skipped
267-
Reason: This async test will be skipped
268-
269-
Skipped: ./tests/test_core_functionality.py::test_skipped
241+
Skipped: tests/test_core_functionality.py::TestClass.test_skipped
270242
Reason: This test will be skipped
243+
244+
Skipped: tests/test_core_functionality.py::TestClass.test_when_skipped
245+
Reason: This test will be skipped with a skip_when condition
271246
========================= short test summary info ==========================
272-
6 failed, 4 skipped, 8 passed in 0.00 seconds
247+
3 failed, 2 skipped, 3 passed in 0.00 seconds
273248
```
274249

275250
## Developer setup

upytest.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import inspect
3232
import time
3333
from pathlib import Path
34+
from pyscript import RUNNING_IN_WORKER
3435

3536
try:
3637
# Pyodide.
@@ -259,6 +260,16 @@ def limit_tests_to(self, test_names):
259260
or (t.test_name.split(".")[0] in test_names)
260261
]
261262

263+
def print(self, text):
264+
"""
265+
Print the provided text to the console.
266+
"""
267+
if is_micropython:
268+
# MicroPython doesn't flush.
269+
print(text, end="")
270+
else:
271+
print(text, end="", flush=True)
272+
262273
async def run(self):
263274
"""
264275
Run each TestCase instance for this module. If a setup or teardown
@@ -281,11 +292,11 @@ async def run(self):
281292
else:
282293
self.teardown()
283294
if test_case.status == SKIPPED:
284-
print("\033[33;1mS\033[0m", end="")
295+
self.print("\033[33;1mS\033[0m")
285296
elif test_case.status == PASS:
286-
print("\033[32;1m.\033[0m", end="")
297+
self.print("\033[32;1m.\033[0m")
287298
else:
288-
print("\033[31;1mF\033[0m", end="")
299+
self.print("\033[31;1mF\033[0m")
289300

290301

291302
def gather_conftest_functions(conftest_path, target):
@@ -447,6 +458,7 @@ async def run(*args, **kwargs):
447458
modules.
448459
"""
449460
print("Python interpreter: \033[1m", sys.platform, sys.version, "\033[0m")
461+
print("Running in worker: \033[1m", RUNNING_IN_WORKER, "\033[0m")
450462
targets = []
451463
pattern = kwargs.get("pattern", "test_*.py")
452464
for arg in args:
@@ -524,6 +536,7 @@ async def run(*args, **kwargs):
524536
"duration": duration,
525537
"platform": sys.platform,
526538
"version": sys.version,
539+
"running_in_worker": RUNNING_IN_WORKER,
527540
"passes": [test.as_dict for test in passed_tests],
528541
"fails": [test.as_dict for test in failed_tests],
529542
"skipped": [test.as_dict for test in skipped_tests],

0 commit comments

Comments
 (0)