From 3acf8d85447f1d58c8b3d0d6997828f166dfac5f Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Wed, 24 Feb 2021 14:46:44 +0100 Subject: Add support for hidden tests --- backend/routes/forms/unittesting.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'backend/routes/forms/unittesting.py') 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 = "" -- cgit v1.2.3