-
-
Notifications
You must be signed in to change notification settings - Fork 36.9k
Expand file tree
/
Copy path_isolated_sample.py
More file actions
190 lines (128 loc) · 4.88 KB
/
Copy path_isolated_sample.py
File metadata and controls
190 lines (128 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Sample tests driven by test.test_support.TestIsolated.
This module is imported, never run as a test file, so that
:func:`test.support.isolation.runInSubprocess` has a real, importable target to run in
a subprocess. Several of these tests fail, error or are skipped on purpose.
"""
import atexit
import os
import sys
import time
import unittest
from test import support
from test.support import isolation
# DurationSample sleeps this long in the subprocess; a parent-reported duration
# close to it proves the subprocess timing was forwarded, not the replay time.
DURATION_SLEEP = 0.2
class MethodSample(unittest.TestCase):
@isolation.runInSubprocess()
def test_pass(self):
self.assertTrue(isolation.runningInSubprocess)
@isolation.runInSubprocess()
def test_fail(self):
self.assertEqual(1, 2)
@isolation.runInSubprocess()
def test_error(self):
raise RuntimeError('boom')
@isolation.runInSubprocess()
def test_skip(self):
self.skipTest('nope')
@isolation.runInSubprocess()
@unittest.expectedFailure
def test_expected_failure(self):
self.assertEqual(1, 2)
@isolation.runInSubprocess()
@unittest.expectedFailure
def test_unexpected_success(self):
pass
@isolation.runInSubprocess()
class ClassSample(unittest.TestCase):
def test_pass(self):
self.assertTrue(isolation.runningInSubprocess)
def test_fail(self):
self.assertEqual(1, 2)
@unittest.expectedFailure
def test_expected_failure(self):
self.assertEqual(1, 2)
class SubtestSample(unittest.TestCase):
@isolation.runInSubprocess()
def test_subtests(self):
for i in range(3):
with self.subTest(i=i):
self.assertNotEqual(i, 1)
@isolation.runInSubprocess()
class DurationSample(unittest.TestCase):
def test_slow(self):
time.sleep(DURATION_SLEEP)
@isolation.runInSubprocess()
class SubclassingSample(unittest.TestCase):
# setUpClass must run bound to the runtime class, so a subclass sees its own
# name here rather than the base class's.
@classmethod
def setUpClass(cls):
cls.setup_class_name = cls.__name__
def setUp(self):
self.set_up = True
def test_runtime_class(self):
self.assertEqual(self.setup_class_name, type(self).__name__)
class SubclassSample(SubclassingSample):
# What a subclass adds or overrides must run in the subprocess too.
def setUp(self):
super().setUp()
self.set_up_in_subclass = True
def test_added_in_subclass(self):
self.assertTrue(isolation.runningInSubprocess)
self.assertTrue(self.set_up)
self.assertTrue(self.set_up_in_subclass)
class BrokenSubclassSample(SubclassingSample):
# An overriding setUpClass() that does not call super() bypasses the
# subprocess entirely.
@classmethod
def setUpClass(cls):
pass
# The exit code the samples below die with, after their tests have run.
EXIT_CODE = 3
def _die_at_exit():
atexit.register(os._exit, EXIT_CODE)
class MethodExitSample(unittest.TestCase):
@isolation.runInSubprocess()
def test_passes_then_dies(self):
_die_at_exit()
@isolation.runInSubprocess()
def test_fails_and_dies(self):
_die_at_exit()
self.fail('the test itself failed')
@isolation.runInSubprocess()
class ClassExitSample(unittest.TestCase):
def test_pass(self):
pass
def test_dies(self):
_die_at_exit()
@isolation.runInSubprocess(options=['-X', 'dev', '-W', 'error::BytesWarning'])
class OptionsSample(unittest.TestCase):
def test_options_applied(self):
self.assertTrue(sys.flags.dev_mode)
self.assertIn('error::BytesWarning', sys.warnoptions)
class EnvSample(unittest.TestCase):
@isolation.runInSubprocess(env={'_PYTHON_ISOLATION_PROBE': 'set-by-test'})
def test_env_set(self):
self.assertEqual(os.environ.get('_PYTHON_ISOLATION_PROBE'), 'set-by-test')
@isolation.runInSubprocess(env={'_PYTHON_ISOLATION_PROBE': None})
def test_env_unset(self):
self.assertNotIn('_PYTHON_ISOLATION_PROBE', os.environ)
@isolation.runInSubprocess()
def test_env_inherited(self):
# Without env= the subprocess inherits the parent environment as it is.
self.assertEqual(os.environ.get('_PYTHON_ISOLATION_PROBE'), 'set-by-parent')
# TimeoutSample hangs this long, so that the timeout always fires first.
TIMEOUT_HANG = 60.0
TIMEOUT = 0.5
class TimeoutSample(unittest.TestCase):
@isolation.runInSubprocess(timeout=TIMEOUT)
def test_hang(self):
time.sleep(TIMEOUT_HANG)
class BigmemSample(unittest.TestCase):
@support.bigmemtest(size=1024, memuse=1)
def test_where_it_runs(self, size):
# A real run is isolated by bigmemtest() itself, a dummy run is not.
self.assertEqual(isolation.runningInSubprocess,
bool(support.real_max_memuse))