aboutsummaryrefslogtreecommitdiffstats
path: root/backend/routes/forms/unittesting.py
diff options
context:
space:
mode:
authorGravatar Matteo Bertucci <[email protected]>2021-02-24 14:46:44 +0100
committerGravatar Matteo Bertucci <[email protected]>2021-02-24 14:46:44 +0100
commit3acf8d85447f1d58c8b3d0d6997828f166dfac5f (patch)
tree69d4be10c349894a63af1f2d062c2f98a9e4c82f /backend/routes/forms/unittesting.py
parentReturn 403 on failed tests (diff)
Add support for hidden tests
Diffstat (limited to 'backend/routes/forms/unittesting.py')
-rw-r--r--backend/routes/forms/unittesting.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/backend/routes/forms/unittesting.py b/backend/routes/forms/unittesting.py
index fe8320f..ddf0843 100644
--- a/backend/routes/forms/unittesting.py
+++ b/backend/routes/forms/unittesting.py
@@ -1,5 +1,6 @@
import ast
from collections import namedtuple
+from itertools import count
from textwrap import indent
from typing import Optional
@@ -19,7 +20,7 @@ def _make_unit_code(units: dict[str, str]) -> str:
result = ""
for unit_name, unit_code in units.items():
- result += f"\ndef test_{unit_name}(unit):\n{indent(unit_code, ' ')}"
+ result += f"\ndef test_{unit_name.lstrip('#')}(unit):\n{indent(unit_code, ' ')}"
return indent(result, " ")
@@ -48,6 +49,13 @@ async def execute_unittest(form_response: FormResponse, form: Form) -> list[Unit
if question.type == "code" and "unittests" in question.data:
passed = False
+ hidden_test_counter = count(1)
+ hidden_tests = {
+ test.lstrip("#"): next(hidden_test_counter)
+ for test in question.data["unittests"].keys()
+ if test.startswith("#")
+ }
+
unit_code = _make_unit_code(question.data["unittests"])
user_code = _make_user_code(form_response.response[question.id])
@@ -78,7 +86,14 @@ async def execute_unittest(form_response: FormResponse, form: Form) -> list[Unit
passed = bool(int(stdout[0]))
if not passed:
- result = stdout[1:].strip()
+ failed_tests = stdout[1:].strip().split(";")
+
+ # Redact failed hidden tests
+ for i, failed_test in enumerate(failed_tests[:]):
+ if failed_test in hidden_tests:
+ failed_tests[i] = f"hidden_test_{hidden_tests[failed_test]}"
+
+ result = ";".join(failed_tests)
else:
result = ""