The importing of classes using imp was tested with:


from unittest import TestCase

import os
import imp
import importer

class TestSource(TestCase):
def test_import(self):
"""
module `source` contains class `SourceCase` with attribute cow set to 'pie'
"""

path = os.path.dirname(__file__)
f, package, description = imp.find_module("source", [path])
source = imp.load_module('source', f, package, description)
SourceCase = getattr(source, "SourceCase")
case_o = SourceCase()
expected = importer.source.SourceCase()
self.assertEqual(expected.cow, case_o.cow)

# this won't work - using imp gets source.SourceCase, but import gets importer.source.SourceCase
#self.assertIsInstance(case_o, source.SourceCase)
return

def test_subdirectory_import(self):
"""
module `subsource` contains a class Subsource with attribute `apropos` set to 'nothing'
"""

top = os.path.dirname(__file__)
f, package_path, d = imp.find_module("subdir", [top])
f, mod_path, description = imp.find_module('subsource', [package_path])
subsource = imp.load_module('subdir.subsource', f, mod_path, description)
Subsource = getattr(subsource, "Subsource")
source_obj = Subsource()
expected = importer.subdir.subsource.Subsource()
self.assertEqual(expected.apropos, source_obj.apropos)
return
# end class TestSource