Skip to content

Commit

Permalink
Work around Python's failure to skip tests in a consistent way -- in …
Browse files Browse the repository at this point in the history
…particular, if SkipTest was raised in setUpClass(), then different versions of Python did different things, none of them very sensible. We take the approach that Python _should have_ taken and skip each test as the SkipTest exception is raised. This results in one skip entry per test in the class that was skipped.
  • Loading branch information
CleanCut committed May 8, 2017
1 parent 6d38419 commit c988919
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion green/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from fnmatch import fnmatch
import sys
from unittest.suite import _isnotsuite, TestSuite
from unittest.suite import _call_if_exists, _isnotsuite, TestSuite
import unittest
from io import StringIO

from green.config import default_args
Expand Down Expand Up @@ -79,6 +80,44 @@ def countTestCases(self):
cases += test.countTestCases()
return cases

def _handleClassSetUp(self, test, result):
previousClass = getattr(result, '_previousTestClass', None)
currentClass = test.__class__
if currentClass == previousClass:
return
if result._moduleSetUpFailed:
return
if getattr(currentClass, "__unittest_skip__", False):
return

try:
currentClass._classSetupFailed = False
except TypeError:
# test may actually be a function
# so its class will be a builtin-type
pass

setUpClass = getattr(currentClass, 'setUpClass', None)
if setUpClass is not None:
_call_if_exists(result, '_setupStdout')
try:
setUpClass()
# THIS is the part Python forgot to implement -- so Green will
except unittest.case.SkipTest as e:
currentClass.__unittest_skip__ = True
currentClass.__unittest_skip_why__ = str(e)
# -- END of fix
except Exception as e:
if isinstance(result, _DebugResult):
raise
currentClass._classSetupFailed = True
className = util.strclass(currentClass)
errorName = 'setUpClass (%s)' % className
self._addClassOrModuleLevelException(result, e, errorName)
finally:
_call_if_exists(result, '_restoreStdout')


def run(self, result):
"""
Emulate unittest's behavior, with Green-specific changes.
Expand Down

0 comments on commit c988919

Please sign in to comment.