-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
Expand file tree
/
Copy path_isolated_sample.py
More file actions
111 lines (77 loc) · 2.82 KB
/
Copy path_isolated_sample.py
File metadata and controls
111 lines (77 loc) · 2.82 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
"""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 time
import unittest
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