Add test_import_error_message_contains_complete_class_path test for get_class

This commit is contained in:
Thomas Adamcik 2010-08-13 22:28:02 +02:00
parent 059f96814d
commit e4bdacbb61
2 changed files with 10 additions and 1 deletions

View File

@ -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

View File

@ -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')