[svn r40702] (pedronis, arigo)

Add setup/teardown calls around generators, with the same semantics as
around functions and methods.

--HG--
branch : trunk
This commit is contained in:
arigo 2007-03-18 16:59:45 +01:00
parent 1b639695b4
commit 5506b03f81
3 changed files with 47 additions and 37 deletions

View File

@ -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]

View File

@ -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)
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
#

View File

@ -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