From eec80884f15179c4ace73dbe3c1fd5cdce125666 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Sat, 21 Dec 2024 10:58:09 +0100 Subject: [PATCH] Put the remove in test `test_save_registry` in try-finally block (#83) 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",