Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1 | import unittest |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 2 | import sys |
Antoine Pitrou | 392f413 | 2014-10-03 11:25:30 +0200 | [diff] [blame] | 3 | |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 4 | from test.support import import_fresh_module, run_unittest |
Georg Brandl | e5a0e0a | 2012-02-20 23:37:36 +0100 | [diff] [blame] | 5 | |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6 | TESTS = 'test.datetimetester' |
Georg Brandl | e5a0e0a | 2012-02-20 23:37:36 +0100 | [diff] [blame] | 7 | |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 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: |
Antoine Pitrou | 392f413 | 2014-10-03 11:25:30 +0200 | [diff] [blame] | 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) |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 18 | test_modules = [pure_tests, fast_tests] |
| 19 | test_suffixes = ["_Pure", "_Fast"] |
Georg Brandl | e5a0e0a | 2012-02-20 23:37:36 +0100 | [diff] [blame] | 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 | test_classes = [] |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 24 | |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 25 | for module, suffix in zip(test_modules, test_suffixes): |
Alexander Belopolsky | 4719ae7 | 2016-07-24 14:39:28 -0400 | [diff] [blame] | 26 | test_classes = [] |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 27 | for name, cls in module.__dict__.items(): |
Alexander Belopolsky | 4719ae7 | 2016-07-24 14:39:28 -0400 | [diff] [blame] | 28 | if not isinstance(cls, type): |
Georg Brandl | e5a0e0a | 2012-02-20 23:37:36 +0100 | [diff] [blame] | 29 | continue |
Alexander Belopolsky | 4719ae7 | 2016-07-24 14:39:28 -0400 | [diff] [blame] | 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 | for cls in test_classes: |
Georg Brandl | e5a0e0a | 2012-02-20 23:37:36 +0100 | [diff] [blame] | 36 | 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 Peters | 528ca53 | 2004-09-16 01:30:50 +0000 | [diff] [blame] | 49 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 50 | def test_main(): |
Georg Brandl | e5a0e0a | 2012-02-20 23:37:36 +0100 | [diff] [blame] | 51 | run_unittest(*test_classes) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 52 | |
| 53 | if __name__ == "__main__": |
| 54 | test_main() |