From e4bdacbb61a6894a8515bd5bae100cb978514028 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Fri, 13 Aug 2010 22:28:02 +0200 Subject: [PATCH] Add test_import_error_message_contains_complete_class_path test for get_class --- mopidy/utils.py | 5 ++++- tests/utils_test.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mopidy/utils.py b/mopidy/utils.py index ff032b4e..b8aa574c 100644 --- a/mopidy/utils.py +++ b/mopidy/utils.py @@ -24,7 +24,10 @@ def get_class(name): module_name = name[:name.rindex('.')] class_name = name[name.rindex('.') + 1:] logger.debug('Loading: %s', name) - module = import_module(module_name) + try: + module = import_module(module_name) + except ImportError: + raise ImportError("Couldn't load: %s" % name) class_object = getattr(module, class_name) return class_object diff --git a/tests/utils_test.py b/tests/utils_test.py index 9a8f1129..d5beade2 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -16,6 +16,12 @@ class GetClassTest(unittest.TestCase): test = lambda: get_class('foo.bar.Baz') self.assertRaises(ImportError, test) + def test_import_error_message_contains_complete_class_path(self): + try: + get_class('foo.bar.Baz') + except ImportError as e: + self.assert_('foo.bar.Baz' in str(e)) + def test_loading_existing_class(self): cls = get_class('unittest.TestCase') self.assertEqual(cls.__name__, 'TestCase')