re #320 fallback to test scope if the class-scoped fixture is used in non-class-based test function

--HG--
branch : 320-class-scoped-fixture-caching-is-broken-if
This commit is contained in:
Anatoly Bubenkov 2013-07-06 21:30:24 +02:00
parent c36186ce65
commit 589138ea71
3 changed files with 33 additions and 4 deletions

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