Skip to content

Commit 9f393ed

Browse files
committed
Python >= 3.8 modified TestSuite._handleClassSetUpPost in a way that broke our monkey-patch. This adds an updated monkey-patch for versions >= 3.8. Part of the investigation in #215.
1 parent b9ff0fb commit 9f393ed

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

green/suite.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def countTestCases(self):
8181
cases += test.countTestCases()
8282
return cases
8383

84-
def _handleClassSetUpFIXED(self, test, result):
84+
def _handleClassSetUpPre38(self, test, result):
8585
previousClass = getattr(result, '_previousTestClass', None)
8686
currentClass = test.__class__
8787
if currentClass == previousClass:
@@ -117,8 +117,56 @@ def _handleClassSetUpFIXED(self, test, result):
117117
self._addClassOrModuleLevelException(result, e, errorName)
118118
finally:
119119
_call_if_exists(result, '_restoreStdout')
120+
121+
def _handleClassSetUpPost38(self, test, result):
122+
previousClass = getattr(result, '_previousTestClass', None)
123+
currentClass = test.__class__
124+
if currentClass == previousClass:
125+
return
126+
if result._moduleSetUpFailed:
127+
return
128+
if getattr(currentClass, "__unittest_skip__", False):
129+
return
130+
131+
try:
132+
currentClass._classSetupFailed = False
133+
except TypeError:
134+
# test may actually be a function
135+
# so its class will be a builtin-type
136+
pass
137+
138+
setUpClass = getattr(currentClass, 'setUpClass', None)
139+
if setUpClass is not None:
140+
_call_if_exists(result, '_setupStdout')
141+
try:
142+
setUpClass()
143+
# THIS is the part Python doesn't get right
144+
except unittest.case.SkipTest as e:
145+
currentClass.__unittest_skip__ = True
146+
currentClass.__unittest_skip_why__ = str(e)
147+
# -- END of fix
148+
except Exception as e:
149+
if isinstance(result, _DebugResult):
150+
raise
151+
currentClass._classSetupFailed = True
152+
className = util.strclass(currentClass)
153+
self._createClassOrModuleLevelException(result, e,
154+
'setUpClass',
155+
className)
156+
finally:
157+
_call_if_exists(result, '_restoreStdout')
158+
if currentClass._classSetupFailed is True:
159+
currentClass.doClassCleanups()
160+
if len(currentClass.tearDown_exceptions) > 0:
161+
for exc in currentClass.tearDown_exceptions:
162+
self._createClassOrModuleLevelException(
163+
result, exc[1], 'setUpClass', className,
164+
info=exc)
165+
120166
if sys.version_info < (3,8): # pragma: no cover
121-
_handleClassSetUp = _handleClassSetUpFIXED
167+
_handleClassSetUp = _handleClassSetUpPre38
168+
else:
169+
_handleClassSetUp = _handleClassSetUpPost38
122170

123171

124172
def run(self, result):

green/test/test_suite.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,13 @@ def test_skip_in_setUpClass(self):
144144
import unittest
145145
class Skipper(unittest.TestCase):
146146
@classmethod
147-
def setUpClass(self):
147+
def setUpClass(cls):
148148
raise unittest.SkipTest("the skip reason")
149149
def test_one(self):
150150
pass
151151
def test_two(self):
152152
pass
153-
import unittest
154-
""".format(os.getpid())))
153+
"""))
155154
fh.close()
156155
os.chdir(sub_tmpdir)
157156

0 commit comments

Comments
 (0)