fix issue320 - fix class scope for fixtures when mixed with

module-level functions.  Thanks Anatloy Bubenkoff.
This commit is contained in:
holger krekel 2013-07-08 15:54:38 +02:00
commit 13ddce2381
5 changed files with 37 additions and 4 deletions

View File

@ -9,6 +9,7 @@ Floris Bruynooghe
Jason R. Coombs Jason R. Coombs
Wouter van Ackooy Wouter van Ackooy
Samuele Pedroni Samuele Pedroni
Anatoly Bubenkoff
Brianna Laugher Brianna Laugher
Carl Friedrich Bolz Carl Friedrich Bolz
Armin Rigo Armin Rigo

View File

@ -1,6 +1,9 @@
Changes between 2.3.5 and 2.4.DEV Changes between 2.3.5 and 2.4.DEV
----------------------------------- -----------------------------------
- fix issue320 - fix class scope for fixtures when mixed with
module-level functions. Thanks Anatloy Bubenkoff.
- you can specify "-q" or "-qq" to get different levels of "quieter" - you can specify "-q" or "-qq" to get different levels of "quieter"
reporting (thanks Katarzyna Jachim) reporting (thanks Katarzyna Jachim)

View File

@ -1317,7 +1317,8 @@ class FixtureRequest(FuncargnamesCompatAttr):
x = self._pyfuncitem.getparent(pytest.Class) x = self._pyfuncitem.getparent(pytest.Class)
if x is not None: if x is not None:
return x return x
scope = "module" # fallback to function
return self._pyfuncitem
if scope == "module": if scope == "module":
return self._pyfuncitem.getparent(pytest.Module) return self._pyfuncitem.getparent(pytest.Module)
raise ValueError("unknown finalization scope %r" %(scope,)) raise ValueError("unknown finalization scope %r" %(scope,))

View File

@ -513,12 +513,12 @@ class TestRequestCachedSetup:
def test_request_cachedsetup_class(self, testdir): def test_request_cachedsetup_class(self, testdir):
reprec = testdir.inline_runsource(""" reprec = testdir.inline_runsource("""
mysetup = ["hello", "hello2"].pop mysetup = ["hello", "hello2", "hello3"].pop
def pytest_funcarg__something(request): def pytest_funcarg__something(request):
return request.cached_setup(mysetup, scope="class") return request.cached_setup(mysetup, scope="class")
def test_func1(something): def test_func1(something):
assert something == "hello2" assert something == "hello3"
def test_func2(something): def test_func2(something):
assert something == "hello2" assert something == "hello2"
class TestClass: class TestClass:
@ -1090,7 +1090,7 @@ class TestAutouseManagement:
def arg(): def arg():
l.append(1) l.append(1)
return 0 return 0
@pytest.fixture(scope="class", autouse=True) @pytest.fixture(scope="module", autouse=True)
def something(arg): def something(arg):
l.append(2) l.append(2)

View File

@ -0,0 +1,28 @@
"""Tests for fixtures with different scoping."""
def test_class_scope_with_normal_tests(testdir):
testpath = testdir.makepyfile("""
import pytest
class Box:
value = 0
@pytest.fixture(scope='class')
def a(request):
Box.value += 1
return Box.value
def test_a(a):
assert a == 1
class Test1:
def test_b(self, a):
assert a == 2
class Test2:
def test_c(self, a):
assert a == 3""")
reprec = testdir.inline_run(testpath)
for test in ['test_a', 'test_b', 'test_c']:
assert reprec.matchreport(test).passed