/usr/lib/python3/dist-packages/nose2/suite.py is in python3-nose2 0.7.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import sys
import logging
import unittest
from nose2 import util
from nose2 import events
log = logging.getLogger(__name__)
__unittest = True
#
# Layer suite class
#
class LayerSuite(unittest.BaseTestSuite):
def __init__(self, session, tests=(), layer=None):
super(LayerSuite, self).__init__(tests)
self.layer = layer
self.wasSetup = False
self.session = session
def run(self, result):
self.handle_previous_test_teardown(result)
if not self._safeMethodCall(self.setUp, result):
return
try:
for test in self:
if result.shouldStop:
break
self._safeMethodCall(self.setUpTest, result, test)
try:
test(result)
finally:
self._safeMethodCall(self.tearDownTest, result, test)
finally:
if self.wasSetup:
self._safeMethodCall(self.tearDown, result)
def handle_previous_test_teardown(self, result):
prev = getattr(result, '_previousTestClass', None)
if prev is None:
return
layer_attr = getattr(prev, 'layer', None)
if isinstance(layer_attr, LayerSuite):
return
try:
suite_obj = unittest.suite.TestSuite()
suite_obj._tearDownPreviousClass(None, result)
suite_obj._handleModuleTearDown(result)
finally:
result._previousTestClass = None
def setUp(self):
if self.layer is None:
return
setup = self._getBoundClassmethod(self.layer, 'setUp')
event = events.StartLayerSetupEvent(self.layer)
self.session.hooks.startLayerSetup(event)
if setup:
setup()
self.wasSetup = True
event = events.StopLayerSetupEvent(self.layer)
self.session.hooks.stopLayerSetup(event)
def setUpTest(self, test):
if self.layer is None:
return
# skip suites, to ensure test setup only runs once around each test
# even for sub-layer suites inside this suite.
try:
iter(test)
except TypeError:
# ok, not a suite
pass
else:
# suite-like enough for skipping
return
if getattr(test, '_layer_wasSetUp', False):
return
event = events.StartLayerSetupTestEvent(self.layer, test)
self.session.hooks.startLayerSetupTest(event)
self._allLayers(test, 'testSetUp')
test._layer_wasSetUp = True
event = events.StopLayerSetupTestEvent(self.layer, test)
self.session.hooks.stopLayerSetupTest(event)
def tearDownTest(self, test):
if self.layer is None:
return
if not getattr(test, '_layer_wasSetUp', None):
return
event = events.StartLayerTeardownTestEvent(self.layer, test)
self.session.hooks.startLayerTeardownTest(event)
self._allLayers(test, 'testTearDown', reverse=True)
event = events.StopLayerTeardownTestEvent(self.layer, test)
self.session.hooks.stopLayerTeardownTest(event)
delattr(test, '_layer_wasSetUp')
def tearDown(self):
if self.layer is None:
return
teardown = self._getBoundClassmethod(self.layer, 'tearDown')
event = events.StartLayerTeardownEvent(self.layer)
self.session.hooks.startLayerTeardown(event)
if teardown:
teardown()
event = events.StopLayerTeardownEvent(self.layer)
self.session.hooks.stopLayerTeardown(event)
def _safeMethodCall(self, method, result, *args):
try:
method(*args)
return True
except KeyboardInterrupt:
raise
except:
result.addError(self, sys.exc_info())
return False
def _allLayers(self, test, method, reverse=False):
done = set()
all_lys = util.ancestry(self.layer)
if reverse:
all_lys = [reversed(lys) for lys in reversed(all_lys)]
for lys in all_lys:
for layer in lys:
if layer in done:
continue
self._inLayer(layer, test, method)
done.add(layer)
def _inLayer(self, layer, test, method):
meth = self._getBoundClassmethod(layer, method)
if meth:
if util.num_expected_args(meth) > 1:
meth(test)
else:
meth()
def _getBoundClassmethod(self, cls, method):
"""
Use instead of :func:`getattr` to get only classmethods explicitly
defined on ``cls`` (not methods inherited from ancestors)
"""
descriptor = cls.__dict__.get(method, None)
if descriptor:
if not isinstance(descriptor, classmethod):
raise TypeError(
'The %s method on a layer must be a classmethod.' % method)
bound_method = descriptor.__get__(None, cls)
return bound_method
else:
return None
|