remove scope argument from request.addfinalizer
--HG-- branch : 1.0.x
This commit is contained in:
parent
5d0fd33b64
commit
4ab0f25b05
|
@ -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
|
Changes between 1.0.0b1 and 1.0.0b3
|
||||||
=============================================
|
=============================================
|
||||||
|
|
|
@ -120,7 +120,27 @@ to access test configuration and test context:
|
||||||
``request.param``: if exists was passed by a `parametrizing test generator`_
|
``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
|
.. 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
|
requesting values of other funcargs
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ class FuncargRequest:
|
||||||
val = setup()
|
val = setup()
|
||||||
cache[cachekey] = val
|
cache[cachekey] = val
|
||||||
if teardown is not None:
|
if teardown is not None:
|
||||||
self.addfinalizer(lambda: teardown(val), scope=scope)
|
self._addfinalizer(lambda: teardown(val), scope=scope)
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def getfuncargvalue(self, argname):
|
def getfuncargvalue(self, argname):
|
||||||
|
@ -142,10 +142,14 @@ class FuncargRequest:
|
||||||
return None
|
return None
|
||||||
raise ValueError("unknown finalization scope %r" %(scope,))
|
raise ValueError("unknown finalization scope %r" %(scope,))
|
||||||
|
|
||||||
def addfinalizer(self, finalizer, scope="function"):
|
def _addfinalizer(self, finalizer, scope):
|
||||||
colitem = self._getscopeitem(scope)
|
colitem = self._getscopeitem(scope)
|
||||||
self.config._setupstate.addfinalizer(finalizer=finalizer, colitem=colitem)
|
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):
|
def __repr__(self):
|
||||||
return "<FuncargRequest for %r>" %(self._pyfuncitem)
|
return "<FuncargRequest for %r>" %(self._pyfuncitem)
|
||||||
|
|
||||||
|
|
|
@ -163,15 +163,11 @@ class TestRequest:
|
||||||
req._fillfuncargs()
|
req._fillfuncargs()
|
||||||
assert item.funcargs == {'something': 1}
|
assert item.funcargs == {'something': 1}
|
||||||
|
|
||||||
def test_request_addfinalizer_scopes(self, testdir):
|
def test_request_addfinalizer(self, testdir):
|
||||||
item = testdir.getitem("""
|
item = testdir.getitem("""
|
||||||
teardownlist = []
|
teardownlist = []
|
||||||
def pytest_funcarg__something(request):
|
def pytest_funcarg__something(request):
|
||||||
for scope in ("function", "module", "session"):
|
request.addfinalizer(lambda: teardownlist.append(1))
|
||||||
request.addfinalizer(
|
|
||||||
lambda x=scope: teardownlist.append(x),
|
|
||||||
scope=scope)
|
|
||||||
|
|
||||||
def test_func(something): pass
|
def test_func(something): pass
|
||||||
""")
|
""")
|
||||||
req = funcargs.FuncargRequest(item)
|
req = funcargs.FuncargRequest(item)
|
||||||
|
@ -183,16 +179,7 @@ class TestRequest:
|
||||||
assert not teardownlist
|
assert not teardownlist
|
||||||
ss.teardown_exact(item)
|
ss.teardown_exact(item)
|
||||||
print ss.stack
|
print ss.stack
|
||||||
assert teardownlist == ['function']
|
assert teardownlist == [1]
|
||||||
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')")
|
|
||||||
|
|
||||||
def test_request_getmodulepath(self, testdir):
|
def test_request_getmodulepath(self, testdir):
|
||||||
modcol = testdir.getmodulecol("def test_somefunc(): pass")
|
modcol = testdir.getmodulecol("def test_somefunc(): pass")
|
||||||
|
@ -200,7 +187,6 @@ class TestRequest:
|
||||||
req = funcargs.FuncargRequest(item)
|
req = funcargs.FuncargRequest(item)
|
||||||
assert req.fspath == modcol.fspath
|
assert req.fspath == modcol.fspath
|
||||||
|
|
||||||
|
|
||||||
class TestRequestCachedSetup:
|
class TestRequestCachedSetup:
|
||||||
def test_request_cachedsetup(self, testdir):
|
def test_request_cachedsetup(self, testdir):
|
||||||
item1,item2 = testdir.getitems("""
|
item1,item2 = testdir.getitems("""
|
||||||
|
|
Loading…
Reference in New Issue