remove scope argument from request.addfinalizer

--HG--
branch : 1.0.x
This commit is contained in:
holger krekel 2009-06-23 17:10:52 +02:00
parent 5d0fd33b64
commit 4ab0f25b05
4 changed files with 35 additions and 44 deletions

View File

@ -1,3 +1,8 @@
Changes between 1.0.0b3 and 1.0.0
=============================================
* remove scope-argument from request.addfinalizer() because
request.cached_setup has the scope arg. TOOWTDI.
Changes between 1.0.0b1 and 1.0.0b3
=============================================

View File

@ -120,7 +120,27 @@ to access test configuration and test context:
``request.param``: if exists was passed by a `parametrizing test generator`_
perform scoped setup and teardown
teardown/cleanup after test function execution
------------------------------------------------
.. sourcecode:: python
def addfinalizer(func):
""" call a finalizer function when test function finishes. """
Calling ``request.addfinalizer()`` is useful for scheduling teardown
functions. Here is an example for providing a ``myfile``
object that is to be closed when the test function finishes.
.. sourcecode:: python
def pytest_funcarg__myfile(self, request):
# ... create and open a unique per-function "myfile" object ...
request.addfinalizer(lambda: myfile.close())
return myfile
perform scope-specific setup and cleanup
---------------------------------------------
.. sourcecode:: python
@ -148,30 +168,6 @@ example for providing a value that is to be setup only once during a test run:
)
cleanup after test function execution
---------------------------------------------
.. sourcecode:: python
def addfinalizer(func, scope="function"):
""" register calling a a finalizer function.
scope == 'function': when the single test function run finishes.
scope == 'module': when tests in a different module are run
scope == 'session': when tests of the session have run.
"""
Calling ``request.addfinalizer()`` is useful for scheduling teardown
functions. The given scope determines when the teardown function
will be called. Here is a basic example for providing a ``myfile``
object that is to be closed when the test function finishes.
.. sourcecode:: python
def pytest_funcarg__myfile(self, request):
# ... create and open a unique per-function "myfile" object ...
request.addfinalizer(lambda: myfile.close())
return myfile
requesting values of other funcargs
---------------------------------------------

View File

@ -113,7 +113,7 @@ class FuncargRequest:
val = setup()
cache[cachekey] = val
if teardown is not None:
self.addfinalizer(lambda: teardown(val), scope=scope)
self._addfinalizer(lambda: teardown(val), scope=scope)
return val
def getfuncargvalue(self, argname):
@ -142,10 +142,14 @@ class FuncargRequest:
return None
raise ValueError("unknown finalization scope %r" %(scope,))
def addfinalizer(self, finalizer, scope="function"):
def _addfinalizer(self, finalizer, scope):
colitem = self._getscopeitem(scope)
self.config._setupstate.addfinalizer(finalizer=finalizer, colitem=colitem)
def addfinalizer(self, finalizer):
""" call the given finalizer after test function finished execution. """
self._addfinalizer(finalizer, scope="function")
def __repr__(self):
return "<FuncargRequest for %r>" %(self._pyfuncitem)

View File

@ -163,15 +163,11 @@ class TestRequest:
req._fillfuncargs()
assert item.funcargs == {'something': 1}
def test_request_addfinalizer_scopes(self, testdir):
def test_request_addfinalizer(self, testdir):
item = testdir.getitem("""
teardownlist = []
def pytest_funcarg__something(request):
for scope in ("function", "module", "session"):
request.addfinalizer(
lambda x=scope: teardownlist.append(x),
scope=scope)
request.addfinalizer(lambda: teardownlist.append(1))
def test_func(something): pass
""")
req = funcargs.FuncargRequest(item)
@ -183,16 +179,7 @@ class TestRequest:
assert not teardownlist
ss.teardown_exact(item)
print ss.stack
assert teardownlist == ['function']
ss.teardown_exact(item.parent)
assert teardownlist == ['function', 'module']
ss.teardown_all()
assert teardownlist == ['function', 'module', 'session']
def test_request_addfinalizer_unknown_scope(self, testdir):
item = testdir.getitem("def test_func(): pass")
req = funcargs.FuncargRequest(item)
py.test.raises(ValueError, "req.addfinalizer(None, scope='xyz')")
assert teardownlist == [1]
def test_request_getmodulepath(self, testdir):
modcol = testdir.getmodulecol("def test_somefunc(): pass")
@ -200,7 +187,6 @@ class TestRequest:
req = funcargs.FuncargRequest(item)
assert req.fspath == modcol.fspath
class TestRequestCachedSetup:
def test_request_cachedsetup(self, testdir):
item1,item2 = testdir.getitems("""