Skip to content

Commit eec8088

Browse files
authored
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.
1 parent fe978ca commit eec8088

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

tests/test_code.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -348,26 +348,31 @@ def test_save_registry(self, function):
348348
"""
349349
Verifies that the CodeExercise works with an answer_registry.
350350
"""
351+
name = "test_save_registry-student_name"
352+
try:
353+
354+
def print_success(code_ex: CodeExercise | None):
355+
code_ex.output.object = "Success"
356+
357+
cue_output = CueObject("Not initialized")
358+
exercise_registry = ExerciseRegistry()
359+
360+
code_ex = CodeExercise(
361+
code=function,
362+
parameters={"parameter": fixed(5)},
363+
exercise_registry=exercise_registry,
364+
key="test_save_registry_ex",
365+
outputs=[cue_output],
366+
update=print_success,
367+
)
351368

352-
def print_success(code_ex: CodeExercise | None):
353-
code_ex.output.object = "Success"
354-
355-
cue_output = CueObject("Not initialized")
356-
exercise_registry = ExerciseRegistry()
357-
358-
code_ex = CodeExercise(
359-
code=function,
360-
parameters={"parameter": fixed(5)},
361-
exercise_registry=exercise_registry,
362-
key="test_save_registry_ex",
363-
outputs=[cue_output],
364-
update=print_success,
365-
)
366-
367-
exercise_registry._student_name_text.value = "test_save_registry-student_name"
368-
exercise_registry.create_new_file_from_dropdown()
369-
code_ex._save_button.click()
370-
os.remove("test_save_registry-student_name.json")
369+
exercise_registry._student_name_text.value = name
370+
exercise_registry.create_new_file_from_dropdown()
371+
code_ex._save_button.click()
372+
finally:
373+
file_name = f"{name}.json"
374+
if os.path.exists(file_name):
375+
os.remove(file_name)
371376

372377
@pytest.mark.parametrize(
373378
"code_ex",

0 commit comments

Comments
 (0)