From d630d02c5b4ddcd1f2499841bb303ad394c9a342 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 8 Oct 2012 08:34:21 +0200 Subject: [PATCH] remove pytest.setup usage --- _pytest/__init__.py | 2 +- _pytest/python.py | 12 +++------- setup.py | 2 +- testing/test_nose.py | 2 +- testing/test_python.py | 50 ++++++++++++++++++++-------------------- testing/test_unittest.py | 4 ++-- 6 files changed, 33 insertions(+), 39 deletions(-) diff --git a/_pytest/__init__.py b/_pytest/__init__.py index f1e87cf85..48afb1496 100644 --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.3.0.dev19' +__version__ = '2.3.0.dev20' diff --git a/_pytest/python.py b/_pytest/python.py index 80e07156b..3a00c75f5 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -24,11 +24,11 @@ class FixtureFunctionMarker: def fixture(scope="function", params=None, autoactive=False): """ (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 referenced to cause its invocation ahead of running tests: test 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 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: # direct decoration - return FixtureFunctionMarker(None, params, autoactive)(scope) + return FixtureFunctionMarker("function", params, autoactive)(scope) else: return FixtureFunctionMarker(scope, params, autoactive=autoactive) 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): """returns a cached property that is calculated by function f. taken from http://code.activestate.com/recipes/576563-cached-property/""" @@ -126,7 +121,6 @@ def pytest_namespace(): raises.Exception = pytest.fail.Exception return { 'fixture': fixture, - 'setup': setup, 'raises' : raises, 'collect': { 'Module': Module, 'Class': Class, 'Instance': Instance, diff --git a/setup.py b/setup.py index de0925a99..3b5a053a0 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ def main(): name='pytest', description='py.test: simple powerful testing with Python', long_description = long_description, - version='2.3.0.dev19', + version='2.3.0.dev20', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], diff --git a/testing/test_nose.py b/testing/test_nose.py index 9b6b53dd3..f67f6cb73 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -28,7 +28,7 @@ def test_setup_func_with_setup_decorator(): from _pytest.nose import call_optional l = [] class A: - @pytest.setup() + @pytest.fixture( autoactive=True) def f(self): l.append(1) call_optional(A(), "f") diff --git a/testing/test_python.py b/testing/test_python.py index 698cfcf62..e17058ac2 100644 --- a/testing/test_python.py +++ b/testing/test_python.py @@ -771,7 +771,7 @@ class TestMarking: def markers(request): return request.node.markers - @pytest.setup(scope="class") + @pytest.fixture(scope="class", autoactive=True) def marking(request): request.applymarker(pytest.mark.XYZ("hello")) """) @@ -1814,7 +1814,7 @@ class TestFixtureUsages: class MySetup: def __init__(self, request, arg1): request.instance.arg1 = arg1 - pytest.setup()(MySetup) + pytest.fixture(autoactive=True)(MySetup) class TestClass: def test_method(self): @@ -1949,14 +1949,14 @@ class TestSetupDiscovery: def pytest_funcarg__testdir(self, testdir): testdir.makeconftest(""" import pytest - @pytest.setup() + @pytest.fixture( autoactive=True) def perfunction(request, tmpdir): pass @pytest.fixture() def arg1(tmpdir): pass - @pytest.setup() + @pytest.fixture( autoactive=True) def perfunction2(arg1): pass @@ -1982,7 +1982,7 @@ class TestSetupDiscovery: testdir.makepyfile(""" import pytest class TestClass: - @pytest.setup() + @pytest.fixture(autoactive=True) def permethod(self, request): request.instance.funcname = request.function.__name__ def test_method1(self): @@ -2005,7 +2005,7 @@ class TestSetupDiscovery: def db(request): return request.param - @pytest.setup(enabled=enabled) + @pytest.fixture(enabled=enabled, autoactive=True) def createdb(db): pass @@ -2046,7 +2046,7 @@ class TestSetupManagement: def arg(): l.append(1) return 0 - @pytest.setup(scope="class") + @pytest.fixture(scope="class", autoactive=True) def something(arg): l.append(2) @@ -2071,7 +2071,7 @@ class TestSetupManagement: def arg(request): return request.param - @pytest.setup() + @pytest.fixture( autoactive=True) def something(arg): l.append(arg) @@ -2097,7 +2097,7 @@ class TestSetupManagement: def arg(request): return request.param - @pytest.setup(scope="function") + @pytest.fixture(scope="function", autoactive=True) def append(request, arg): if request.function.__name__ == "test_some": l.append(arg) @@ -2127,7 +2127,7 @@ class TestSetupManagement: def carg(request): return request.param - @pytest.setup(scope="function") + @pytest.fixture(scope="function", autoactive=True) def append(request, farg, carg): def fin(): l.append("fin_%s%s" % (carg, farg)) @@ -2153,13 +2153,13 @@ class TestSetupManagement: testdir.makepyfile(""" import pytest l = [] - @pytest.setup(scope="function") + @pytest.fixture(scope="function", autoactive=True) def fappend2(): l.append(2) - @pytest.setup(scope="class") + @pytest.fixture(scope="class", autoactive=True) def classappend3(): l.append(3) - @pytest.setup(scope="module") + @pytest.fixture(scope="module", autoactive=True) def mappend(): l.append(1) @@ -2179,7 +2179,7 @@ class TestSetupManagement: if metafunc.cls is not None: metafunc.parametrize("item", [1,2], scope="class") class TestClass: - @pytest.setup(scope="class") + @pytest.fixture(scope="class", autoactive=True) def addteardown(self, item, request): l.append("setup-%d" % item) request.addfinalizer(lambda: l.append("teardown-%d" % item)) @@ -2456,7 +2456,7 @@ class TestFixtureMarker: def carg(request): return request.param - @pytest.setup(scope="function") + @pytest.fixture(scope="function", autoactive=True) def append(request, farg, carg): def fin(): l.append("fin_%s%s" % (carg, farg)) @@ -2594,7 +2594,7 @@ class TestFixtureMarker: def arg(request): return request.param - @pytest.setup(scope="module") + @pytest.fixture(scope="module", autoactive=True) def mysetup(request, arg): request.addfinalizer(lambda: l.append("fin%s" % arg)) l.append("setup%s" % arg) @@ -2628,7 +2628,7 @@ class TestTestContextScopeAccess: def test_setup(self, testdir, scope, ok, error): testdir.makepyfile(""" import pytest - @pytest.setup(scope=%r) + @pytest.fixture(scope=%r, autoactive=True) def myscoped(request): for x in %r: assert hasattr(request, x) @@ -2683,7 +2683,7 @@ class TestErrors: def test_setupfunc_missing_funcarg(self, testdir): testdir.makepyfile(""" import pytest - @pytest.setup() + @pytest.fixture( autoactive=True) def gen(qwe123): return 1 def test_something(): @@ -2718,7 +2718,7 @@ class TestTestContextVarious: def arg(request): return request.param - @pytest.setup() + @pytest.fixture( autoactive=True) def mysetup(request, arg): assert not hasattr(request, "param") def test_1(arg): @@ -2731,10 +2731,10 @@ def test_setupdecorator_and_xunit(testdir): testdir.makepyfile(""" import pytest l = [] - @pytest.setup(scope='module') + @pytest.fixture(scope='module', autoactive=True) def setup_module(): l.append("module") - @pytest.setup() + @pytest.fixture( autoactive=True) def setup_function(): l.append("function") @@ -2742,10 +2742,10 @@ def test_setupdecorator_and_xunit(testdir): pass class TestClass: - @pytest.setup(scope="class") + @pytest.fixture(scope="class", autoactive=True) def setup_class(self): l.append("class") - @pytest.setup() + @pytest.fixture( autoactive=True) def setup_method(self): l.append("method") def test_method(self): @@ -2764,7 +2764,7 @@ def test_setup_funcarg_order(testdir): import pytest l = [] - @pytest.setup() + @pytest.fixture( autoactive=True) def fix1(): l.append(1) @pytest.fixture() @@ -2786,7 +2786,7 @@ def test_request_fixturenames(testdir): @pytest.fixture() def farg(arg1): pass - @pytest.setup() + @pytest.fixture( autoactive=True) def sarg(tmpdir): pass def test_function(request, farg): diff --git a/testing/test_unittest.py b/testing/test_unittest.py index a699f4a23..bab28635f 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -497,10 +497,10 @@ def test_unittest_setup_interaction(testdir): import unittest import pytest class MyTestCase(unittest.TestCase): - @pytest.setup(scope="class") + @pytest.fixture(scope="class", autoactive=True) def perclass(self, request): request.cls.hello = "world" - @pytest.setup(scope="function") + @pytest.fixture(scope="function", autoactive=True) def perfunction(self, request): request.instance.funcname = request.function.__name__