Commits

holger krekel committed e811b2a

move pytest.mark.factory/setup to pytest.factory/setup, as per flub 's suggestion

  • Participants
  • Parent commits e9293db

Comments (0)

Files changed (8)

File _pytest/__init__.py

 #
-__version__ = '2.3.0.dev7'
+__version__ = '2.3.0.dev8'

File _pytest/python.py

 from _pytest.main import getfslineno
 from _pytest.monkeypatch import monkeypatch
 from py._code.code import TerminalRepr
-from _pytest.mark import MarkInfo
 
 import _pytest
 cutdir = py.path.local(_pytest.__file__).dirpath()
 
+class FactoryMarker:
+    def __init__(self, scope, params):
+        self.scope = scope
+        self.params = params
+    def __call__(self, function):
+        function._pytestfactory = self
+        return function
+
+class SetupMarker:
+    def __init__(self, scope):
+        self.scope = scope
+    def __call__(self, function):
+        function._pytestsetup = self
+        return function
+
+# XXX a test fails when scope="function" how it should be, investigate
+def factory(scope=None, params=None):
+    """ return a decorator to mark functions as resource factories.
+
+    :arg scope: the scope for which this resource is shared, one of
+                "function", "class", "module", "session". Defaults to "function".
+    :arg params: an optional list of parameters which will cause multiple
+                invocations of tests depending on the resource.
+    """
+    return FactoryMarker(scope, params)
+
+def setup(scope="function"):
+    """ return a decorator to mark functions as setup functions.
+
+    :arg scope: the scope for which the setup function will be active, one
+                of "function", "class", "module", "session".
+                Defaults to "function".
+    """
+    return SetupMarker(scope)
+
 def cached_property(f):
     """returns a cached property that is calculated by function f.
     taken from http://code.activestate.com/recipes/576563-cached-property/"""
 def pytest_namespace():
     raises.Exception = pytest.fail.Exception
     return {
+        'factory': factory,
+        'setup': setup,
         'raises' : raises,
         'collect': {
         'Module': Module, 'Class': Class, 'Instance': Instance,
             # or are "funcarg" marked
             if not callable(obj):
                 continue
-            marker = getattr(obj, "factory", None)
-            if marker is not None and isinstance(marker, MarkInfo):
+            marker = getattr(obj, "_pytestfactory", None)
+            if marker is not None:
                 assert not name.startswith(self._argprefix)
                 argname = name
-                scope = marker.kwargs.get("scope")
-                params = marker.kwargs.get("params")
+                scope = marker.scope
+                params = marker.params
                 new = True
             elif name.startswith(self._argprefix):
                 argname = name[len(self._argprefix):]
                 new = False
             else:
                 # no funcargs. check if we have a setup function.
-                setup = getattr(obj, "setup", None)
-                if setup is not None and isinstance(setup, MarkInfo):
-                    scope = setup.kwargs.get("scope")
+                setup = getattr(obj, "_pytestsetup", None)
+                if setup is not None:
+                    scope = setup.scope
                     sf = SetupCall(self, nodeid, obj, scope)
                     self.setuplist.append(sf)
                 continue

File doc/en/conf.py

 #
 # The full version, including alpha/beta/rc tags.
 # The short X.Y version.
-version = release = "2.3.0.dev6"
+version = release = "2.3.0.dev8"
 
 import sys, os
 

File doc/en/funcarg_compare.txt

 
 It follows that pytest_configure/session/runtest_setup are often not
 appropriate for implementing common fixture needs.  Therefore, 
-pytest-2.X introduces a new "@pytest.mark.setup" marker which takes
+pytest-2.X introduces a new :ref:`@pytest.setup` marker which takes
 an optional "scope" parameter.  
 
 See :ref:`new_setup` for examples.

File doc/en/resources.txt

 
 Concretely, there are three means of resource and parametrization management:
 
-* a `@pytest.mark.factory`_ marker to define resource factories,
+* a `@pytest.factory`_ marker to define resource factories,
   their scoping and parametrization.   Factories can themselves 
   receive resources through their function arguments, easing
   the setup of interdependent resources.  They can also use
 plugins offer their own resources so that after installation
 you can simply use them in your test and setup functions.
 
-.. _`@pytest.mark.factory`:
+.. _`@pytest.factory`:
 
-``@pytest.mark.factory``: Creating parametrized, scoped resources
+``@pytest.factory``: Creating parametrized, scoped resources
 -----------------------------------------------------------------
 
 .. regendoc:wipe
 
 .. versionadded:: 2.3
 
-The `@pytest.mark.factory`_ marker allows to
+The `@pytest.factory`_ marker allows to
 
 * mark a function as a factory for resources, useable by test and setup functions 
 * define parameters in order to run tests multiple times with different
     import pytest
     import smtplib
 
-    @pytest.mark.factory(scope="session")
+    @pytest.factory(scope="session")
     def smtp(testcontext):
         smtp = smtplib.SMTP("merlinux.eu")
         testcontext.addfinalizer(smtp.close)
     import pytest
     import smtplib
 
-    @pytest.mark.factory(scope="session", 
+    @pytest.factory(scope="session", 
                          params=["merlinux.eu", "mail.python.org"])
     def smtp(testcontext):
         smtp = smtplib.SMTP(testcontext.param)
         def __init__(self, smtp):
             self.smtp = smtp
 
-    @pytest.mark.factory(scope="module")
+    @pytest.factory(scope="module")
     def app(smtp):
         return App(smtp)
 
     # content of test_module.py
     import pytest
 
-    @pytest.mark.factory(scope="module", params=["mod1", "mod2"])
+    @pytest.factory(scope="module", params=["mod1", "mod2"])
     def modarg(testcontext):
         param = testcontext.param
         print "create", param
         testcontext.addfinalizer(fin)
         return param
 
-    @pytest.mark.factory(scope="function", params=[1,2])
+    @pytest.factory(scope="function", params=[1,2])
     def otherarg(testcontext):
         return testcontext.param
 
 ``testcontext``: interacting with test context
 ---------------------------------------------------
 
-The ``testcontext`` object may be received by `@pytest.mark.factory`_ or
-`@pytest.mark.setup`_ marked functions.  It contains information relating
+The ``testcontext`` object may be received by `@pytest.factory`_ or
+`@pytest.setup`_ marked functions.  It contains information relating
 to the test context within which the function executes. Moreover, you
 can call ``testcontext.addfinalizer(myfinalizer)`` in order to trigger
 a call to ``myfinalizer`` after the last test in the test context has executed.
 If passed to a parametrized factory ``testcontext.param`` will contain
 a parameter (one value out of the ``params`` list specified with the 
-`@pytest.mark.factory`_ marker).
+`@pytest.factory`_ marker).
 
 .. autoclass:: _pytest.python.TestContext()
     :members:

File doc/en/setup.txt

 All of these features are now demonstrated by little examples.
 
 .. _`new_setup`:
-.. _`@pytest.mark.setup`:
+.. _`@pytest.setup`:
 
 basic per-function setup
 -------------------------------
     import pytest
     import os
 
-    @pytest.mark.setup()
+    @pytest.setup()
     def mydir(tmpdir):
         tmpdir.join("myfile").write("example content")
         old = tmpdir.chdir()
         assert not os.path.exists("anotherfile")
 
 Our ``mydir`` setup function is executed on a per-function basis,
-the default scope used by the ``pytest.mark.setup`` decorator.
+the default scope used by the ``pytest.setup`` decorator.
 It accesses the ``tmpdir`` resource which provides a new empty
 directory path object.  The ``test_function2`` here checks that 
 it executes with a fresh directory and specifically
     import pytest
     import os
 
-    @pytest.mark.setup()
+    @pytest.setup()
     def cleandir(tmpdir):
         old = tmpdir.chdir()
 
 If you want test modules to access a global resource,
 you can stick the resource to the module globals in
 a per-module setup function.  We use a :ref:`resource factory
-<@pytest.mark.factory>` to create our global resource::
+<@pytest.factory>` to create our global resource::
 
     # content of conftest.py
     import pytest
         def __init__(self):
             pass
 
-    @pytest.mark.factory(scope="session")
+    @pytest.factory(scope="session")
     def globresource():
         return GlobalResource()
 
-    @pytest.mark.setup(scope="module")
+    @pytest.setup(scope="module")
     def setresource(testcontext, globresource):
         testcontext.module.globresource = globresource
        
         def __init__(self, param):
             self.param = param
 
-    @pytest.mark.factory(scope="session", params=[1,2])
+    @pytest.factory(scope="session", params=[1,2])
     def globresource(testcontext):
         g = GlobalResource(testcontext.param)
         def fin():
         testcontext.addfinalizer(fin)
         return g
 
-    @pytest.mark.setup(scope="module")
+    @pytest.setup(scope="module")
     def setresource(testcontext, globresource):
         testcontext.module.globresource = globresource
 
         name='pytest',
         description='py.test: simple powerful testing with Python',
         long_description = long_description,
-        version='2.3.0.dev7',
+        version='2.3.0.dev8',
         url='http://pytest.org',
         license='MIT license',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

File testing/test_python.py

 
         result = testdir.makeconftest("""
             import pytest
-            @pytest.mark.setup
+            @pytest.setup()
             def mysetup(testcontext):
                 testcontext.uses_funcarg("db")
         """)
     def test_receives_funcargs(self, testdir):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory
+            @pytest.factory()
             def arg1():
                 return 1
 
-            @pytest.mark.factory
+            @pytest.factory()
             def arg2(arg1):
                 return arg1 + 1
 
     def test_receives_funcargs_scope_mismatch(self, testdir):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory(scope="function")
+            @pytest.factory(scope="function")
             def arg1():
                 return 1
 
-            @pytest.mark.factory(scope="module")
+            @pytest.factory(scope="module")
             def arg2(arg1):
                 return arg1 + 1
 
         testdir.makepyfile("""
             import pytest
             l = []
-            @pytest.mark.factory(params=[1,2])
+            @pytest.factory(params=[1,2])
             def arg1(testcontext):
                 l.append(1)
                 return testcontext.param
 
-            @pytest.mark.factory
+            @pytest.factory()
             def arg2(arg1):
                 return arg1 + 1
 
         testdir = request.getfuncargvalue("testdir")
         testdir.makeconftest("""
             import pytest
-            @pytest.mark.setup
+            @pytest.setup()
             def perfunction(testcontext, tmpdir):
                 pass
 
-            @pytest.mark.factory
+            @pytest.factory()
             def arg1(tmpdir):
                 pass
-            @pytest.mark.setup
+            @pytest.setup()
             def perfunction2(arg1):
                 pass
 
         testdir.makepyfile("""
             import pytest
             l = []
-            @pytest.mark.factory(scope="module")
+            @pytest.factory(scope="module")
             def arg():
                 l.append(1)
                 return 0
-            @pytest.mark.setup(scope="class")
+            @pytest.setup(scope="class")
             def something(arg):
                 l.append(2)
 
         testdir.makepyfile("""
             import pytest
             l = []
-            @pytest.mark.factory(params=[1,2])
+            @pytest.factory(params=[1,2])
             def arg(testcontext):
                 return testcontext.param
 
-            @pytest.mark.setup
+            @pytest.setup()
             def something(arg):
                 l.append(arg)
 
 
             l = []
 
-            @pytest.mark.factory(scope="session", params=[1,2])
+            @pytest.factory(scope="session", params=[1,2])
             def arg(testcontext):
                return testcontext.param
 
-            @pytest.mark.setup(scope="function")
+            @pytest.setup(scope="function")
             def append(testcontext, arg):
                 if testcontext.function.__name__ == "test_some":
                     l.append(arg)
 
             l = []
 
-            @pytest.mark.factory(scope="function", params=[1,2])
+            @pytest.factory(scope="function", params=[1,2])
             def farg(testcontext):
                 return testcontext.param
 
-            @pytest.mark.factory(scope="class", params=list("ab"))
+            @pytest.factory(scope="class", params=list("ab"))
             def carg(testcontext):
                 return testcontext.param
 
-            @pytest.mark.setup(scope="class")
+            @pytest.setup(scope="class")
             def append(testcontext, farg, carg):
                 def fin():
                     l.append("fin_%s%s" % (carg, farg))
     def test_parametrize(self, testdir):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory(params=["a", "b", "c"])
+            @pytest.factory(params=["a", "b", "c"])
             def arg(testcontext):
                 return testcontext.param
             l = []
         testdir.makepyfile("""
             import pytest
             l = []
-            @pytest.mark.factory(scope="module")
+            @pytest.factory(scope="module")
             def arg():
                 l.append(1)
                 return 1
         testdir.makepyfile("""
             import pytest
             l = []
-            @pytest.mark.factory(scope="module")
+            @pytest.factory(scope="module")
             def arg():
                 l.append(1)
                 return 1
             import pytest
             finalized = []
             created = []
-            @pytest.mark.factory(scope="module")
+            @pytest.factory(scope="module")
             def arg(testcontext):
                 created.append(1)
                 assert testcontext.scope == "module"
             import pytest
             finalized = []
             created = []
-            @pytest.mark.factory(scope="function")
+            @pytest.factory(scope="function")
             def arg(testcontext):
                 pass
         """)
         testdir.makepyfile(
             test_mod1="""
                 import pytest
-                @pytest.mark.factory(scope="session")
+                @pytest.factory(scope="session")
                 def arg(testcontext):
                     %s
                 def test_1(arg):
     def test_register_only_with_mark(self, testdir):
         testdir.makeconftest("""
             import pytest
-            @pytest.mark.factory
+            @pytest.factory()
             def arg():
                 return 1
         """)
         testdir.makepyfile(
             test_mod1="""
                 import pytest
-                @pytest.mark.factory
+                @pytest.factory()
                 def arg(arg):
                     return arg + 1
                 def test_1(arg):
     def test_parametrize_and_scope(self, testdir):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory(scope="module", params=["a", "b", "c"])
+            @pytest.factory(scope="module", params=["a", "b", "c"])
             def arg(testcontext):
                 return testcontext.param
             l = []
     def test_scope_mismatch(self, testdir):
         testdir.makeconftest("""
             import pytest
-            @pytest.mark.factory(scope="function")
+            @pytest.factory(scope="function")
             def arg(testcontext):
                 pass
         """)
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory(scope="session")
+            @pytest.factory(scope="session")
             def arg(arg):
                 pass
             def test_mismatch(arg):
         testdir.makepyfile("""
             import pytest
 
-            @pytest.mark.factory(scope="module", params=[1, 2])
+            @pytest.factory(scope="module", params=[1, 2])
             def arg(testcontext):
                 return testcontext.param
 
         testdir.makeconftest("""
             import pytest
 
-            @pytest.mark.factory(scope="session", params="s1 s2".split())
+            @pytest.factory(scope="session", params="s1 s2".split())
             def sarg():
                 pass
-            @pytest.mark.factory(scope="module", params="m1 m2".split())
+            @pytest.factory(scope="module", params="m1 m2".split())
             def marg():
                 pass
         """)
 
             l = []
 
-            @pytest.mark.factory(scope="function", params=[1,2])
+            @pytest.factory(scope="function", params=[1,2])
             def farg(testcontext):
                 return testcontext.param
 
-            @pytest.mark.factory(scope="class", params=list("ab"))
+            @pytest.factory(scope="class", params=list("ab"))
             def carg(testcontext):
                 return testcontext.param
 
-            @pytest.mark.setup(scope="class")
+            @pytest.setup(scope="class")
             def append(testcontext, farg, carg):
                 def fin():
                     l.append("fin_%s%s" % (carg, farg))
         testdir.makepyfile("""
             import pytest
 
-            @pytest.mark.factory(scope="function", params=[1, 2])
+            @pytest.factory(scope="function", params=[1, 2])
             def arg(testcontext):
                 param = testcontext.param
                 testcontext.addfinalizer(lambda: l.append("fin:%s" % param))
                 l.append("create:%s" % param)
                 return testcontext.param
 
-            @pytest.mark.factory(scope="module", params=["mod1", "mod2"])
+            @pytest.factory(scope="module", params=["mod1", "mod2"])
             def modarg(testcontext):
                 param = testcontext.param
                 testcontext.addfinalizer(lambda: l.append("fin:%s" % param))
         testdir.makepyfile("""
             import pytest
 
-            @pytest.mark.factory(scope="module", params=[1, 2])
+            @pytest.factory(scope="module", params=[1, 2])
             def arg(testcontext):
                 testcontext.config.l = l # to access from outer
                 x = testcontext.param
         testdir.makepyfile("""
             import pytest
 
-            @pytest.mark.factory(scope="function", params=[1, 2])
+            @pytest.factory(scope="function", params=[1, 2])
             def arg(testcontext):
                 x = testcontext.param
                 testcontext.addfinalizer(lambda: l.append("fin%s" % x))
         testdir.makepyfile("""
             import pytest
 
-            @pytest.mark.factory(scope="module", params=[1, 2])
+            @pytest.factory(scope="module", params=[1, 2])
             def arg(testcontext):
                 return testcontext.param
 
-            @pytest.mark.setup(scope="module")
+            @pytest.setup(scope="module")
             def mysetup(testcontext, arg):
                 testcontext.addfinalizer(lambda: l.append("fin%s" % arg))
                 l.append("setup%s" % arg)
     def test_setup(self, testdir, scope, ok, error):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.setup(scope=%r)
+            @pytest.setup(scope=%r)
             def myscoped(testcontext):
                 for x in %r:
                     assert hasattr(testcontext, x)
     def test_resource(self, testdir, scope, ok, error):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory(scope=%r)
+            @pytest.factory(scope=%r)
             def arg(testcontext):
                 for x in %r:
                     assert hasattr(testcontext, x)
     def test_newstyle_no_request(self, testdir):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory
+            @pytest.factory()
             def arg(request):
                 pass
             def test_1(arg):
     def test_setupcontext_no_param(self, testdir):
         testdir.makepyfile("""
             import pytest
-            @pytest.mark.factory(params=[1,2])
+            @pytest.factory(params=[1,2])
             def arg(testcontext):
                 return testcontext.param
 
-            @pytest.mark.setup
+            @pytest.setup()
             def mysetup(testcontext, arg):
                 assert not hasattr(testcontext, "param")
             def test_1(arg):