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