Home | History | Annotate | Download | only in test
      1 import unittest
      2 import sys
      3 
      4 from test.support import import_fresh_module, run_unittest
      5 
      6 TESTS = 'test.datetimetester'
      7 
      8 try:
      9     pure_tests = import_fresh_module(TESTS, fresh=['datetime', '_strptime'],
     10                                      blocked=['_datetime'])
     11     fast_tests = import_fresh_module(TESTS, fresh=['datetime',
     12                                                    '_datetime', '_strptime'])
     13 finally:
     14     # XXX: import_fresh_module() is supposed to leave sys.module cache untouched,
     15     # XXX: but it does not, so we have to cleanup ourselves.
     16     for modname in ['datetime', '_datetime', '_strptime']:
     17         sys.modules.pop(modname, None)
     18 test_modules = [pure_tests, fast_tests]
     19 test_suffixes = ["_Pure", "_Fast"]
     20 # XXX(gb) First run all the _Pure tests, then all the _Fast tests.  You might
     21 # not believe this, but in spite of all the sys.modules trickery running a _Pure
     22 # test last will leave a mix of pure and native datetime stuff lying around.
     23 all_test_classes = []
     24 
     25 for module, suffix in zip(test_modules, test_suffixes):
     26     test_classes = []
     27     for name, cls in module.__dict__.items():
     28         if not isinstance(cls, type):
     29             continue
     30         if issubclass(cls, unittest.TestCase):
     31             test_classes.append(cls)
     32         elif issubclass(cls, unittest.TestSuite):
     33             suit = cls()
     34             test_classes.extend(type(test) for test in suit)
     35     test_classes = sorted(set(test_classes), key=lambda cls: cls.__qualname__)
     36     for cls in test_classes:
     37         cls.__name__ += suffix
     38         cls.__qualname__ += suffix
     39         @classmethod
     40         def setUpClass(cls_, module=module):
     41             cls_._save_sys_modules = sys.modules.copy()
     42             sys.modules[TESTS] = module
     43             sys.modules['datetime'] = module.datetime_module
     44             sys.modules['_strptime'] = module._strptime
     45         @classmethod
     46         def tearDownClass(cls_):
     47             sys.modules.clear()
     48             sys.modules.update(cls_._save_sys_modules)
     49         cls.setUpClass = setUpClass
     50         cls.tearDownClass = tearDownClass
     51     all_test_classes.extend(test_classes)
     52 
     53 def test_main():
     54     run_unittest(*all_test_classes)
     55 
     56 if __name__ == "__main__":
     57     test_main()
     58