From b94d6c104ad4b47d623bb898d8fab36dd50a8e64 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Fri, 29 Nov 2024 17:31:54 +0100 Subject: [PATCH] Put the remove in test `test_save_registry` in try-finally block In case the tests fail after the creation of the file but before the remove, the file still is not removed and will make the test fail in the next run. --- tests/test_code.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/tests/test_code.py b/tests/test_code.py index 601c3f2..16f9380 100644 --- a/tests/test_code.py +++ b/tests/test_code.py @@ -348,26 +348,31 @@ def test_save_registry(self, function): """ Verifies that the CodeExercise works with an answer_registry. """ + name = "test_save_registry-student_name" + try: + + def print_success(code_ex: CodeExercise | None): + code_ex.output.object = "Success" + + cue_output = CueObject("Not initialized") + exercise_registry = ExerciseRegistry() + + code_ex = CodeExercise( + code=function, + parameters={"parameter": fixed(5)}, + exercise_registry=exercise_registry, + key="test_save_registry_ex", + outputs=[cue_output], + update=print_success, + ) - def print_success(code_ex: CodeExercise | None): - code_ex.output.object = "Success" - - cue_output = CueObject("Not initialized") - exercise_registry = ExerciseRegistry() - - code_ex = CodeExercise( - code=function, - parameters={"parameter": fixed(5)}, - exercise_registry=exercise_registry, - key="test_save_registry_ex", - outputs=[cue_output], - update=print_success, - ) - - exercise_registry._student_name_text.value = "test_save_registry-student_name" - exercise_registry.create_new_file_from_dropdown() - code_ex._save_button.click() - os.remove("test_save_registry-student_name.json") + exercise_registry._student_name_text.value = name + exercise_registry.create_new_file_from_dropdown() + code_ex._save_button.click() + finally: + file_name = f"{name}.json" + if os.path.exists(file_name): + os.remove(file_name) @pytest.mark.parametrize( "code_ex",