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