blob: 242e1bba035bf90cf643324b5b8081c8b008c544 [file] [log] [blame]
Tim Peters2a799bf2002-12-16 20:18:38 +00001import unittest
Alexander Belopolskycf86e362010-07-23 19:25:47 +00002import sys
Antoine Pitrou392f4132014-10-03 11:25:30 +02003
Alexander Belopolskycf86e362010-07-23 19:25:47 +00004from test.support import import_fresh_module, run_unittest
Georg Brandle5a0e0a2012-02-20 23:37:36 +01005
Alexander Belopolskycf86e362010-07-23 19:25:47 +00006TESTS = 'test.datetimetester'
Georg Brandle5a0e0a2012-02-20 23:37:36 +01007
Alexander Belopolskycf86e362010-07-23 19:25:47 +00008try:
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'])
13finally:
Antoine Pitrou392f4132014-10-03 11:25:30 +020014 # 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)
Alexander Belopolskycf86e362010-07-23 19:25:47 +000018test_modules = [pure_tests, fast_tests]
19test_suffixes = ["_Pure", "_Fast"]
Georg Brandle5a0e0a2012-02-20 23:37:36 +010020# 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.
23test_classes = []
Tim Peters2a799bf2002-12-16 20:18:38 +000024
Alexander Belopolskycf86e362010-07-23 19:25:47 +000025for module, suffix in zip(test_modules, test_suffixes):
Alexander Belopolsky4719ae72016-07-24 14:39:28 -040026 test_classes = []
Alexander Belopolskycf86e362010-07-23 19:25:47 +000027 for name, cls in module.__dict__.items():
Alexander Belopolsky4719ae72016-07-24 14:39:28 -040028 if not isinstance(cls, type):
Georg Brandle5a0e0a2012-02-20 23:37:36 +010029 continue
Alexander Belopolsky4719ae72016-07-24 14:39:28 -040030 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 for cls in test_classes:
Georg Brandle5a0e0a2012-02-20 23:37:36 +010036 cls.__name__ = name + suffix
37 @classmethod
38 def setUpClass(cls_, module=module):
39 cls_._save_sys_modules = sys.modules.copy()
40 sys.modules[TESTS] = module
41 sys.modules['datetime'] = module.datetime_module
42 sys.modules['_strptime'] = module._strptime
43 @classmethod
44 def tearDownClass(cls_):
45 sys.modules.clear()
46 sys.modules.update(cls_._save_sys_modules)
47 cls.setUpClass = setUpClass
48 cls.tearDownClass = tearDownClass
Tim Peters528ca532004-09-16 01:30:50 +000049
Tim Peters2a799bf2002-12-16 20:18:38 +000050def test_main():
Georg Brandle5a0e0a2012-02-20 23:37:36 +010051 run_unittest(*test_classes)
Tim Peters2a799bf2002-12-16 20:18:38 +000052
53if __name__ == "__main__":
54 test_main()