Skip to content

Commit 56402b8

Browse files
committed
Exceptions in user code don't cause the whole request to fail, instead they fail individual tests, greatly improving user feedback and experience
1 parent 0a67f36 commit 56402b8

File tree

4 files changed

+297
-266
lines changed

4 files changed

+297
-266
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ app.get(
3636
theme: "kepler",
3737
}),
3838
);
39+
3940
// Sorry, Scalar is just cooler than Swagger
4041
// app.get("/api-docs", swaggerUI({ url: "/doc" }));
4142

src/lib/python/unittestlib.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import os
21
import contextlib
2+
import os
33

44

55
def defaultequalityfn(x, y):
@@ -24,37 +24,47 @@ def __init__(
2424
self.equalityFunc = equalityFunc or defaultequalityfn
2525

2626
def run(self, solution):
27-
if self.is_secret:
28-
with open(os.devnull, "w") as devnull:
29-
with (
30-
contextlib.redirect_stdout(devnull),
31-
contextlib.redirect_stderr(devnull),
32-
):
33-
result = solution(*self.args)
34-
else:
35-
result = solution(*self.args)
36-
# This is to help students debug their code (their prints are not split by tests, so this helps)
37-
print(f"-- Test: {self.name}: {result} --")
38-
if (
39-
# If you override the equality function, type checking is on you!
40-
self.equalityFunc != defaultequalityfn
41-
or type(result) is type(self.expected)
42-
) and self.equalityFunc(result, self.expected):
43-
return {
44-
"name": "Ukryty test" if self.is_secret else self.name,
45-
"points": self.points,
46-
"max_points": self.points,
47-
"expected": "N/A" if self.is_secret else str(self.expected),
48-
"result": "N/A" if self.is_secret else str(result),
49-
"is_secret": self.is_secret,
50-
}
51-
else:
27+
try:
28+
if self.is_secret:
29+
with open(os.devnull, "w") as devnull:
30+
with (
31+
contextlib.redirect_stdout(devnull),
32+
contextlib.redirect_stderr(devnull),
33+
):
34+
result = solution(*self.args)
35+
else:
36+
result = solution(*self.args)
37+
# This is to help students debug their code (their prints are not split by tests, so this helps)
38+
print(f"-- Test: {self.name}: {result} --")
39+
if (
40+
# If you override the equality function, type checking is on you!
41+
self.equalityFunc != defaultequalityfn
42+
or type(result) is type(self.expected)
43+
) and self.equalityFunc(result, self.expected):
44+
return {
45+
"name": "Ukryty test" if self.is_secret else self.name,
46+
"points": self.points,
47+
"max_points": self.points,
48+
"expected": "N/A" if self.is_secret else str(self.expected),
49+
"result": "N/A" if self.is_secret else str(result),
50+
"is_secret": self.is_secret,
51+
}
52+
else:
53+
return {
54+
"name": "Ukryty test" if self.is_secret else self.name,
55+
"points": 0,
56+
"max_points": self.points,
57+
"expected": "N/A" if self.is_secret else str(self.expected),
58+
"result": "N/A" if self.is_secret else str(result),
59+
"is_secret": self.is_secret,
60+
}
61+
except Exception as e:
5262
return {
5363
"name": "Ukryty test" if self.is_secret else self.name,
5464
"points": 0,
5565
"max_points": self.points,
5666
"expected": "N/A" if self.is_secret else str(self.expected),
57-
"result": "N/A" if self.is_secret else str(result),
67+
"result": f"Exception: {str(e)}",
5868
"is_secret": self.is_secret,
5969
}
6070

0 commit comments

Comments
 (0)