From 4ab0f25b050cc6d29e2f68704a88e06ba61ea5aa Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 23 Jun 2009 17:10:52 +0200 Subject: [PATCH] remove scope argument from request.addfinalizer --HG-- branch : 1.0.x --- CHANGELOG | 5 ++++ doc/test/funcargs.txt | 46 +++++++++++++++----------------- py/test/funcargs.py | 8 ++++-- py/test/testing/test_funcargs.py | 20 +++----------- 4 files changed, 35 insertions(+), 44 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9982545f1..c77c95d41 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 ============================================= diff --git a/doc/test/funcargs.txt b/doc/test/funcargs.txt index b4b915eb4..45cdb137b 100644 --- a/doc/test/funcargs.txt +++ b/doc/test/funcargs.txt @@ -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 --------------------------------------------- diff --git a/py/test/funcargs.py b/py/test/funcargs.py index ec30c353e..74f36a054 100644 --- a/py/test/funcargs.py +++ b/py/test/funcargs.py @@ -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 "" %(self._pyfuncitem) diff --git a/py/test/testing/test_funcargs.py b/py/test/testing/test_funcargs.py index 1b4b4ea86..4f981c40c 100644 --- a/py/test/testing/test_funcargs.py +++ b/py/test/testing/test_funcargs.py @@ -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("""