diff --git a/py/test/collect.py b/py/test/collect.py index 55ec9ee05..0b1f30237 100644 --- a/py/test/collect.py +++ b/py/test/collect.py @@ -442,7 +442,9 @@ class Instance(PyCollectorMixin, Collector): Collector.Function.__get__(self)) # XXX for python 2.2 Function = property(Function) -class Generator(PyCollectorMixin, Collector): +from py.__.test.item import FunctionMixin # XXX import order issues :-( + +class Generator(FunctionMixin, PyCollectorMixin, Collector): def run(self): self._prepare() itemlist = self._name2items @@ -468,13 +470,6 @@ class Generator(PyCollectorMixin, Collector): call, args = obj, () return call, args - def _getpathlineno(self): - code = py.code.Code(self.obj) - return code.path, code.firstlineno - - def _getsortvalue(self): - return self._getpathlineno() - class DoctestFile(PyCollectorMixin, FSCollector): def run(self): return [self.fspath.basename] diff --git a/py/test/item.py b/py/test/item.py index 1add3c54f..ac4fab9d6 100644 --- a/py/test/item.py +++ b/py/test/item.py @@ -37,38 +37,15 @@ class Item(py.test.collect.Collector): def finishcapture(self): self._config._finishcapture(self) -class Function(Item): - """ a Function Item is responsible for setting up - and executing a Python callable test object. +class FunctionMixin(object): + """ mixin for the code common to Function and Generator. """ - _state = SetupState() - def __init__(self, name, parent, args=(), obj=_dummy, sort_value = None): - super(Function, self).__init__(name, parent) - self._args = args - if obj is not _dummy: - self._obj = obj - self._sort_value = sort_value - - def __repr__(self): - return "<%s %r>" %(self.__class__.__name__, self.name) - def _getpathlineno(self): code = py.code.Code(self.obj) return code.path, code.firstlineno def _getsortvalue(self): - if self._sort_value is None: - return self._getpathlineno() - return self._sort_value - - def run(self): - """ setup and execute the underlying test function. """ - self._state.prepare(self) - self.execute(self.obj, *self._args) - - def execute(self, target, *args): - """ execute the given test function. """ - target(*args) + return self._getpathlineno() def setup(self): """ perform setup for this test function. """ @@ -92,6 +69,35 @@ class Function(Item): if meth is not None: return meth(self.obj) +class Function(FunctionMixin, Item): + """ a Function Item is responsible for setting up + and executing a Python callable test object. + """ + _state = SetupState() + def __init__(self, name, parent, args=(), obj=_dummy, sort_value = None): + super(Function, self).__init__(name, parent) + self._args = args + if obj is not _dummy: + self._obj = obj + self._sort_value = sort_value + + def __repr__(self): + return "<%s %r>" %(self.__class__.__name__, self.name) + + def _getsortvalue(self): + if self._sort_value is None: + return self._getpathlineno() + return self._sort_value + + def run(self): + """ setup and execute the underlying test function. """ + self._state.prepare(self) + self.execute(self.obj, *self._args) + + def execute(self, target, *args): + """ execute the given test function. """ + target(*args) + # # triggering specific outcomes while executing Items # diff --git a/py/test/testing/test_setup_nested.py b/py/test/testing/test_setup_nested.py index 8630124b4..a053ef3ab 100644 --- a/py/test/testing/test_setup_nested.py +++ b/py/test/testing/test_setup_nested.py @@ -42,14 +42,23 @@ class TestInheritedClassSetupStillWorks(TestSimpleClassSetup): class TestSetupTeardownOnInstance(TestSimpleClassSetup): def setup_method(self, method): - self.clslevel.append(17) + self.clslevel.append(method.__name__) def teardown_method(self, method): x = self.clslevel.pop() - assert x == 17 + assert x == method.__name__ def test_setup(self): - assert self.clslevel[-1] == 17 + assert self.clslevel[-1] == 'test_setup' + + def test_generate(self): + assert self.clslevel[-1] == 'test_generate' + yield self.generated, 5 + assert self.clslevel[-1] == 'test_generate' + + def generated(self, value): + assert value == 5 + assert self.clslevel[-1] == 'test_generate' def test_teardown_method_worked(): assert not TestSetupTeardownOnInstance.clslevel