remove pytest.setup usage

This commit is contained in:
holger krekel 2012-10-08 08:34:21 +02:00
parent 30b10a6950
commit d630d02c5b
6 changed files with 33 additions and 39 deletions

View File

@ -1,2 +1,2 @@
# #
__version__ = '2.3.0.dev19' __version__ = '2.3.0.dev20'

View File

@ -24,11 +24,11 @@ class FixtureFunctionMarker:
def fixture(scope="function", params=None, autoactive=False): def fixture(scope="function", params=None, autoactive=False):
""" (return a) decorator to mark a fixture factory function. """ (return a) decorator to mark a fixture factory function.
This decorator can be used (directly or with parameters) to define This decorator can be used (with or or without parameters) to define
a fixture function. The name of the fixture function can later be a fixture function. The name of the fixture function can later be
referenced to cause its invocation ahead of running tests: test referenced to cause its invocation ahead of running tests: test
modules or classes can use the pytest.mark.usefixtures(fixturename) modules or classes can use the pytest.mark.usefixtures(fixturename)
marker and test functions can directly use fixture names as input marker. Test functions can directly use fixture names as input
arguments in which case the fixture instance returned from the fixture arguments in which case the fixture instance returned from the fixture
function will be injected. function will be injected.
@ -45,17 +45,12 @@ def fixture(scope="function", params=None, autoactive=False):
""" """
if hasattr(scope, "__call__") and params is None and autoactive == False: if hasattr(scope, "__call__") and params is None and autoactive == False:
# direct decoration # direct decoration
return FixtureFunctionMarker(None, params, autoactive)(scope) return FixtureFunctionMarker("function", params, autoactive)(scope)
else: else:
return FixtureFunctionMarker(scope, params, autoactive=autoactive) return FixtureFunctionMarker(scope, params, autoactive=autoactive)
defaultfuncargprefixmarker = fixture() defaultfuncargprefixmarker = fixture()
# XXX remove in favour of fixture(autoactive=True)
def setup(scope="function"):
""" alias for fixture(scope, autoactive=True) """
return FixtureFunctionMarker(scope, params=None, autoactive=True)
def cached_property(f): def cached_property(f):
"""returns a cached property that is calculated by function f. """returns a cached property that is calculated by function f.
taken from http://code.activestate.com/recipes/576563-cached-property/""" taken from http://code.activestate.com/recipes/576563-cached-property/"""
@ -126,7 +121,6 @@ def pytest_namespace():
raises.Exception = pytest.fail.Exception raises.Exception = pytest.fail.Exception
return { return {
'fixture': fixture, 'fixture': fixture,
'setup': setup,
'raises' : raises, 'raises' : raises,
'collect': { 'collect': {
'Module': Module, 'Class': Class, 'Instance': Instance, 'Module': Module, 'Class': Class, 'Instance': Instance,

View File

@ -24,7 +24,7 @@ def main():
name='pytest', name='pytest',
description='py.test: simple powerful testing with Python', description='py.test: simple powerful testing with Python',
long_description = long_description, long_description = long_description,
version='2.3.0.dev19', version='2.3.0.dev20',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -28,7 +28,7 @@ def test_setup_func_with_setup_decorator():
from _pytest.nose import call_optional from _pytest.nose import call_optional
l = [] l = []
class A: class A:
@pytest.setup() @pytest.fixture( autoactive=True)
def f(self): def f(self):
l.append(1) l.append(1)
call_optional(A(), "f") call_optional(A(), "f")

View File

@ -771,7 +771,7 @@ class TestMarking:
def markers(request): def markers(request):
return request.node.markers return request.node.markers
@pytest.setup(scope="class") @pytest.fixture(scope="class", autoactive=True)
def marking(request): def marking(request):
request.applymarker(pytest.mark.XYZ("hello")) request.applymarker(pytest.mark.XYZ("hello"))
""") """)
@ -1814,7 +1814,7 @@ class TestFixtureUsages:
class MySetup: class MySetup:
def __init__(self, request, arg1): def __init__(self, request, arg1):
request.instance.arg1 = arg1 request.instance.arg1 = arg1
pytest.setup()(MySetup) pytest.fixture(autoactive=True)(MySetup)
class TestClass: class TestClass:
def test_method(self): def test_method(self):
@ -1949,14 +1949,14 @@ class TestSetupDiscovery:
def pytest_funcarg__testdir(self, testdir): def pytest_funcarg__testdir(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
import pytest import pytest
@pytest.setup() @pytest.fixture( autoactive=True)
def perfunction(request, tmpdir): def perfunction(request, tmpdir):
pass pass
@pytest.fixture() @pytest.fixture()
def arg1(tmpdir): def arg1(tmpdir):
pass pass
@pytest.setup() @pytest.fixture( autoactive=True)
def perfunction2(arg1): def perfunction2(arg1):
pass pass
@ -1982,7 +1982,7 @@ class TestSetupDiscovery:
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
class TestClass: class TestClass:
@pytest.setup() @pytest.fixture(autoactive=True)
def permethod(self, request): def permethod(self, request):
request.instance.funcname = request.function.__name__ request.instance.funcname = request.function.__name__
def test_method1(self): def test_method1(self):
@ -2005,7 +2005,7 @@ class TestSetupDiscovery:
def db(request): def db(request):
return request.param return request.param
@pytest.setup(enabled=enabled) @pytest.fixture(enabled=enabled, autoactive=True)
def createdb(db): def createdb(db):
pass pass
@ -2046,7 +2046,7 @@ class TestSetupManagement:
def arg(): def arg():
l.append(1) l.append(1)
return 0 return 0
@pytest.setup(scope="class") @pytest.fixture(scope="class", autoactive=True)
def something(arg): def something(arg):
l.append(2) l.append(2)
@ -2071,7 +2071,7 @@ class TestSetupManagement:
def arg(request): def arg(request):
return request.param return request.param
@pytest.setup() @pytest.fixture( autoactive=True)
def something(arg): def something(arg):
l.append(arg) l.append(arg)
@ -2097,7 +2097,7 @@ class TestSetupManagement:
def arg(request): def arg(request):
return request.param return request.param
@pytest.setup(scope="function") @pytest.fixture(scope="function", autoactive=True)
def append(request, arg): def append(request, arg):
if request.function.__name__ == "test_some": if request.function.__name__ == "test_some":
l.append(arg) l.append(arg)
@ -2127,7 +2127,7 @@ class TestSetupManagement:
def carg(request): def carg(request):
return request.param return request.param
@pytest.setup(scope="function") @pytest.fixture(scope="function", autoactive=True)
def append(request, farg, carg): def append(request, farg, carg):
def fin(): def fin():
l.append("fin_%s%s" % (carg, farg)) l.append("fin_%s%s" % (carg, farg))
@ -2153,13 +2153,13 @@ class TestSetupManagement:
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
l = [] l = []
@pytest.setup(scope="function") @pytest.fixture(scope="function", autoactive=True)
def fappend2(): def fappend2():
l.append(2) l.append(2)
@pytest.setup(scope="class") @pytest.fixture(scope="class", autoactive=True)
def classappend3(): def classappend3():
l.append(3) l.append(3)
@pytest.setup(scope="module") @pytest.fixture(scope="module", autoactive=True)
def mappend(): def mappend():
l.append(1) l.append(1)
@ -2179,7 +2179,7 @@ class TestSetupManagement:
if metafunc.cls is not None: if metafunc.cls is not None:
metafunc.parametrize("item", [1,2], scope="class") metafunc.parametrize("item", [1,2], scope="class")
class TestClass: class TestClass:
@pytest.setup(scope="class") @pytest.fixture(scope="class", autoactive=True)
def addteardown(self, item, request): def addteardown(self, item, request):
l.append("setup-%d" % item) l.append("setup-%d" % item)
request.addfinalizer(lambda: l.append("teardown-%d" % item)) request.addfinalizer(lambda: l.append("teardown-%d" % item))
@ -2456,7 +2456,7 @@ class TestFixtureMarker:
def carg(request): def carg(request):
return request.param return request.param
@pytest.setup(scope="function") @pytest.fixture(scope="function", autoactive=True)
def append(request, farg, carg): def append(request, farg, carg):
def fin(): def fin():
l.append("fin_%s%s" % (carg, farg)) l.append("fin_%s%s" % (carg, farg))
@ -2594,7 +2594,7 @@ class TestFixtureMarker:
def arg(request): def arg(request):
return request.param return request.param
@pytest.setup(scope="module") @pytest.fixture(scope="module", autoactive=True)
def mysetup(request, arg): def mysetup(request, arg):
request.addfinalizer(lambda: l.append("fin%s" % arg)) request.addfinalizer(lambda: l.append("fin%s" % arg))
l.append("setup%s" % arg) l.append("setup%s" % arg)
@ -2628,7 +2628,7 @@ class TestTestContextScopeAccess:
def test_setup(self, testdir, scope, ok, error): def test_setup(self, testdir, scope, ok, error):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
@pytest.setup(scope=%r) @pytest.fixture(scope=%r, autoactive=True)
def myscoped(request): def myscoped(request):
for x in %r: for x in %r:
assert hasattr(request, x) assert hasattr(request, x)
@ -2683,7 +2683,7 @@ class TestErrors:
def test_setupfunc_missing_funcarg(self, testdir): def test_setupfunc_missing_funcarg(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
@pytest.setup() @pytest.fixture( autoactive=True)
def gen(qwe123): def gen(qwe123):
return 1 return 1
def test_something(): def test_something():
@ -2718,7 +2718,7 @@ class TestTestContextVarious:
def arg(request): def arg(request):
return request.param return request.param
@pytest.setup() @pytest.fixture( autoactive=True)
def mysetup(request, arg): def mysetup(request, arg):
assert not hasattr(request, "param") assert not hasattr(request, "param")
def test_1(arg): def test_1(arg):
@ -2731,10 +2731,10 @@ def test_setupdecorator_and_xunit(testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
l = [] l = []
@pytest.setup(scope='module') @pytest.fixture(scope='module', autoactive=True)
def setup_module(): def setup_module():
l.append("module") l.append("module")
@pytest.setup() @pytest.fixture( autoactive=True)
def setup_function(): def setup_function():
l.append("function") l.append("function")
@ -2742,10 +2742,10 @@ def test_setupdecorator_and_xunit(testdir):
pass pass
class TestClass: class TestClass:
@pytest.setup(scope="class") @pytest.fixture(scope="class", autoactive=True)
def setup_class(self): def setup_class(self):
l.append("class") l.append("class")
@pytest.setup() @pytest.fixture( autoactive=True)
def setup_method(self): def setup_method(self):
l.append("method") l.append("method")
def test_method(self): def test_method(self):
@ -2764,7 +2764,7 @@ def test_setup_funcarg_order(testdir):
import pytest import pytest
l = [] l = []
@pytest.setup() @pytest.fixture( autoactive=True)
def fix1(): def fix1():
l.append(1) l.append(1)
@pytest.fixture() @pytest.fixture()
@ -2786,7 +2786,7 @@ def test_request_fixturenames(testdir):
@pytest.fixture() @pytest.fixture()
def farg(arg1): def farg(arg1):
pass pass
@pytest.setup() @pytest.fixture( autoactive=True)
def sarg(tmpdir): def sarg(tmpdir):
pass pass
def test_function(request, farg): def test_function(request, farg):

View File

@ -497,10 +497,10 @@ def test_unittest_setup_interaction(testdir):
import unittest import unittest
import pytest import pytest
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
@pytest.setup(scope="class") @pytest.fixture(scope="class", autoactive=True)
def perclass(self, request): def perclass(self, request):
request.cls.hello = "world" request.cls.hello = "world"
@pytest.setup(scope="function") @pytest.fixture(scope="function", autoactive=True)
def perfunction(self, request): def perfunction(self, request):
request.instance.funcname = request.function.__name__ request.instance.funcname = request.function.__name__