From 124afe48738fb0daa327d86b790747b03e30d548 Mon Sep 17 00:00:00 2001 From: Renzo Lucioni Date: Wed, 9 Dec 2015 22:18:24 -0500 Subject: [PATCH] Make modifies_courseware robust to exceptions raised during tests Guarantees that SharedModuleStoreTestCase's reset() method is always called, regardless of whether decorated test cases raise exceptions. --- .../xmodule/modulestore/tests/django_utils.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py index c0feac00fa..a7353de502 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py @@ -317,10 +317,20 @@ class SharedModuleStoreTestCase(TestCase): @functools.wraps(f) def wrapper(*args, **kwargs): """Call the object method, and reset the test case afterwards.""" - return_val = f(*args, **kwargs) - obj = args[0] - obj.reset() - return return_val + try: + # Attempt execution of the test. + return_val = f(*args, **kwargs) + except: + # If the test raises an exception, re-raise it. + raise + else: + # Otherwise, return the test's return value. + return return_val + finally: + # In either case, call SharedModuleStoreTestCase.reset() "on the way out." + # For more, see here: https://docs.python.org/2/tutorial/errors.html#defining-clean-up-actions. + obj = args[0] + obj.reset() return wrapper