def test_generated_files(self): """generate test files on the fly""" self._testdir = 'ImportTestDir' _f_init = os.path.join(self.test_dir, self._testdir, '__init__.py') _f_error = os.path.join(self.test_dir, self._testdir, 'Error.py') _f_gen = os.path.join(self.test_dir, self._testdir, 'Gen.py') _f_module = os.path.join(self.test_dir, self._testdir, 'Module.py') self.write_to_file(_f_init) self.write_to_file(_f_error, 'raise AssertionError()') self.write_to_file(_f_gen, ''' def gen(): try: yield "yield inside try" except: pass ''') unique_line = "This is the module to test 'from ImportTestDir import Module'" self.write_to_file(_f_module, ''' value = %r a = 1 b = 2 c = 3 d = 4 e = 5 f = 6 g = 7 h = 8 i = 9 j = 10 ''' % unique_line) import sys result = imp.reload(sys) self.assertTrue(not sys.modules.__contains__("Error")) try: import ImportTestDir.Error except AssertionError: pass except ImportError: self.fail("Should have thrown AssertionError from Error.py") else: self.fail("Should have thrown AssertionError from Error.py") self.assertTrue(not sys.modules.__contains__("Error")) from ImportTestDir import Module filename = Module.__file__.lower() self.assertTrue(filename.endswith("module.py") or filename.endswith("module.pyc")) self.assertEqual(Module.__name__.lower(), "importtestdir.module") self.assertEqual(Module.value, unique_line) from ImportTestDir.Module import (a, b, c , d, e, f, g, h , i, j) for x in range(ord('a'), ord('j')+1): self.assertTrue(chr(x) in dir()) # testing double import of generators with yield inside try from ImportTestDir import Gen result = sys.modules #u = sys.modules.pop("iptest") if "ImportTestDir.Gen" in sys.modules: sys.modules.pop("ImportTestDir.Gen") from ImportTestDir import Gen self.assertEqual(next(Gen.gen()), "yield inside try")
, d, e, f, g, h , i, j) for x in range(ord('a'), ord('j')+1): Assert(chr(x) in dir()) # testing double import of generators with yield inside try from ImportTestDir import Gen result = sys.modules #u = sys.modules.pop("iptest") g = sys.modules.pop("ImportTestDir.Gen") from ImportTestDir import Gen AreEqual(Gen.gen().next(), "yield inside try") ######################################################################################### # using import in nested blocks del time # picked this up from iptest.assert_util def f(): import time now = time.time() f() try: print time except NameError: pass
def test_generated_files(self): """generate test files on the fly""" self._testdir = 'ImportTestDir' _f_init = os.path.join(self.test_dir, self._testdir, '__init__.py') _f_error = os.path.join(self.test_dir, self._testdir, 'Error.py') _f_gen = os.path.join(self.test_dir, self._testdir, 'Gen.py') _f_module = os.path.join(self.test_dir, self._testdir, 'Module.py') self.write_to_file(_f_init) self.write_to_file(_f_error, 'raise AssertionError()') self.write_to_file(_f_gen, ''' def gen(): try: yield "yield inside try" except: pass ''') unique_line = "This is the module to test 'from ImportTestDir import Module'" self.write_to_file(_f_module, ''' value = %r a = 1 b = 2 c = 3 d = 4 e = 5 f = 6 g = 7 h = 8 i = 9 j = 10 ''' % unique_line) import sys result = reload(sys) self.assertTrue(not sys.modules.__contains__("Error")) try: import ImportTestDir.Error except AssertionError: pass except ImportError: self.fail("Should have thrown AssertionError from Error.py") else: self.fail("Should have thrown AssertionError from Error.py") self.assertTrue(not sys.modules.__contains__("Error")) from ImportTestDir import Module filename = Module.__file__.lower() self.assertTrue(filename.endswith("module.py") or filename.endswith("module.pyc")) self.assertEqual(Module.__name__.lower(), "importtestdir.module") self.assertEqual(Module.value, unique_line) from ImportTestDir.Module import (a, b, c , d, e, f, g, h , i, j) for x in range(ord('a'), ord('j')+1): self.assertTrue(chr(x) in dir()) # testing double import of generators with yield inside try from ImportTestDir import Gen result = sys.modules #u = sys.modules.pop("iptest") if "ImportTestDir.Gen" in sys.modules: sys.modules.pop("ImportTestDir.Gen") from ImportTestDir import Gen self.assertEqual(Gen.gen().next(), "yield inside try")
AreEqual(Module.__name__.lower(), "importtestdir.module") AreEqual(Module.value, unique_line) from ImportTestDir.Module import (a, b, c, d, e, f, g, h, i, j) for x in range(ord('a'), ord('j') + 1): Assert(chr(x) in dir()) # testing double import of generators with yield inside try from ImportTestDir import Gen result = sys.modules #u = sys.modules.pop("iptest") g = sys.modules.pop("ImportTestDir.Gen") from ImportTestDir import Gen AreEqual(next(Gen.gen()), "yield inside try") ######################################################################################### # using import in nested blocks del time # picked this up from iptest.assert_util def f(): import time now = time.time() f() try: