[py-svn] commit/pytest: 8 new changesets

Bitbucket commits-noreply at bitbucket.org
Fri Jul 20 14:17:04 CEST 2012


8 new commits in pytest:


https://bitbucket.org/hpk42/pytest/changeset/ffe816408bb9/
changeset:   ffe816408bb9
user:        hpk42
date:        2012-07-16 11:11:26
summary:     V3 draft of resource api
affected #:  7 files

diff -r 4f076fee4f6d9f1710701507fded8fe994d9431a -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 _pytest/main.py
--- a/_pytest/main.py
+++ b/_pytest/main.py
@@ -610,6 +610,7 @@
                         yield x
             node.ihook.pytest_collectreport(report=rep)
 
+    # XXX not used yet
     def register_resource_factory(self, name, factoryfunc,
                                   matchscope=None,
                                   cachescope=None):
@@ -634,4 +635,3 @@
 
 
 
-


diff -r 4f076fee4f6d9f1710701507fded8fe994d9431a -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 doc/en/conf.py
--- a/doc/en/conf.py
+++ b/doc/en/conf.py
@@ -27,6 +27,7 @@
 #sys.path.insert(0, os.path.abspath('.'))
 
 autodoc_member_order = "bysource"
+todo_include_todos = 1
 
 # -- General configuration -----------------------------------------------------
 


diff -r 4f076fee4f6d9f1710701507fded8fe994d9431a -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 doc/en/contents.txt
--- a/doc/en/contents.txt
+++ b/doc/en/contents.txt
@@ -23,5 +23,6 @@
    :hidden:
 
    changelog.txt
-   examples/resources.txt
+   resources
+   example/resources_attic
 


diff -r 4f076fee4f6d9f1710701507fded8fe994d9431a -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 doc/en/example/resources.txt
--- a/doc/en/example/resources.txt
+++ /dev/null
@@ -1,369 +0,0 @@
-
-V2: Creating and working with parametrized test resources
-===============================================================
-
-pytest-2.X provides generalized resource parametrization, unifying
-and extending all existing funcarg and parametrization features of
-previous pytest versions.  Existing test suites and plugins written
-for previous pytest versions shall run unmodified.
-
-This V2 draft focuses on incorporating feedback provided by Floris Bruynooghe, 
-Carl Meyer and Ronny Pfannschmidt. It remains as draft documentation, pending 
-further refinements and changes according to implementation or backward 
-compatibility issues. The main changes to V1 are:
-
-* changed API names (atnode -> scopenode)
-* register_factory now happens at Node.collect_init() or pytest_collection_init
-  time.  It will raise an Error if called during the runtestloop
-  (which performs setup/call/teardown for each collected test).
-* new examples and notes related to @parametrize and metafunc.parametrize()
-* use 2.X as the version for introduction - not sure if 2.3 or 2.4 will
-  actually bring it.
-* examples/uses which were previously not possible to implement easily
-  are marked with "NEW" in the title.
-
-(NEW) the init_collection and init_runtestloop hooks
-------------------------------------------------------
-
-pytest for a long time offers a pytest_configure and a pytest_sessionstart
-hook which are often used to setup global resources.  This suffers from
-several problems:
-
-1. in distributed testing the master process would setup test resources
-   that are never needed because it only co-ordinates the test run
-   activities of the slave processes.  
-
-2. In large test suites resources are created which might not be needed 
-   for the concrete test run.  
-
-3. Thirdly, even if you only perform a collection (with "--collectonly") 
-   resource-setup will be executed.  
-
-4. there is no place way to allow global parametrized collection and setup 
-
-The existing hooks are not a good place regarding these issues. pytest-2.X 
-solves all these issues through the introduction of two specific hooks
-(and the new register_factory/getresource API)::
-
-    def pytest_init_collection(session):
-        # called ahead of pytest_collection, which implements the
-        # collection process
-        
-    def pytest_init_runtestloop(session):
-        # called ahead of pytest_runtestloop() which executes the
-        # setup and calling of tests
-
-The pytest_init_collection hook can be used for registering resources,
-see `global resource management`_ and `parametrizing global resources`_.  
-
-The init_runtests can be used to setup and/or interact with global 
-resources.  If you just use a global resource, you may explicitely
-use it in a function argument or through a `class resource attribute`_.
-
-.. _`global resource management`:
-
-managing a global database resource
----------------------------------------------------------------
-
-If you have one database object which you want to use in tests
-you can write the following into a conftest.py file::
-
-    # contest of conftest.py
-
-    class Database:
-        def __init__(self):
-            print ("database instance created")
-        def destroy(self):
-            print ("database instance destroyed")
-
-    def factory_db(name, node):
-        db = Database()
-        node.addfinalizer(db.destroy)
-        return db
-
-    def pytest_init_collection(session):
-        session.register_factory("db", factory_db)
-
-You can then access the constructed resource in a test by specifying
-the pre-registered name in your function definition::
-
-    def test_something(db):
-        ...
-
-The "db" function argument will lead to a lookup and call of the respective
-factory function and its result will be passed to the function body.  
-As the factory is registered on the session, it will by default only
-get called once per session and its value will thus be re-used across
-the whole test session.  
-
-Previously, factories would need to call the ``request.cached_setup()``
-method to manage caching.  Here is how we could implement the above 
-with traditional funcargs::
-
-    # content of conftest.py 
-    class DataBase: 
-        ... as above
-
-    def pytest_funcarg__db(request):
-        return request.cached_setup(setup=DataBase, 
-                                    teardown=lambda db: db.destroy,
-                                    scope="session")
-
-As the funcarg factory is automatically registered by detecting its
-name and because it is called each time "db" is requested, it needs 
-to care for caching itself, here by calling the cached_setup() method
-to manage it.  As it encodes the caching scope in the factory code body,
-py.test has no way to report this via e. g. "py.test --funcargs".
-More seriously, it's not exactly trivial to provide parametrization: 
-we would need to add a "parametrize" decorator where the resource is
-used or implement a pytest_generate_tests(metafunc) hook to
-call metafunc.parametrize() with the "db" argument, and then the 
-factory would need to care to pass the appropriate "extrakey" into 
-cached_setup().  By contrast, the new way just requires a modified
-call to register factories::
-
-    def pytest_init_collection(session):
-        session.register_factory("db", [factory_mysql, factory_pg])
-
-and no other code needs to change or get decorated.
-
-(NEW) instantiating one database for each test module
----------------------------------------------------------------
-
-If you want one database instance per test module you can restrict
-caching by modifying the "scopenode" parameter of the registration 
-call above:
-
-    def pytest_init_collection(session):
-        session.register_factory("db", factory_db, scopenode=pytest.Module)
-
-Neither the tests nor the factory function will need to change.
-This means that you can decide the scoping of resources at runtime -
-e.g. based on a command line option: for developer settings you might
-want per-session and for Continous Integration runs you might prefer
-per-module or even per-function scope like this::
-
-    def pytest_init_collection(session):
-        session.register_factory("db", factory_db, 
-                                 scopenode=pytest.Function)
-
-Using a resource from another resource factory
-----------------------------------------------
-
-You can use the database resource from a another resource factory through
-the ``node.getresource()`` method.  Let's add a resource factory for
-a "db_users" table at module-level, extending the previous db-example::
-
-    def pytest_init_collection(session):
-        ...
-        # this factory will be using a scopenode=pytest.Module because
-        # it is defined in a test module.
-        session.register_factory("db_users", createusers)
-
-    def createusers(name, node):
-        db = node.getresource("db")
-        table = db.create_table("users", ...)
-        node.addfinalizer(lambda: db.destroy_table("users")
-
-    def test_user_creation(db_users):
-        ...
-
-The create-users will be called for each module.  After the tests in
-that module finish execution, the table will be destroyed according
-to registered finalizer.  Note that calling getresource() for a resource
-which has a tighter scope will raise a LookupError because the
-is not available at a more general scope. Concretely, if you
-table is defined as a per-session resource and the database object as a
-per-module one, the table creation cannot work on a per-session basis.
-
-amending/decorating a resource / funcarg__ compatibility
-----------------------------------------------------------------------
-
-If you want to decorate a session-registered resource with
-a test-module one, you can do the following::
-
-    # content of conftest.py
-    def pytest_init_collection(session):
-        session.register_factory("db_users", createusers)
-
-This will register a db_users method on a per-session basis.
-If you want to create a dummy user such that all test
-methods in a test module can work with it::
-
-    # content of test_user_admin.py
-    def setup_class(cls, db_users):
-
-    def pytest_init_collection(session):
-        session.register_factory("db_users", createcreate_users, 
-                                 scopenode=pytest.Module)
-
-    def create_users(name, node):
-        # get the session-managed resource
-        db_users = node.getresource(name)
-        # add a user and define a remove_user undo function
-        ...
-        node.addfinalizer(remove_user)
-        return db_users
-
-    def test_user_fields(db_users):
-        # work with db_users with a pre-created entry
-        ...
-
-Using the pytest_funcarg__ mechanism, you can do the equivalent::
-
-    # content of test_user_admin.py
-
-    def pytest_funcarg__db_users(request):
-        def create_user():
-            db_users = request.getfuncargvalue("db_users")
-            # add a user
-            return db_users
-        def remove_user(db_users):
-            ...
-        return request.cached_setup(create_user, remove_user, scope="module")
-
-As the funcarg mechanism is implemented in terms of the new API
-it's also possible to mix - use register_factory/getresource at plugin-level
-and pytest_funcarg__ factories at test module level.
-
-As discussed previously with `global resource management`_, the funcarg-factory
-does not easily extend to provide parametrization. 
-
-
-.. _`class resource attributes`:
-
-(NEW) Setting resources as class attributes 
--------------------------------------------
-
-If you want to make an attribute available on a test class, you can 
-use a new mark::
-
-    @pytest.mark.class_resource("db")
-    class TestClass:
-        def test_something(self):
-            #use self.db 
-
-Note that this way of using resources work with unittest.TestCase-style
-tests as well.  If you have defined "db" as a parametrized resource,
-the functions of the Test class will be run multiple times with different
-values found in "self.db".
-
-Previously, pytest could not offer its resource management features
-since those were tied to passing function arguments ("funcargs") and
-this cannot be easily integrated with the unittest framework and its
-common per-project customizations. 
-
-
-.. _`parametrizing global resources`:
-
-(NEW) parametrizing global resources
-----------------------------------------------------
-
-If you want to rerun tests with different resource values you can specify
-a list of factories instead of just one::
-
-    def pytest_init_collection(session):
-        session.register_factory("db", [factory1, factory2])
-
-In this case all tests that require the "db" resource will be run twice
-using the respective values obtained from the two factory functions.
-
-For reporting purposes you might want to also define identifiers
-for the db values::
-
-    def pytest_init_collection(session):
-        session.register_factory("db", [factory1, factory2],
-                                 ids=["mysql", "pg"])
-
-This will make pytest use the respective id values when reporting
-nodeids.
-
-
-(New) Declaring resource usage / implicit parametrization
-----------------------------------------------------------
-
-Sometimes you may have a resource that can work in multiple variants,
-like using different database backends. As another use-case,
-pytest's own test suite uses a "testdir" funcarg which helps to setup
-example scenarios, perform a subprocess-pytest run and check the output.
-However, there are many features that should also work with the pytest-xdist
-mode, distributing tests to multiple CPUs or hosts.  The invocation
-variants are not visible in the function signature and cannot be easily
-addressed through a "parametrize" decorator or call.  Nevertheless we want 
-to have both invocation variants to be collected and executed. 
-
-The solution is to tell pytest that you are using a resource implicitely::
-
-    @pytest.mark.uses_resource("invocation-option")
-    class TestClass:
-        def test_method(self, testdir):
-            ...
-
-When the testdir factory gets the parametrized "invocation-option"
-resource, it will see different values, depending on what the respective
-factories provide.  To register the invocation-mode factory you would write::
-
-    # content of conftest.py
-    def pytest_init_collection(session):
-        session.register_factory("invocation-option", 
-                                 [lambda **kw: "", lambda **kw: "-n1"])
-
-The testdir factory can then access it easily::
-
-    option = node.getresource("invocation-option", "")
-    ...
-
-.. note::
-    
-   apart from the "uses_resource" decoration none of the already
-   written test functions needs to be modified for the new API.
-   
-   The implicit "testdir" parametrization only happens for the tests
-   which declare use of the invocation-option resource.  All other
-   tests will get the default value passed as the second parameter
-   to node.getresource() above.  You can thus restrict 
-   running the variants to particular tests or test sets. 
-
-To conclude, these three code fragments work together to allow efficient 
-cross-session resource parametrization.
-
-
-Implementation and compatibility notes
-============================================================
-
-The new API is designed to support all existing resource parametrization
-and funcarg usages.  This chapter discusses implementation aspects.
-Feel free to choose ignorance and only consider the above usage-level.
-
-Implementing the funcarg mechanism in terms of the new API
--------------------------------------------------------------
-
-Prior to pytest-2.X, pytest mainly advertised the "funcarg" mechanism 
-for resource management.  It provides automatic registration of
-factories through discovery of ``pytest_funcarg__NAME`` factory methods
-on plugins, test modules, classes and functions.  Those factories are be 
-called *each time* a resource (funcarg) is required, hence the support
-for a ``request.cached_setup" method which helps to cache resources 
-across calls.  Request objects internally keep a (item, requested_name,
-remaining-factories) state.  The "reamaining-factories" state is
-used for implementing decorating factories; a factory for a given
-name can call ``getfuncargvalue(name)`` to invoke the next-matching
-factory factories and then amend the return value.
-
-In order to implement the existing funcarg mechanism through
-the new API, the new API needs to internally keep around similar
-state.  XXX
-
-As an example let's consider the Module.setup_collect() method::
-
-    class Module(PyCollector):
-        def setup_collect(self):
-            for name, func in self.obj.__dict__.items():
-                if name.startswith("pytest_funcarg__"):
-                    resourcename = name[len("pytest_funcarg__"):]
-                    self.register_factory(resourcename, 
-                                          RequestAdapter(self, name, func))
-
-The request adapater takes care to provide the pre-2.X API for funcarg
-factories, i.e. request.cached_setup/addfinalizer/getfuncargvalue
-methods and some attributes.


diff -r 4f076fee4f6d9f1710701507fded8fe994d9431a -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 doc/en/funcargs.txt
--- a/doc/en/funcargs.txt
+++ b/doc/en/funcargs.txt
@@ -110,12 +110,13 @@
 The request object passed to factories
 -----------------------------------------
 
-Each funcarg factory receives a :py:class:`~_pytest.main.Request` object which
+Each funcarg factory receives a :py:class:`~_pytest.python.Request` object which
 provides methods to manage caching and finalization in the context of the
 test invocation as well as several attributes of the the underlying test item.  In fact, as of version pytest-2.3, the request API is implemented on all Item 
 objects and therefore the request object has general :py:class:`Node attributes and methods <_pytest.main.Node>` attributes.  This is a backward compatible 
 change so no changes are neccessary for pre-2.3 funcarg factories.
 
+
 .. _`parametrizing tests, generalized`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/
 
 .. _`blog post about the monkeypatch funcarg`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/


diff -r 4f076fee4f6d9f1710701507fded8fe994d9431a -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 doc/en/plugins.txt
--- a/doc/en/plugins.txt
+++ b/doc/en/plugins.txt
@@ -296,6 +296,7 @@
 
 The :py:mod:`_pytest.terminal` reported specifically uses
 the reporting hook to print information about a test run.
+
 Collection hooks
 ------------------------------
 
@@ -309,6 +310,7 @@
 you can use the following hook:
 
 .. autofunction:: pytest_pycollect_makeitem
+.. autofunction:: pytest_generate_tests
 
 
 Reporting hooks
@@ -329,7 +331,7 @@
 Reference of objects involved in hooks
 ===========================================================
 
-.. autoclass:: _pytest.main.Request()
+.. autoclass:: _pytest.python.Request()
     :members:
 
 .. autoclass:: _pytest.config.Config()


diff -r 4f076fee4f6d9f1710701507fded8fe994d9431a -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 doc/en/resources.txt
--- /dev/null
+++ b/doc/en/resources.txt
@@ -0,0 +1,426 @@
+
+V3: Creating and working with parametrized test resources
+===============================================================
+
+**Target audience**: Reading this document requires basic knowledge of 
+python testing, xUnit setup methods and the basic pytest funcarg mechanism,
+see http://pytest.org/latest/funcargs.html 
+
+**Abstract**: pytest-2.X provides more powerful and more flexible funcarg
+and setup machinery.  It does so by introducing a new @funcarg and a
+new @setup marker which allows to define scoping and parametrization
+parameters.  If using ``@funcarg``, following the ``pytest_funcarg__`` 
+naming pattern becomes optional.  Functions decorated with ``@setup`` 
+are called independenlty from the definition of funcargs but can 
+access funcarg values if needed.  This allows for ultimate flexibility
+in designing your test fixtures and their parametrization. Also,
+you can now use ``py.test --collectonly`` to inspect your fixture 
+setup.  Nonwithstanding these extensions, pre-existing test suites 
+and plugins written to work for previous pytest versions shall run unmodified.
+
+
+**Changes**: This V3 draft is based on incorporating and thinking about
+feedback provided by Floris Bruynooghe, Carl Meyer and Samuele Pedroni.
+It remains as draft documentation, pending further refinements and
+changes according to implementation or backward compatibility issues.
+The main changes to V2 are:
+
+* Collapse funcarg factory decorator into a single "@funcarg" one.
+  You can specify scopes and params with it.  Moreover, if you supply
+  a "name" you do not need to follow the "pytest_funcarg__NAME" naming
+  pattern.  Keeping with "funcarg" naming arguable now makes more
+  sense since the main interface using these resources are test and
+  setup functions. Keeping it probably causes the least semantic friction.
+
+* Drop setup_directory/setup_session and introduce a new @setup
+  decorator similar to the @funcarg one but accepting funcargs.
+
+* cosnider the extended setup_X funcargs for dropping because
+  the new @setup decorator probably is more flexible and introduces
+  less implementation complexity.
+
+.. currentmodule:: _pytest
+
+
+Shortcomings of the previous pytest_funcarg__ mechanism
+---------------------------------------------------------
+
+The previous funcarg mechanism calls a factory each time a
+funcarg for a test function is requested.  If a factory wants
+t re-use a resource across different scopes, it often used 
+the ``request.cached_setup()`` helper to manage caching of 
+resources.  Here is a basic example how we could implement 
+a per-session Database object::
+
+    # content of conftest.py 
+    class Database:
+        def __init__(self):
+            print ("database instance created")
+        def destroy(self):
+            print ("database instance destroyed")
+
+    def pytest_funcarg__db(request):
+        return request.cached_setup(setup=DataBase, 
+                                    teardown=lambda db: db.destroy,
+                                    scope="session")
+
+There are some problems with this approach:
+
+1. Scoping resource creation is not straight forward, instead one must
+   understand the intricate cached_setup() method mechanics.
+
+2. parametrizing the "db" resource is not straight forward: 
+   you need to apply a "parametrize" decorator or implement a
+   :py:func:`~hookspec.pytest_generate_tests` hook 
+   calling :py:func:`~python.Metafunc.parametrize` which
+   performs parametrization at the places where the resource 
+   is used.  Moreover, you need to modify the factory to use an 
+   ``extrakey`` parameter containing ``request.param`` to the 
+   :py:func:`~python.Request.cached_setup` call.
+
+3. the current implementation is inefficient: it performs factory discovery
+   each time a "db" argument is required.  This discovery wrongly happens at 
+   setup-time.
+
+4. there is no way how you can use funcarg factories, let alone 
+   parametrization, when your tests use the xUnit setup_X approach.
+
+5. there is no way to specify a per-directory scope for caching.
+
+In the following sections, API extensions are presented to solve 
+each of these problems. 
+
+
+Direct scoping of funcarg factories
+--------------------------------------------------------
+
+Instead of calling cached_setup(), you can decorate your factory
+to state its scope::
+
+    @pytest.mark.funcarg(scope="session")
+    def pytest_funcarg__db(request):
+        # factory will only be invoked once per session - 
+        db = DataBase()
+        request.addfinalizer(db.destroy)  # destroy when session is finished
+        return db
+
+This factory implementation does not need to call ``cached_setup()`` anymore
+because it will only be invoked once per session.  Moreover, the 
+``request.addfinalizer()`` registers a finalizer according to the specified
+resource scope on which the factory function is operating.  With this new
+scoping, the still existing ``cached_setup()`` should be much less used
+but will remain for compatibility reasons and for the case where you
+still want to have your factory get called on a per-item basis.
+
+
+Direct parametrization of funcarg resource factories 
+----------------------------------------------------------
+
+Previously, funcarg factories could not directly cause parametrization.
+You needed to specify a ``@parametrize`` or implement a ``pytest_generate_tests`` hook to perform parametrization, i.e. calling a test multiple times
+with different value sets.  pytest-2.X introduces a decorator for use
+on the factory itself::
+
+    @pytest.mark.funcarg(params=["mysql", "pg"])
+    def pytest_funcarg__db(request):
+        ...
+
+Here the factory will be invoked twice (with the respective "mysql" 
+and "pg" values set as ``request.param`` attributes) and and all of 
+the tests requiring "db" will run twice as well.  The "mysql" and 
+"pg" values will also be used for reporting the test-invocation variants.
+
+This new way of parametrizing funcarg factories should in many cases
+allow to re-use already written factories because effectively
+``request.param`` are already the parametrization attribute for test 
+functions/classes were parametrized via
+:py:func:`~_pytest.python.Metafunc.parametrize(indirect=True)` calls.
+
+Of course it's perfectly fine to combine parametrization and scoping::
+
+    @pytest.mark.funcarg(scope="session", params=["mysql", "pg"])
+    def pytest_funcarg__db(request):
+        if request.param == "mysql":
+            db = MySQL()
+        elif request.param == "pg":
+            db = PG()
+        request.addfinalizer(db.destroy)  # destroy when session is finished
+        return db
+
+This would execute all tests requiring the per-session "db" resource twice,
+receiving the values created by the two respective invocations to the
+factory function.
+
+Direct usage of funcargs with funcargs factories
+----------------------------------------------------------
+
+You can now directly use funcargs in funcarg factories.  Example::
+
+    @pytest.mark.funcarg(scope="session")
+    def db(request, tmpdir):
+        # tmpdir is a session-specific tempdir
+
+Apart from convenience it also solves an issue when your factory
+depends on a parametrized funcarg.  Previously, a call to 
+``request.getfuncargvalue()`` would not allow pytest to know
+at collection time about the fact that a required resource is
+actually parametrized.
+
+The "pytest_funcarg__" prefix becomes optional
+-----------------------------------------------------
+
+When using the ``@funcarg`` decorator you do not need to use
+the ``pytest_funcarg__`` prefix any more::
+
+    @pytest.mark.funcarg
+    def db(request):
+        ...
+
+The name under which the funcarg resource can be requested is ``db``.
+Any ``pytest_funcarg__`` prefix will be stripped. Note that a an
+unqualified funcarg-marker implies a scope of "function" meaning
+that the funcarg factory will be called for each test function invocation.
+
+
+
+support for a new @setup marker
+------------------------------------------------------
+
+pytest for a long time offered a pytest_configure and a pytest_sessionstart
+hook which are often used to setup global resources.  This suffers from
+several problems:
+
+1. in distributed testing the master process would setup test resources
+   that are never needed because it only co-ordinates the test run
+   activities of the slave processes.  
+
+2. if you only perform a collection (with "--collectonly") 
+   resource-setup will still be executed.  
+
+3. If a pytest_sessionstart is contained in some subdirectories
+   conftest.py file, it will not be called.  This stems from the
+   fact that this hook is actually used for reporting, in particular
+   the test-header with platform/custom information.
+
+4. there is no direct way how you can restrict setup to a directory scope.
+
+Moreover, it is today not easy to define scoped setup from plugins or
+conftest files other than to implement a ``pytest_runtest_setup()`` hook
+and caring for scoping/caching yourself.  And it's virtually impossible
+to do this with parametrization as ``pytest_runtest_setup()`` is called
+during test execution and parametrization happens at collection time.
+
+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, accepting
+the same parameters as the @funcargs decorator.  The difference is
+that the decorated function can accept function arguments itself
+Example::
+    
+    # content of conftest.py
+    import pytest
+    @pytest.mark.setup(scope="session")
+    def mysetup(db):
+        ...
+
+This ``mysetup`` function is going to be executed when the first
+test in the directory tree executes.  It is going to be executed once
+per-session and it receives the ``db`` funcarg which must be of same
+of higher scope; you e. g. generally cannot use a per-module or per-function 
+scoped resource in a session-scoped setup function.
+
+You can also use ``@setup`` inside a test module or class::
+
+    # content of test_module.py
+    import pytest
+
+    @pytest.mark.setup(scope="module", params=[1,2,3])
+    def modes(tmpdir, request):
+        # ...
+
+This would execute the ``modes`` function once for each parameter.
+In addition to normal funcargs you can also receive the "request"
+funcarg which represents a takes on each of the values in the
+``params=[1,2,3]`` decorator argument. 
+
+.. note::
+   
+  For each scope, the funcargs will be setup and then the setup functions
+  will be called.  This allows @setup-decorated functions to depend
+  on already setup funcarg values by accessing ``request.funcargs``.
+
+Using funcarg resources in xUnit setup methods
+------------------------------------------------------------
+
+XXX Consider this feature in contrast to the @setup feature - probably
+introducing one of them is better and the @setup decorator is more flexible.
+
+For a long time, pytest has recommended the usage of funcarg 
+factories as a primary means for managing resources in your test run.
+It is a better approach than the jUnit-based approach in many cases, even 
+more with the new pytest-2.X features, because the funcarg resource factory
+provides a single place to determine scoping and parametrization.  Your tests 
+do not need to encode setup/teardown details in every test file's 
+setup_module/class/method.  
+
+However, the jUnit methods originally introduced by pytest to Python,
+remain popoular with nose and unittest-based test suites.  Without question,
+there are large existing test suites using this paradigm.  pytest-2.X
+recognizes this fact and now offers direct integration with funcarg resources.  Here is a basic example for getting a per-module tmpdir::
+
+    def setup_module(mod, tmpdir):
+        mod.tmpdir = tmpdir
+
+This will trigger pytest's funcarg mechanism to create a value of
+"tmpdir" which can then be used throughout the module as a global.
+
+The new extension to setup_X methods also works in case a resource is 
+parametrized. For example, let's consider an setup_class example using
+our "db" resource::
+
+    class TestClass:
+        def setup_class(cls, db):
+            cls.db = db
+            # perform some extra things on db
+            # so that test methods can work with it
+
+With pytest-2.X the setup* methods will be discovered at collection-time,
+allowing to seemlessly integrate this approach with parametrization,
+allowing the factory specification to determine all details. The
+setup_class itself does not itself need to be aware of the fact that 
+"db" might be a mysql/PG database.
+Note that if the specified resource is provided only as a per-testfunction
+resource, collection would early on report a ScopingMismatch error.
+
+
+the "directory" caching scope
+--------------------------------------------
+
+All API accepting a scope (:py:func:`cached_setup()`  and
+the new funcarg/setup decorators) now also accept a "directory"
+specification.  This allows to restrict/cache resource values on a
+per-directory level.
+
+funcarg and setup discovery now happens at collection time
+---------------------------------------------------------------------
+
+pytest-2.X takes care to discover funcarg factories and setup_X methods
+at collection time.  This is more efficient especially for large test suites. 
+Moreover, a call to "py.test --collectonly" should be able to show
+a lot of setup-information and thus presents a nice method to get an
+overview of resource management in your project.
+
+Implementation level 
+===================================================================
+
+To implement the above new features, pytest-2.X grows some new hooks and
+methods.  At the time of writing V2 and without actually implementing
+it, it is not clear how much of this new internal API will also be
+exposed and advertised e. g. for plugin writers. 
+
+The main effort, however, will lie in revising what is done at
+collection and what at test setup time.  All funcarg factories and
+xUnit setup methods need to be discovered at collection time
+for the above mechanism to work.  Additionally all test function
+signatures need to be parsed in order to know which resources are
+used.  On the plus side, all previously collected fixtures and
+test functions only need to be called, no discovery is neccessary
+is required anymore.
+
+the "request" object incorporates scope-specific behaviour
+------------------------------------------------------------------
+
+funcarg factories receive a request object to help with implementing
+finalization and inspection of the requesting-context.  If there is
+no scoping is in effect, nothing much will change of the API behaviour.
+However, with scoping the request object represents the according context.
+Let's consider this example::
+
+    @pytest.mark.factory_scope("class")
+    def pytest_funcarg__db(request):
+        # ...
+        request.getfuncargvalue(...)
+        #
+        request.addfinalizer(db)
+
+Due to the class-scope, the request object will:
+
+- provide a ``None`` value for the ``request.function`` attribute. 
+- default to per-class finalization with the addfinalizer() call.
+- raise a ScopeMismatchError if a more broadly scoped factory
+  wants to use a more tighly scoped factory (e.g. per-function)
+
+In fact, the request object is likely going to provide a "node" 
+attribute, denoting the current collection node on which it internally
+operates.  (Prior to pytest-2.3 there already was an internal
+_pyfuncitem).
+
+As these are rather intuitive extensions, not much friction is expected 
+for test/plugin writers using the new scoping and parametrization mechanism. 
+It's, however, a serious internal effort to reorganize the pytest 
+implementation.
+
+
+node.register_factory/getresource() methods
+--------------------------------------------------------
+
+In order to implement factory- and setup-method discovery at
+collection time, a new node API will be introduced to allow
+for factory registration and a getresource() call to obtain
+created values.  The exact details of this API remain subject
+to experimentation. The basic idea is to introduce two new
+methods to the Session class which is already available on all nodes
+through the ``node.session`` attribute::
+
+    class Session:
+        def register_resource_factory(self, name, factory_or_list, scope):
+            """ register a resource factory for the given name.
+
+            :param name: Name of the resource.
+            :factory_or_list: a function or a list of functions creating
+                              one or multiple resource values.
+            :param scope: a node instance. The factory will be only visisble
+                          available for all descendant nodes.
+                          specify the "session" instance for global availability
+            """
+
+        def getresource(self, name, node):
+            """ get a named resource for the give node.
+
+            This method looks up a matching funcarg resource factory 
+            and calls it.
+            """
+
+.. todo::
+
+    XXX While this new API (or some variant of it) may suffices to implement
+    all of the described new usage-level features, it remains unclear how the
+    existing "@parametrize" or "metafunc.parametrize()" calls will map to it.
+    These parametrize-approaches tie resource parametrization to the 
+    function/funcargs-usage rather than to the factories. 
+
+
+
+ISSUES
+--------------------------
+
+decorating a parametrized funcarg factory:
+
+    @pytest.mark.funcarg(scope="session", params=["mysql", "pg"])
+    def db(request):
+        ...
+    class TestClass:
+        @pytest.mark.funcarg(scope="function")
+        def something(self, request):
+            session_db = request.getfuncargvalue("db")
+            ...
+
+Here the function-scoped "something" factory uses the session-scoped
+"db" factory to perform some additional steps.  The dependency, however,
+is only visible at setup-time, when the factory actually gets called.
+
+In order to allow parametrization at collection-time I see two ways:
+
+- allow specifying dependencies in the funcarg-marker 
+- allow funcargs for factories as well 
+



https://bitbucket.org/hpk42/pytest/changeset/19affc442801/
changeset:   19affc442801
user:        hpk42
date:        2012-07-18 19:49:14
summary:     re-introduce the old 2.2.4 FuncargRequest implementation as it is a better
base for implementing the new funcarg/setup api. Also Un-optimize
funcargnames discovery for now.
affected #:  8 files

diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 _pytest/capture.py
--- a/_pytest/capture.py
+++ b/_pytest/capture.py
@@ -187,7 +187,7 @@
     captured output available via ``capsys.readouterr()`` method calls
     which return a ``(out, err)`` tuple.
     """
-    if "capfd" in request.funcargs:
+    if "capfd" in request._funcargs:
         raise request.LookupError(error_capsysfderror)
     return CaptureFuncarg(py.io.StdCapture)
 
@@ -196,7 +196,7 @@
     captured output available via ``capsys.readouterr()`` method calls
     which return a ``(out, err)`` tuple.
     """
-    if "capsys" in request.funcargs:
+    if "capsys" in request._funcargs:
         raise request.LookupError(error_capsysfderror)
     if not hasattr(os, 'dup'):
         pytest.skip("capfd funcarg needs os.dup")


diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 _pytest/impl
--- /dev/null
+++ b/_pytest/impl
@@ -0,0 +1,97 @@
+
+Implementation plan for resources
+------------------------------------------
+
+1. Revert FuncargRequest to the old form, unmerge item/request
+2. make setup functions be discovered at collection time
+3. make funcarg factories be discovered at collection time
+4. Introduce funcarg marker
+5. Introduce funcarg scope parameter
+6. Introduce funcarg parametrize parameter
+7. (Introduce a pytest_fixture_protocol/setup_funcargs hook)
+
+methods and data structures
+--------------------------------
+
+A FuncarcDB holds all information about funcarg definitions,
+parametrization and the places where funcargs are required.  It can
+answer the following questions:
+
+* given a node and a funcargname, return a paramlist so that collection 
+  can perform parametrization (parametrized nodes?)
+* given a node (possibly containing a param), perform a funcargrequest 
+  and return the value
+* if funcargname is an empty string, it matches general setup.
+
+pytest could perform 2-pass collection:
+- first perform normal collection (no parametrization at all!), populate
+  FuncargDB
+- walk through the node tree and ask FuncargDB for each node for
+  required funcargs and their parameters - clone subtrees (deepcopy) and
+  substitute the un-parametrized node with parametrized ones
+
+as a simple example, let's consider a tree where a test function requires
+a "abc" funcarg and its factory defines it as parametrized and scoped
+for Modules.  When the 2nd collection pass asks FuncargDB to return
+params for the test module, it will know that the test functions in it
+requires "abc" and that is it parametrized and defined for module scope.
+Therefore parametrization of the module node is performed, substituting
+the node with multiple module nodes ("test_module.py[1]", ...).
+When test_module.py[1] is setup() it will call all its (parametrized)
+factories and populate a funcargs dictionary, mapping funcargnames to values.
+When a test function below test_module.py[1] is executed, it looks up
+its required arguments from the thus populated funcargs dictionary.
+
+Let's add to this example a second funcarg "def" that has a per-function parametrization.  When the 2nd collection pass asks FuncargDB to return
+params for the test function, it will know that the test functions in it
+requires "def" and that is it parametrized and defined for function scope.
+Therefore parametrization of the function node is performed, substituting
+the node with multiple function nodes ("test_function[1]", ...).
+
+When test_function[1] is setup() it will call all its (parametrized)
+factories and populate a funcargs dictionary.  The "def" will only appear
+in the funcargs dict seen by test_function[1]. When test_function[1] 
+executes, it will use its funcargs.
+
+
+
+
+where
+
+* ``nodeidbase`` is a basestring; for all nodeids matching
+  startswith(nodeidbase) it defines a (scopecls, factorylist) tuple
+* ``scopecls`` is a node class for the which the factorylist s defined
+* ``param`` is a parametrizing parameter for the factorylist
+* ``factorylist`` is a list of factories which will be used to perform
+  a funcarg request
+* the whole list is sorted by length of nodeidbase (longest first)
+
+conftest loading:
+    each funcarg-factory will populate FuncargDefs which keeps references
+    to all definitions the funcarg2 marked function or pytest_funcarg__ 
+
+
+scope can be a string or a nodenames-tuple. 
+
+        scopestring -> list of (funcargname, factorylist)
+
+        nodenames -> (funcargname, list of factories)
+
+It needs to be a list because factories can decorate
+
+For any given node and a required funcarg it is thus
+easy to lookup a list of matching factories.
+
+When a test item is collected, it grows a dictionary 
+(funcargname2factorycalllist).  A factory lookup is performed 
+for each required funcarg.  The resulting factory call is stored 
+with the item.  If a function is parametrized multiple items are 
+created with respective factory calls. Else if a factory is parametrized
+multiple items and calls to the factory function are created as well.
+
+At setup time, an item populates a funcargs mapping, mapping names
+to values.  If a value is funcarg factories are queried for a given item
+test functions and setup functions are put in a class
+which looks up required funcarg factories.
+
+


diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 _pytest/main.py
--- a/_pytest/main.py
+++ b/_pytest/main.py
@@ -177,11 +177,11 @@
         #: fspath sensitive hook proxy used to call pytest hooks
         self.ihook = self.session.gethookproxy(self.fspath)
 
-        self.extrainit()
+        #self.extrainit()
 
-    def extrainit(self):
-        """"extra initialization after Node is initialized.  Implemented
-        by some subclasses. """
+    #def extrainit(self):
+    #    """"extra initialization after Node is initialized.  Implemented
+    #    by some subclasses. """
 
     Module = compatproperty("Module")
     Class = compatproperty("Class")


diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -33,126 +33,6 @@
           name.lower(),)
     return property(get, None, None, doc)
 
-class Request(object):
-    _argprefix = "pytest_funcarg__"
-
-    class LookupError(LookupError):
-        """ error while performing funcarg factory lookup. """
-
-    def extrainit(self):
-        self._name2factory = {}
-        self._currentarg = None
-        self.funcargs = None # later set to a dict from fillfuncargs() or
-                             # from getfuncargvalue().  Setting it to
-                             # None prevents users from performing
-                             # "name in item.funcargs" checks too early.
-
-    @property
-    def _plugins(self):
-        extra = [obj for obj in (self.module, self.instance) if obj]
-        return self.getplugins() + extra
-
-    def _getscopeitem(self, scope):
-        if scope == "function":
-            return self
-        elif scope == "session":
-            return None
-        elif scope == "class":
-            x = self.getparent(pytest.Class)
-            if x is not None:
-                return x
-            scope = "module"
-        if scope == "module":
-            return self.getparent(pytest.Module)
-        raise ValueError("unknown scope %r" %(scope,))
-
-    def getfuncargvalue(self, argname):
-        """ Retrieve a named function argument value.
-
-        This function looks up a matching factory and invokes
-        it to obtain the return value.  The factory receives
-        can itself perform recursive calls to this method,
-        either for using multiple other funcarg values under the hood
-        or to decorate values from other factories matching the same name.
-        """
-        try:
-            return self.funcargs[argname]
-        except KeyError:
-            pass
-        except TypeError:
-            self.funcargs = getattr(self, "_funcargs", {})
-        if argname not in self._name2factory:
-            self._name2factory[argname] = self.config.pluginmanager.listattr(
-                    plugins=self._plugins,
-                    attrname=self._argprefix + str(argname)
-            )
-        #else: we are called recursively
-        if not self._name2factory[argname]:
-            self._raiselookupfailed(argname)
-        funcargfactory = self._name2factory[argname].pop()
-        oldarg = self._currentarg
-        mp = monkeypatch()
-        mp.setattr(self, '_currentarg', argname)
-        try:
-            param = self.callspec.getparam(argname)
-        except (AttributeError, ValueError):
-            pass
-        else:
-            mp.setattr(self, 'param', param, raising=False)
-        try:
-            self.funcargs[argname] = res = funcargfactory(self)
-        finally:
-            mp.undo()
-        return res
-
-    def addfinalizer(self, finalizer):
-        """ add a no-args finalizer function to be called when the underlying
-        node is torn down."""
-        self.session._setupstate.addfinalizer(finalizer, self)
-
-    def cached_setup(self, setup, teardown=None,
-                     scope="module", extrakey=None):
-        """ Return a cached testing resource created by ``setup`` &
-        detroyed by a respective ``teardown(resource)`` call.
-
-        :arg teardown: function receiving a previously setup resource.
-        :arg setup: a no-argument function creating a resource.
-        :arg scope: a string value out of ``function``, ``class``, ``module``
-            or ``session`` indicating the caching lifecycle of the resource.
-        :arg extrakey: added to internal caching key.
-        """
-        if not hasattr(self.config, '_setupcache'):
-            self.config._setupcache = {} # XXX weakref?
-        colitem = self._getscopeitem(scope)
-        cachekey = (self._currentarg, colitem, extrakey)
-        cache = self.config._setupcache
-        try:
-            val = cache[cachekey]
-        except KeyError:
-            val = setup()
-            cache[cachekey] = val
-            if teardown is not None:
-                def finalizer():
-                    del cache[cachekey]
-                    teardown(val)
-                self.session._setupstate.addfinalizer(finalizer, colitem)
-        return val
-
-    def _raiselookupfailed(self, argname):
-        available = []
-        for plugin in self._plugins:
-            for name in vars(plugin):
-                if name.startswith(self._argprefix):
-                    name = name[len(self._argprefix):]
-                    if name not in available:
-                        available.append(name)
-        fspath, lineno, msg = self.reportinfo()
-        msg = "LookupError: no factory found for function argument %r" % (argname,)
-        msg += "\n available funcargs: %s" %(", ".join(available),)
-        msg += "\n use 'py.test --funcargs [testpath]' for help on them."
-        raise self.LookupError(msg)
-
-
 
 def pytest_addoption(parser):
     group = parser.getgroup("general")
@@ -222,6 +102,15 @@
                     funcargs[name] = pyfuncitem.funcargs[name]
             testfunction(**funcargs)
 
+def pytest_pyfunc_call(__multicall__, pyfuncitem):
+    if not __multicall__.execute():
+        testfunction = pyfuncitem.obj
+        if pyfuncitem._isyieldedfunction():
+            testfunction(*pyfuncitem._args)
+        else:
+            funcargs = pyfuncitem.funcargs
+            testfunction(**funcargs)
+
 def pytest_collect_file(path, parent):
     ext = path.ext
     pb = path.purebasename
@@ -267,7 +156,7 @@
     cls = pyobj_property("Class")
     instance = pyobj_property("Instance")
 
-class PyobjMixin(Request, PyobjContext):
+class PyobjMixin(PyobjContext):
     def obj():
         def fget(self):
             try:
@@ -390,14 +279,12 @@
         gentesthook.pcall(plugins, metafunc=metafunc)
         Function = self._getcustomclass("Function")
         if not metafunc._calls:
-            return Function(name, parent=self,
-                            funcargnames=metafunc.funcargnames)
+            return Function(name, parent=self)
         l = []
         for callspec in metafunc._calls:
             subname = "%s[%s]" %(name, callspec.id)
             function = Function(name=subname, parent=self,
                 callspec=callspec, callobj=funcobj,
-                funcargnames=metafunc.funcargnames,
                 keywords={callspec.id:True})
             l.append(function)
         return l
@@ -494,6 +381,7 @@
 class FunctionMixin(PyobjMixin):
     """ mixin for the code common to Function and Generator.
     """
+
     def setup(self):
         """ perform setup for this test function. """
         if hasattr(self, '_preservedparent'):
@@ -535,7 +423,7 @@
             excinfo.traceback = ntraceback.filter()
 
     def _repr_failure_py(self, excinfo, style="long"):
-        if excinfo.errisinstance(Request.LookupError):
+        if excinfo.errisinstance(FuncargRequest.LookupError):
             fspath, lineno, msg = self.reportinfo()
             lines, _ = inspect.getsourcelines(self.obj)
             for i, line in enumerate(lines):
@@ -626,10 +514,21 @@
         return argnames[startindex:-numdefaults]
     return argnames[startindex:]
 
-def fillfuncargs(node):
+def fillfuncargs(function):
     """ fill missing funcargs. """
-    if not isinstance(node, Function):
-        node = OldFuncargRequest(pyfuncitem=node)
+    #if not getattr(function, "_args", None) is not None:
+    #    request = FuncargRequest(pyfuncitem=function)
+    #    request._fillfuncargs()
+    if getattr(function, "_args", None) is None:
+        try:
+            request = function._request
+        except AttributeError:
+            request = FuncargRequest(function)
+        request._fillfuncargs()
+
+def XXXfillfuncargs(node):
+    """ fill missing funcargs. """
+    node = FuncargRequest(node)
     if node.funcargs is None:
         node.funcargs = getattr(node, "_funcargs", {})
     if not isinstance(node, Function) or not node._isyieldedfunction():
@@ -815,8 +714,8 @@
     for plugin in plugins:
         available = []
         for name, factory in vars(plugin).items():
-            if name.startswith(Request._argprefix):
-                name = name[len(Request._argprefix):]
+            if name.startswith(FuncargRequest._argprefix):
+                name = name[len(FuncargRequest._argprefix):]
                 if name not in available:
                     available.append([name, factory])
         if available:
@@ -931,11 +830,9 @@
     """
     _genid = None
     def __init__(self, name, parent=None, args=None, config=None,
-                 callspec=None, callobj=_dummy, keywords=None,
-                 session=None, funcargnames=()):
+                 callspec=None, callobj=_dummy, keywords=None, session=None):
         super(Function, self).__init__(name, parent, config=config,
                                        session=session)
-        self.funcargnames = funcargnames
         self._args = args
         if self._isyieldedfunction():
             assert not callspec, (
@@ -943,12 +840,17 @@
         else:
             if callspec is not None:
                 self.callspec = callspec
-                self._funcargs = callspec.funcargs or {}
+                self.funcargs = callspec.funcargs or {}
                 self._genid = callspec.id
                 if hasattr(callspec, "param"):
                     self.param = callspec.param
+            else:
+                self.funcargs = {}
+            self._request = req = FuncargRequest(self)
         if callobj is not _dummy:
-            self._obj = callobj
+            self.obj = callobj
+        startindex = int(self.cls is not None)
+        self.funcargnames = getfuncargnames(self.obj, startindex=startindex)
 
         self.keywords.update(py.builtin._getfuncdict(self.obj) or {})
         if keywords:
@@ -1002,49 +904,196 @@
         return hash((self.parent, self.name))
 
 
-def itemapi_property(name, set=False):
-    prop = getattr(Function, name, None)
-    doc = getattr(prop, "__doc__", None)
-    def get(self):
-        return getattr(self._pyfuncitem, name)
-    if set:
-        def set(self, value):
-            setattr(self._pyfuncitem, name, value)
-    else:
-        set = None
-    return property(get, set, None, doc)
+class FuncargRequest:
+    """ A request for function arguments from a test function.
 
+        Note that there is an optional ``param`` attribute in case
+        there was an invocation to metafunc.addcall(param=...).
+        If no such call was done in a ``pytest_generate_tests``
+        hook, the attribute will not be present.
+    """
+    _argprefix = "pytest_funcarg__"
+    _argname = None
 
-class OldFuncargRequest(Request, PyobjContext):
-    """ (deprecated) helper interactions with a test function invocation.
+    class LookupError(LookupError):
+        """ error on performing funcarg request. """
 
-    Note that there is an optional ``param`` attribute in case
-    there was an invocation to metafunc.addcall(param=...).
-    If no such call was done in a ``pytest_generate_tests``
-    hook, the attribute will not be present.
-    """
     def __init__(self, pyfuncitem):
         self._pyfuncitem = pyfuncitem
-        Request.extrainit(self)
-        self.funcargs = pyfuncitem.funcargs
-        self.getplugins = self._pyfuncitem.getplugins
-        self.reportinfo = self._pyfuncitem.reportinfo
-        self.getparent = self._pyfuncitem.getparent
+        if hasattr(pyfuncitem, '_requestparam'):
+            self.param = pyfuncitem._requestparam
+        self.getparent = pyfuncitem.getparent
+        self._funcargs  = self._pyfuncitem.funcargs.copy()
+        self._name2factory = {}
+        self._currentarg = None
+
+    @cached_property
+    def _plugins(self):
+        extra = [obj for obj in (self.module, self.instance) if obj]
+        return self._pyfuncitem.getplugins() + extra
+
+    @property
+    def function(self):
+        """ function object of the test invocation. """
+        return self._pyfuncitem.obj
+
+    @property
+    def keywords(self):
+        """ keywords of the test function item.
+
+        .. versionadded:: 2.0
+        """
+        return self._pyfuncitem.keywords
+
+    @property
+    def module(self):
+        """ module where the test function was collected. """
+        return self._pyfuncitem.getparent(pytest.Module).obj
+
+    @property
+    def cls(self):
+        """ class (can be None) where the test function was collected. """
+        clscol = self._pyfuncitem.getparent(pytest.Class)
+        if clscol:
+            return clscol.obj
+    @property
+    def instance(self):
+        """ instance (can be None) on which test function was collected. """
+        return py.builtin._getimself(self.function)
+
+    @property
+    def config(self):
+        """ the pytest config object associated with this request. """
+        return self._pyfuncitem.config
+
+    @property
+    def fspath(self):
+        """ the file system path of the test module which collected this test. """
+        return self._pyfuncitem.fspath
+
+    def _fillfuncargs(self):
+        argnames = getfuncargnames(self.function)
+        if argnames:
+            assert not getattr(self._pyfuncitem, '_args', None), (
+                "yielded functions cannot have funcargs")
+        for argname in argnames:
+            if argname not in self._pyfuncitem.funcargs:
+                self._pyfuncitem.funcargs[argname] = self.getfuncargvalue(argname)
+
+
+    def applymarker(self, marker):
+        """ Apply a marker to a single test function invocation.
+        This method is useful if you don't want to have a keyword/marker
+        on all function invocations.
+
+        :arg marker: a :py:class:`_pytest.mark.MarkDecorator` object
+            created by a call to ``py.test.mark.NAME(...)``.
+        """
+        if not isinstance(marker, py.test.mark.XYZ.__class__):
+            raise ValueError("%r is not a py.test.mark.* object")
+        self._pyfuncitem.keywords[marker.markname] = marker
+
+    def cached_setup(self, setup, teardown=None, scope="module", extrakey=None):
+        """ Return a testing resource managed by ``setup`` &
+        ``teardown`` calls.  ``scope`` and ``extrakey`` determine when the
+        ``teardown`` function will be called so that subsequent calls to
+        ``setup`` would recreate the resource.
+
+        :arg teardown: function receiving a previously setup resource.
+        :arg setup: a no-argument function creating a resource.
+        :arg scope: a string value out of ``function``, ``class``, ``module``
+            or ``session`` indicating the caching lifecycle of the resource.
+        :arg extrakey: added to internal caching key of (funcargname, scope).
+        """
+        if not hasattr(self.config, '_setupcache'):
+            self.config._setupcache = {} # XXX weakref?
+        cachekey = (self._currentarg, self._getscopeitem(scope), extrakey)
+        cache = self.config._setupcache
         try:
-            self.param = self._pyfuncitem.param
-        except AttributeError:
+            val = cache[cachekey]
+        except KeyError:
+            val = setup()
+            cache[cachekey] = val
+            if teardown is not None:
+                def finalizer():
+                    del cache[cachekey]
+                    teardown(val)
+                self._addfinalizer(finalizer, scope=scope)
+        return val
+
+    def getfuncargvalue(self, argname):
+        """ Retrieve a function argument by name for this test
+        function invocation.  This allows one function argument factory
+        to call another function argument factory.  If there are two
+        funcarg factories for the same test function argument the first
+        factory may use ``getfuncargvalue`` to call the second one and
+        do something additional with the resource.
+        """
+        try:
+            return self._funcargs[argname]
+        except KeyError:
             pass
+        if argname not in self._name2factory:
+            self._name2factory[argname] = self.config.pluginmanager.listattr(
+                    plugins=self._plugins,
+                    attrname=self._argprefix + str(argname)
+            )
+        #else: we are called recursively
+        if not self._name2factory[argname]:
+            self._raiselookupfailed(argname)
+        funcargfactory = self._name2factory[argname].pop()
+        oldarg = self._currentarg
+        mp = monkeypatch()
+        mp.setattr(self, '_currentarg', argname)
+        try:
+            param = self._pyfuncitem.callspec.getparam(argname)
+        except (AttributeError, ValueError):
+            pass
+        else:
+            mp.setattr(self, 'param', param, raising=False)
+        try:
+            self._funcargs[argname] = res = funcargfactory(request=self)
+        finally:
+            mp.undo()
+        return res
+
+    def _getscopeitem(self, scope):
+        if scope == "function":
+            return self._pyfuncitem
+        elif scope == "session":
+            return None
+        elif scope == "class":
+            x = self._pyfuncitem.getparent(pytest.Class)
+            if x is not None:
+                return x
+            scope = "module"
+        if scope == "module":
+            return self._pyfuncitem.getparent(pytest.Module)
+        raise ValueError("unknown finalization scope %r" %(scope,))
+
+    def addfinalizer(self, finalizer):
+        """add finalizer function to be called after test function
+        finished execution. """
+        self._addfinalizer(finalizer, scope="function")
+
+    def _addfinalizer(self, finalizer, scope):
+        colitem = self._getscopeitem(scope)
+        self._pyfuncitem.session._setupstate.addfinalizer(
+            finalizer=finalizer, colitem=colitem)
 
     def __repr__(self):
-        return "<OldFuncargRequest for %r>" % (self._pyfuncitem.name)
+        return "<FuncargRequest for %r>" %(self._pyfuncitem)
 
-    _getscopeitem = itemapi_property("_getscopeitem")
-    funcargs = itemapi_property("funcargs", set=True)
-    keywords = itemapi_property("keywords")
-    config   = itemapi_property("config")
-    session  = itemapi_property("session")
-    fspath   = itemapi_property("fspath")
-    applymarker = itemapi_property("applymarker")
-    @property
-    def function(self):
-        return self._pyfuncitem.obj
+    def _raiselookupfailed(self, argname):
+        available = []
+        for plugin in self._plugins:
+            for name in vars(plugin):
+                if name.startswith(self._argprefix):
+                    name = name[len(self._argprefix):]
+                    if name not in available:
+                        available.append(name)
+        fspath, lineno, msg = self._pyfuncitem.reportinfo()
+        msg = "LookupError: no factory found for function argument %r" % (argname,)
+        msg += "\n available funcargs: %s" %(", ".join(available),)
+        msg += "\n use 'py.test --funcargs [testpath]' for help on them."
+        raise self.LookupError(msg)


diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 _pytest/tmpdir.py
--- a/_pytest/tmpdir.py
+++ b/_pytest/tmpdir.py
@@ -54,15 +54,15 @@
     mp.setattr(config, '_tmpdirhandler', t, raising=False)
     mp.setattr(pytest, 'ensuretemp', t.ensuretemp, raising=False)
 
-def pytest_funcarg__tmpdir(item):
+def pytest_funcarg__tmpdir(request):
     """return a temporary directory path object
     which is unique to each test function invocation,
     created as a sub directory of the base temporary
     directory.  The returned object is a `py.path.local`_
     path object.
     """
-    name = item.name
+    name = request._pyfuncitem.name
     name = py.std.re.sub("[\W]", "_", name)
-    x = item.config._tmpdirhandler.mktemp(name, numbered=True)
+    x = request.config._tmpdirhandler.mktemp(name, numbered=True)
     return x
 


diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 doc/en/funcargs.txt
--- a/doc/en/funcargs.txt
+++ b/doc/en/funcargs.txt
@@ -110,7 +110,7 @@
 The request object passed to factories
 -----------------------------------------
 
-Each funcarg factory receives a :py:class:`~_pytest.python.Request` object which
+Each funcarg factory receives a :py:class:`~_pytest.python.FuncargRequest` object which
 provides methods to manage caching and finalization in the context of the
 test invocation as well as several attributes of the the underlying test item.  In fact, as of version pytest-2.3, the request API is implemented on all Item 
 objects and therefore the request object has general :py:class:`Node attributes and methods <_pytest.main.Node>` attributes.  This is a backward compatible 


diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 doc/en/plugins.txt
--- a/doc/en/plugins.txt
+++ b/doc/en/plugins.txt
@@ -331,7 +331,7 @@
 Reference of objects involved in hooks
 ===========================================================
 
-.. autoclass:: _pytest.python.Request()
+.. autoclass:: _pytest.python.FuncargRequest()
     :members:
 
 .. autoclass:: _pytest.config.Config()


diff -r ffe816408bb9e8bd4dc31b5d181b8e6984f7a5c0 -r 19affc442801ad53c603e3d5ac912539dc1a6533 testing/test_python.py
--- a/testing/test_python.py
+++ b/testing/test_python.py
@@ -277,14 +277,18 @@
     def test_function_equality(self, testdir, tmpdir):
         config = testdir.parseconfigure()
         session = testdir.Session(config)
+        def func1():
+            pass
+        def func2():
+            pass
         f1 = pytest.Function(name="name", config=config,
-                args=(1,), callobj=isinstance, session=session)
+                args=(1,), callobj=func1, session=session)
         f2 = pytest.Function(name="name",config=config,
-                args=(1,), callobj=py.builtin.callable, session=session)
+                args=(1,), callobj=func2, session=session)
         assert not f1 == f2
         assert f1 != f2
         f3 = pytest.Function(name="name", config=config,
-                args=(1,2), callobj=py.builtin.callable, session=session)
+                args=(1,2), callobj=func2, session=session)
         assert not f3 == f2
         assert f3 != f2
 
@@ -292,7 +296,7 @@
         assert f3 != f1
 
         f1_b = pytest.Function(name="name", config=config,
-              args=(1,), callobj=isinstance, session=session)
+              args=(1,), callobj=func1, session=session)
         assert f1 == f1_b
         assert not f1 != f1_b
 
@@ -307,10 +311,12 @@
             funcargs = {}
             id = "world"
         session = testdir.Session(config)
+        def func():
+            pass
         f5 = pytest.Function(name="name", config=config,
-            callspec=callspec1, callobj=isinstance, session=session)
+            callspec=callspec1, callobj=func, session=session)
         f5b = pytest.Function(name="name", config=config,
-            callspec=callspec2, callobj=isinstance, session=session)
+            callspec=callspec2, callobj=func, session=session)
         assert f5 != f5b
         assert not (f5 == f5b)
 
@@ -549,7 +555,7 @@
                 return 42
         """)
         item = testdir.getitem("def test_func(some): pass")
-        exc = pytest.raises(funcargs.OldFuncargRequest.LookupError,
+        exc = pytest.raises(funcargs.FuncargRequest.LookupError,
             "funcargs.fillfuncargs(item)")
         s = str(exc.value)
         assert s.find("xyzsomething") != -1
@@ -624,7 +630,7 @@
             def pytest_funcarg__something(request): pass
             def test_func(something): pass
         """)
-        req = funcargs.OldFuncargRequest(item)
+        req = funcargs.FuncargRequest(item)
         assert req.function == item.obj
         assert req.keywords is item.keywords
         assert hasattr(req.module, 'test_func')
@@ -639,7 +645,7 @@
                 def test_func(self, something):
                     pass
         """)
-        req = funcargs.OldFuncargRequest(item)
+        req = funcargs.FuncargRequest(item)
         assert req.cls.__name__ == "TestB"
         assert req.instance.__class__ == req.cls
 
@@ -653,7 +659,7 @@
         """)
         item1, = testdir.genitems([modcol])
         assert item1.name == "test_method"
-        name2factory = funcargs.OldFuncargRequest(item1)._name2factory
+        name2factory = funcargs.FuncargRequest(item1)._name2factory
         assert len(name2factory) == 1
         assert name2factory[0].__name__ == "pytest_funcarg__something"
 
@@ -668,7 +674,7 @@
             def test_func(something):
                 assert something == 2
         """)
-        req = funcargs.OldFuncargRequest(item)
+        req = funcargs.FuncargRequest(item)
         val = req.getfuncargvalue("something")
         assert val == 2
 
@@ -680,7 +686,7 @@
                 return l.pop()
             def test_func(something): pass
         """)
-        req = funcargs.OldFuncargRequest(item)
+        req = funcargs.FuncargRequest(item)
         pytest.raises(req.LookupError, req.getfuncargvalue, "notexists")
         val = req.getfuncargvalue("something")
         assert val == 1
@@ -691,7 +697,8 @@
         val2 = req.getfuncargvalue("other")  # see about caching
         assert val2 == 2
         pytest._fillfuncargs(item)
-        assert item.funcargs == {'something': 1, "other": 2}
+        assert item.funcargs == {'something': 1}
+        #assert item.funcargs == {'something': 1, "other": 2}
 
     def test_request_addfinalizer(self, testdir):
         item = testdir.getitem("""
@@ -728,7 +735,7 @@
     def test_request_getmodulepath(self, testdir):
         modcol = testdir.getmodulecol("def test_somefunc(): pass")
         item, = testdir.genitems([modcol])
-        req = funcargs.OldFuncargRequest(item)
+        req = funcargs.FuncargRequest(item)
         assert req.fspath == modcol.fspath
 
 def test_applymarker(testdir):
@@ -739,7 +746,7 @@
             def test_func2(self, something):
                 pass
     """)
-    req1 = funcargs.OldFuncargRequest(item1)
+    req1 = funcargs.FuncargRequest(item1)
     assert 'xfail' not in item1.keywords
     req1.applymarker(pytest.mark.xfail)
     assert 'xfail' in item1.keywords
@@ -757,7 +764,7 @@
                 def test_func2(self, something):
                     pass
         """)
-        req1 = funcargs.OldFuncargRequest(item1)
+        req1 = funcargs.FuncargRequest(item1)
         l = ["hello"]
         def setup():
             return l.pop()
@@ -766,7 +773,7 @@
         assert ret1 == "hello"
         ret1b = req1.cached_setup(setup)
         assert ret1 == ret1b
-        req2 = funcargs.OldFuncargRequest(item2)
+        req2 = funcargs.FuncargRequest(item2)
         ret2 = req2.cached_setup(setup)
         assert ret2 == ret1
 
@@ -782,7 +789,7 @@
                 def test_func2b(self, something):
                     pass
         """)
-        req1 = funcargs.OldFuncargRequest(item2)
+        req1 = funcargs.FuncargRequest(item2)
         l = ["hello2", "hello"]
         def setup():
             return l.pop()
@@ -791,22 +798,22 @@
         # automatically turn "class" to "module" scope
         ret1 = req1.cached_setup(setup, scope="class")
         assert ret1 == "hello"
-        req2 = funcargs.OldFuncargRequest(item2)
+        req2 = funcargs.FuncargRequest(item2)
         ret2 = req2.cached_setup(setup, scope="class")
         assert ret2 == "hello"
 
-        req3 = funcargs.OldFuncargRequest(item3)
+        req3 = funcargs.FuncargRequest(item3)
         ret3a = req3.cached_setup(setup, scope="class")
         ret3b = req3.cached_setup(setup, scope="class")
         assert ret3a == "hello2"
         assert ret3b == "hello2"
-        req4 = funcargs.OldFuncargRequest(item4)
+        req4 = funcargs.FuncargRequest(item4)
         ret4 = req4.cached_setup(setup, scope="class")
         assert ret4 == ret3a
 
     def test_request_cachedsetup_extrakey(self, testdir):
         item1 = testdir.getitem("def test_func(): pass")
-        req1 = funcargs.OldFuncargRequest(item1)
+        req1 = funcargs.FuncargRequest(item1)
         l = ["hello", "world"]
         def setup():
             return l.pop()
@@ -821,7 +828,7 @@
 
     def test_request_cachedsetup_cache_deletion(self, testdir):
         item1 = testdir.getitem("def test_func(): pass")
-        req1 = funcargs.OldFuncargRequest(item1)
+        req1 = funcargs.FuncargRequest(item1)
         l = []
         def setup():
             l.append("setup")
@@ -1093,9 +1100,9 @@
             def pytest_generate_tests(metafunc):
                 metafunc.addcall(param=metafunc)
 
-            def pytest_funcarg__metafunc(item):
-                assert item._genid == "0"
-                return item.param
+            def pytest_funcarg__metafunc(request):
+                assert request._pyfuncitem._genid == "0"
+                return request.param
 
             def test_function(metafunc, pytestconfig):
                 assert metafunc.config == pytestconfig
@@ -1591,6 +1598,7 @@
     ])
 
 class TestRequestAPI:
+    @pytest.mark.xfail(reason="reverted refactoring")
     def test_addfinalizer_cachedsetup_getfuncargvalue(self, testdir):
         testdir.makeconftest("""
             l = []
@@ -1615,10 +1623,11 @@
             "*2 passed*",
         ])
 
+    @pytest.mark.xfail(reason="consider item's funcarg access and error conditions")
     def test_runtest_setup_sees_filled_funcargs(self, testdir):
         testdir.makeconftest("""
             def pytest_runtest_setup(item):
-                assert item.funcargs is None
+                assert not hasattr(item, "_request")
         """)
         testdir.makepyfile("""
             def pytest_funcarg__a(request):



https://bitbucket.org/hpk42/pytest/changeset/ab0f23204d9f/
changeset:   ab0f23204d9f
user:        hpk42
date:        2012-07-19 09:20:14
summary:     move funcarg factory to a new FuncargManager object at session level
affected #:  7 files

diff -r 19affc442801ad53c603e3d5ac912539dc1a6533 -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e _pytest/capture.py
--- a/_pytest/capture.py
+++ b/_pytest/capture.py
@@ -188,7 +188,7 @@
     which return a ``(out, err)`` tuple.
     """
     if "capfd" in request._funcargs:
-        raise request.LookupError(error_capsysfderror)
+        raise request.raiseerror(error_capsysfderror)
     return CaptureFuncarg(py.io.StdCapture)
 
 def pytest_funcarg__capfd(request):
@@ -197,7 +197,7 @@
     which return a ``(out, err)`` tuple.
     """
     if "capsys" in request._funcargs:
-        raise request.LookupError(error_capsysfderror)
+        request.raiseerror(error_capsysfderror)
     if not hasattr(os, 'dup'):
         pytest.skip("capfd funcarg needs os.dup")
     return CaptureFuncarg(py.io.StdCaptureFD)


diff -r 19affc442801ad53c603e3d5ac912539dc1a6533 -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e _pytest/impl
--- a/_pytest/impl
+++ b/_pytest/impl
@@ -3,84 +3,32 @@
 ------------------------------------------
 
 1. Revert FuncargRequest to the old form, unmerge item/request
-2. make setup functions be discovered at collection time
-3. make funcarg factories be discovered at collection time
-4. Introduce funcarg marker
-5. Introduce funcarg scope parameter
-6. Introduce funcarg parametrize parameter
+   (done)
+2. make funcarg factories be discovered at collection time
+3. Introduce funcarg marker
+4. Introduce funcarg scope parameter
+5. Introduce funcarg parametrize parameter
+6. make setup functions be discovered at collection time
 7. (Introduce a pytest_fixture_protocol/setup_funcargs hook)
 
 methods and data structures
 --------------------------------
 
-A FuncarcDB holds all information about funcarg definitions,
-parametrization and the places where funcargs are required.  It can
-answer the following questions:
-
-* given a node and a funcargname, return a paramlist so that collection 
-  can perform parametrization (parametrized nodes?)
-* given a node (possibly containing a param), perform a funcargrequest 
-  and return the value
-* if funcargname is an empty string, it matches general setup.
-
-pytest could perform 2-pass collection:
-- first perform normal collection (no parametrization at all!), populate
-  FuncargDB
-- walk through the node tree and ask FuncargDB for each node for
-  required funcargs and their parameters - clone subtrees (deepcopy) and
-  substitute the un-parametrized node with parametrized ones
+A FuncarcManager holds all information about funcarg definitions
+including parametrization and scope definitions.  It implements
+a pytest_generate_tests hook which performs parametrization as appropriate.
 
 as a simple example, let's consider a tree where a test function requires
 a "abc" funcarg and its factory defines it as parametrized and scoped
-for Modules.  When the 2nd collection pass asks FuncargDB to return
-params for the test module, it will know that the test functions in it
-requires "abc" and that is it parametrized and defined for module scope.
-Therefore parametrization of the module node is performed, substituting
-the node with multiple module nodes ("test_module.py[1]", ...).
-When test_module.py[1] is setup() it will call all its (parametrized)
-factories and populate a funcargs dictionary, mapping funcargnames to values.
-When a test function below test_module.py[1] is executed, it looks up
-its required arguments from the thus populated funcargs dictionary.
-
-Let's add to this example a second funcarg "def" that has a per-function parametrization.  When the 2nd collection pass asks FuncargDB to return
-params for the test function, it will know that the test functions in it
-requires "def" and that is it parametrized and defined for function scope.
-Therefore parametrization of the function node is performed, substituting
-the node with multiple function nodes ("test_function[1]", ...).
-
-When test_function[1] is setup() it will call all its (parametrized)
-factories and populate a funcargs dictionary.  The "def" will only appear
-in the funcargs dict seen by test_function[1]. When test_function[1] 
-executes, it will use its funcargs.
-
-
-
-
-where
-
-* ``nodeidbase`` is a basestring; for all nodeids matching
-  startswith(nodeidbase) it defines a (scopecls, factorylist) tuple
-* ``scopecls`` is a node class for the which the factorylist s defined
-* ``param`` is a parametrizing parameter for the factorylist
-* ``factorylist`` is a list of factories which will be used to perform
-  a funcarg request
-* the whole list is sorted by length of nodeidbase (longest first)
+for Modules.  When collections hits the function item, it creates
+the metafunc object, and calls funcargdb.pytest_generate_tests(metafunc)
+which looks up available funcarg factories and their scope and parametrization.
+This information is equivalent to what can be provided today directly
+at the function site and it should thus be relatively straight forward
+to implement the additional way of defining parametrization/scoping.
 
 conftest loading:
-    each funcarg-factory will populate FuncargDefs which keeps references
-    to all definitions the funcarg2 marked function or pytest_funcarg__ 
-
-
-scope can be a string or a nodenames-tuple. 
-
-        scopestring -> list of (funcargname, factorylist)
-
-        nodenames -> (funcargname, list of factories)
-
-It needs to be a list because factories can decorate
-
-For any given node and a required funcarg it is thus
-easy to lookup a list of matching factories.
+    each funcarg-factory will populate the session.funcargmanager
 
 When a test item is collected, it grows a dictionary 
 (funcargname2factorycalllist).  A factory lookup is performed 


diff -r 19affc442801ad53c603e3d5ac912539dc1a6533 -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e _pytest/main.py
--- a/_pytest/main.py
+++ b/_pytest/main.py
@@ -2,8 +2,12 @@
 
 import py
 import pytest, _pytest
+import inspect
 import os, sys, imp
 
+from _pytest.monkeypatch import monkeypatch
+from py._code.code import TerminalRepr
+
 tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
 
 # exitcodes for the command line
@@ -279,6 +283,15 @@
         pass
 
     def _repr_failure_py(self, excinfo, style=None):
+        LE = self.session.funcargmanager.FuncargLookupError
+        if excinfo.errisinstance(LE):
+            request = excinfo.value.request
+            fspath, lineno, msg = request._pyfuncitem.reportinfo()
+            lines, _ = inspect.getsourcelines(request.function)
+            for i, line in enumerate(lines):
+                if line.strip().startswith('def'):
+                    return FuncargLookupErrorRepr(fspath, lineno, lines[:i+1],
+                                str(excinfo.value.msg))
         if self.config.option.fulltrace:
             style="long"
         else:
@@ -391,6 +404,75 @@
             self._location = location
             return location
 
+class FuncargLookupError(LookupError):
+    """ could not find a factory. """
+    def __init__(self, request, msg):
+        self.request = request
+        self.msg = msg
+
+class FuncargManager:
+    _argprefix = "pytest_funcarg__"
+    FuncargLookupError = FuncargLookupError
+
+    def __init__(self, session):
+        self.session = session
+        self.config = session.config
+        self.node2name2factory = {}
+
+    def _discoverfactories(self, request, argname):
+        node = request._pyfuncitem
+        name2factory = self.node2name2factory.setdefault(node, {})
+        if argname not in name2factory:
+            name2factory[argname] = self.config.pluginmanager.listattr(
+                    plugins=request._plugins,
+                    attrname=self._argprefix + str(argname)
+            )
+        #else: we are called recursively
+        if not name2factory[argname]:
+            self._raiselookupfailed(request, argname)
+
+    def _getfuncarg(self, request, argname):
+        node = request._pyfuncitem
+        try:
+            factorylist = self.node2name2factory[node][argname]
+        except KeyError:
+            # XXX at collection time this funcarg was not know to be a
+            # requirement, would be better if it would be known
+            self._discoverfactories(request, argname)
+            factorylist = self.node2name2factory[node][argname]
+
+        if not factorylist:
+            self._raiselookupfailed(request, argname)
+        funcargfactory = factorylist.pop()
+        oldarg = request._currentarg
+        mp = monkeypatch()
+        mp.setattr(request, '_currentarg', argname)
+        try:
+            param = node.callspec.getparam(argname)
+        except (AttributeError, ValueError):
+            pass
+        else:
+            mp.setattr(request, 'param', param, raising=False)
+        try:
+            return funcargfactory(request=request)
+        finally:
+            mp.undo()
+
+    def _raiselookupfailed(self, request, argname):
+        available = []
+        for plugin in request._plugins:
+            for name in vars(plugin):
+                if name.startswith(self._argprefix):
+                    name = name[len(self._argprefix):]
+                    if name not in available:
+                        available.append(name)
+        fspath, lineno, msg = request._pyfuncitem.reportinfo()
+        msg = "LookupError: no factory found for argument %r" % (argname,)
+        msg += "\n available funcargs: %s" %(", ".join(available),)
+        msg += "\n use 'py.test --funcargs [testpath]' for help on them."
+        raise FuncargLookupError(request, msg)
+
+
 class NoMatch(Exception):
     """ raised if matching cannot locate a matching names. """
 
@@ -407,6 +489,7 @@
         self.shouldstop = False
         self.trace = config.trace.root.get("collection")
         self._norecursepatterns = config.getini("norecursedirs")
+        self.funcargmanager = FuncargManager(self)
 
     def pytest_collectstart(self):
         if self.shouldstop:
@@ -634,4 +717,18 @@
 
 
 
+class FuncargLookupErrorRepr(TerminalRepr):
+    def __init__(self, filename, firstlineno, deflines, errorstring):
+        self.deflines = deflines
+        self.errorstring = errorstring
+        self.filename = filename
+        self.firstlineno = firstlineno
 
+    def toterminal(self, tw):
+        tw.line()
+        for line in self.deflines:
+            tw.line("    " + line.strip())
+        for line in self.errorstring.split("\n"):
+            tw.line("        " + line.strip(), red=True)
+        tw.line()
+        tw.line("%s:%d" % (self.filename, self.firstlineno+1))


diff -r 19affc442801ad53c603e3d5ac912539dc1a6533 -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -3,8 +3,6 @@
 import inspect
 import sys
 import pytest
-from py._code.code import TerminalRepr
-from _pytest.monkeypatch import monkeypatch
 
 import _pytest
 cutdir = py.path.local(_pytest.__file__).dirpath()
@@ -278,9 +276,9 @@
         plugins = self.getplugins() + extra
         gentesthook.pcall(plugins, metafunc=metafunc)
         Function = self._getcustomclass("Function")
+        l = []
         if not metafunc._calls:
-            return Function(name, parent=self)
-        l = []
+            l.append(Function(name, parent=self))
         for callspec in metafunc._calls:
             subname = "%s[%s]" %(name, callspec.id)
             function = Function(name=subname, parent=self,
@@ -423,13 +421,6 @@
             excinfo.traceback = ntraceback.filter()
 
     def _repr_failure_py(self, excinfo, style="long"):
-        if excinfo.errisinstance(FuncargRequest.LookupError):
-            fspath, lineno, msg = self.reportinfo()
-            lines, _ = inspect.getsourcelines(self.obj)
-            for i, line in enumerate(lines):
-                if line.strip().startswith('def'):
-                    return FuncargLookupErrorRepr(fspath, lineno,
-            lines[:i+1], str(excinfo.value))
         if excinfo.errisinstance(pytest.fail.Exception):
             if not excinfo.value.pytrace:
                 return str(excinfo.value)
@@ -441,22 +432,6 @@
         return self._repr_failure_py(excinfo,
             style=self.config.option.tbstyle)
 
-class FuncargLookupErrorRepr(TerminalRepr):
-    def __init__(self, filename, firstlineno, deflines, errorstring):
-        self.deflines = deflines
-        self.errorstring = errorstring
-        self.filename = filename
-        self.firstlineno = firstlineno
-
-    def toterminal(self, tw):
-        tw.line()
-        for line in self.deflines:
-            tw.line("    " + line.strip())
-        for line in self.errorstring.split("\n"):
-            tw.line("        " + line.strip(), red=True)
-        tw.line()
-        tw.line("%s:%d" % (self.filename, self.firstlineno+1))
-
 
 class Generator(FunctionMixin, PyCollector):
     def collect(self):
@@ -523,23 +498,9 @@
         try:
             request = function._request
         except AttributeError:
-            request = FuncargRequest(function)
+            request = function._request = FuncargRequest(function)
         request._fillfuncargs()
 
-def XXXfillfuncargs(node):
-    """ fill missing funcargs. """
-    node = FuncargRequest(node)
-    if node.funcargs is None:
-        node.funcargs = getattr(node, "_funcargs", {})
-    if not isinstance(node, Function) or not node._isyieldedfunction():
-        try:
-            funcargnames = node.funcargnames
-        except AttributeError:
-            funcargnames = getfuncargnames(node.function)
-        if funcargnames:
-            for argname in funcargnames:
-                node.getfuncargvalue(argname)
-
 _notexists = object()
 
 class CallSpec2(object):
@@ -711,11 +672,12 @@
     curdir = py.path.local()
     tw = py.io.TerminalWriter()
     verbose = config.getvalue("verbose")
+    argprefix = session.funcargmanager._argprefix
     for plugin in plugins:
         available = []
         for name, factory in vars(plugin).items():
-            if name.startswith(FuncargRequest._argprefix):
-                name = name[len(FuncargRequest._argprefix):]
+            if name.startswith(argprefix):
+                name = name[len(argprefix):]
                 if name not in available:
                     available.append([name, factory])
         if available:
@@ -847,11 +809,11 @@
             else:
                 self.funcargs = {}
             self._request = req = FuncargRequest(self)
+            req._discoverfactories()
         if callobj is not _dummy:
             self.obj = callobj
         startindex = int(self.cls is not None)
         self.funcargnames = getfuncargnames(self.obj, startindex=startindex)
-
         self.keywords.update(py.builtin._getfuncdict(self.obj) or {})
         if keywords:
             self.keywords.update(keywords)
@@ -912,11 +874,6 @@
         If no such call was done in a ``pytest_generate_tests``
         hook, the attribute will not be present.
     """
-    _argprefix = "pytest_funcarg__"
-    _argname = None
-
-    class LookupError(LookupError):
-        """ error on performing funcarg request. """
 
     def __init__(self, pyfuncitem):
         self._pyfuncitem = pyfuncitem
@@ -925,13 +882,24 @@
         self.getparent = pyfuncitem.getparent
         self._funcargs  = self._pyfuncitem.funcargs.copy()
         self._name2factory = {}
+        self.funcargmanager = pyfuncitem.session.funcargmanager
         self._currentarg = None
+        self.funcargnames = getfuncargnames(self.function)
+
+    def _discoverfactories(self):
+        for argname in self.funcargnames:
+            if argname not in self._funcargs:
+                self.funcargmanager._discoverfactories(self, argname)
 
     @cached_property
     def _plugins(self):
         extra = [obj for obj in (self.module, self.instance) if obj]
         return self._pyfuncitem.getplugins() + extra
 
+    def raiseerror(self, msg):
+        """ raise a FuncargLookupError with the given message. """
+        raise self.funcargmanager.FuncargLookupError(self, msg)
+
     @property
     def function(self):
         """ function object of the test invocation. """
@@ -972,14 +940,13 @@
         return self._pyfuncitem.fspath
 
     def _fillfuncargs(self):
-        argnames = getfuncargnames(self.function)
-        if argnames:
+        if self.funcargnames:
             assert not getattr(self._pyfuncitem, '_args', None), (
                 "yielded functions cannot have funcargs")
-        for argname in argnames:
+        for argname in self.funcargnames:
             if argname not in self._pyfuncitem.funcargs:
-                self._pyfuncitem.funcargs[argname] = self.getfuncargvalue(argname)
-
+                self._pyfuncitem.funcargs[argname] = \
+                        self.getfuncargvalue(argname)
 
     def applymarker(self, marker):
         """ Apply a marker to a single test function invocation.
@@ -1021,6 +988,7 @@
                 self._addfinalizer(finalizer, scope=scope)
         return val
 
+
     def getfuncargvalue(self, argname):
         """ Retrieve a function argument by name for this test
         function invocation.  This allows one function argument factory
@@ -1033,29 +1001,9 @@
             return self._funcargs[argname]
         except KeyError:
             pass
-        if argname not in self._name2factory:
-            self._name2factory[argname] = self.config.pluginmanager.listattr(
-                    plugins=self._plugins,
-                    attrname=self._argprefix + str(argname)
-            )
-        #else: we are called recursively
-        if not self._name2factory[argname]:
-            self._raiselookupfailed(argname)
-        funcargfactory = self._name2factory[argname].pop()
-        oldarg = self._currentarg
-        mp = monkeypatch()
-        mp.setattr(self, '_currentarg', argname)
-        try:
-            param = self._pyfuncitem.callspec.getparam(argname)
-        except (AttributeError, ValueError):
-            pass
-        else:
-            mp.setattr(self, 'param', param, raising=False)
-        try:
-            self._funcargs[argname] = res = funcargfactory(request=self)
-        finally:
-            mp.undo()
-        return res
+        val = self.funcargmanager._getfuncarg(self, argname)
+        self._funcargs[argname] = val
+        return val
 
     def _getscopeitem(self, scope):
         if scope == "function":
@@ -1084,16 +1032,3 @@
     def __repr__(self):
         return "<FuncargRequest for %r>" %(self._pyfuncitem)
 
-    def _raiselookupfailed(self, argname):
-        available = []
-        for plugin in self._plugins:
-            for name in vars(plugin):
-                if name.startswith(self._argprefix):
-                    name = name[len(self._argprefix):]
-                    if name not in available:
-                        available.append(name)
-        fspath, lineno, msg = self._pyfuncitem.reportinfo()
-        msg = "LookupError: no factory found for function argument %r" % (argname,)
-        msg += "\n available funcargs: %s" %(", ".join(available),)
-        msg += "\n use 'py.test --funcargs [testpath]' for help on them."
-        raise self.LookupError(msg)


diff -r 19affc442801ad53c603e3d5ac912539dc1a6533 -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e testing/test_mark.py
--- a/testing/test_mark.py
+++ b/testing/test_mark.py
@@ -272,7 +272,7 @@
             import pytest
             @pytest.mark.hello("pos1", z=4)
             @pytest.mark.hello("pos0", z=3)
-            def test_func(self):
+            def test_func():
                 pass
         """)
         items, rec = testdir.inline_genitems(p)


diff -r 19affc442801ad53c603e3d5ac912539dc1a6533 -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e testing/test_python.py
--- a/testing/test_python.py
+++ b/testing/test_python.py
@@ -1,5 +1,6 @@
 import pytest, py, sys
 from _pytest import python as funcargs
+from _pytest.main import FuncargLookupError
 
 class TestModule:
     def test_failing_import(self, testdir):
@@ -301,24 +302,14 @@
         assert not f1 != f1_b
 
     def test_function_equality_with_callspec(self, testdir, tmpdir):
-        config = testdir.parseconfigure()
-        class callspec1:
-            param = 1
-            funcargs = {}
-            id = "hello"
-        class callspec2:
-            param = 1
-            funcargs = {}
-            id = "world"
-        session = testdir.Session(config)
-        def func():
-            pass
-        f5 = pytest.Function(name="name", config=config,
-            callspec=callspec1, callobj=func, session=session)
-        f5b = pytest.Function(name="name", config=config,
-            callspec=callspec2, callobj=func, session=session)
-        assert f5 != f5b
-        assert not (f5 == f5b)
+        items = testdir.getitems("""
+            import pytest
+            @pytest.mark.parametrize('arg', [1,2])
+            def test_function(arg):
+                pass
+        """)
+        assert items[0] != items[1]
+        assert not (items[0] == items[1])
 
     def test_pyfunc_call(self, testdir):
         item = testdir.getitem("def test_func(): raise ValueError")
@@ -550,33 +541,30 @@
         assert pytest._fillfuncargs == funcargs.fillfuncargs
 
     def test_funcarg_lookupfails(self, testdir):
-        testdir.makeconftest("""
+        testdir.makepyfile("""
             def pytest_funcarg__xyzsomething(request):
                 return 42
+
+            def test_func(some):
+                pass
         """)
-        item = testdir.getitem("def test_func(some): pass")
-        exc = pytest.raises(funcargs.FuncargRequest.LookupError,
-            "funcargs.fillfuncargs(item)")
-        s = str(exc.value)
-        assert s.find("xyzsomething") != -1
-
-    def test_funcarg_lookup_default(self, testdir):
-        item = testdir.getitem("def test_func(some, other=42): pass")
-        class Provider:
-            def pytest_funcarg__some(self, request):
-                return request.function.__name__
-        item.config.pluginmanager.register(Provider())
-        funcargs.fillfuncargs(item)
-        assert len(item.funcargs) == 1
+        result = testdir.runpytest() # "--collectonly")
+        assert result.ret != 0
+        result.stdout.fnmatch_lines([
+            "*def test_func(some)*",
+            "*LookupError*",
+            "*xyzsomething*",
+        ])
 
     def test_funcarg_basic(self, testdir):
-        item = testdir.getitem("def test_func(some, other): pass")
-        class Provider:
-            def pytest_funcarg__some(self, request):
+        item = testdir.getitem("""
+            def pytest_funcarg__some(request):
                 return request.function.__name__
-            def pytest_funcarg__other(self, request):
+            def pytest_funcarg__other(request):
                 return 42
-        item.config.pluginmanager.register(Provider())
+            def test_func(some, other):
+                pass
+        """)
         funcargs.fillfuncargs(item)
         assert len(item.funcargs) == 2
         assert item.funcargs['some'] == "test_func"
@@ -612,17 +600,6 @@
             "*1 passed*"
         ])
 
-    def test_fillfuncargs_exposed(self, testdir):
-        item = testdir.getitem("def test_func(some, other=42): pass")
-        class Provider:
-            def pytest_funcarg__some(self, request):
-                return request.function.__name__
-        item.config.pluginmanager.register(Provider())
-        if hasattr(item, '_args'):
-            del item._args
-        from _pytest.python import fillfuncargs
-        fillfuncargs(item)
-        assert len(item.funcargs) == 1
 
 class TestRequest:
     def test_request_attributes(self, testdir):
@@ -642,10 +619,12 @@
     def test_request_attributes_method(self, testdir):
         item, = testdir.getitems("""
             class TestB:
+                def pytest_funcarg__something(request):
+                    return 1
                 def test_func(self, something):
                     pass
         """)
-        req = funcargs.FuncargRequest(item)
+        req = item._request
         assert req.cls.__name__ == "TestB"
         assert req.instance.__class__ == req.cls
 
@@ -686,8 +665,8 @@
                 return l.pop()
             def test_func(something): pass
         """)
-        req = funcargs.FuncargRequest(item)
-        pytest.raises(req.LookupError, req.getfuncargvalue, "notexists")
+        req = item._request
+        pytest.raises(FuncargLookupError, req.getfuncargvalue, "notexists")
         val = req.getfuncargvalue("something")
         assert val == 1
         val = req.getfuncargvalue("something")
@@ -729,7 +708,7 @@
         """)
         result = testdir.runpytest(p)
         result.stdout.fnmatch_lines([
-            "*1 passed*1 error*"
+            "*1 error*"  # XXX the whole module collection fails
             ])
 
     def test_request_getmodulepath(self, testdir):
@@ -740,6 +719,8 @@
 
 def test_applymarker(testdir):
     item1,item2 = testdir.getitems("""
+        def pytest_funcarg__something(request):
+            pass
         class TestClass:
             def test_func1(self, something):
                 pass
@@ -756,60 +737,38 @@
     pytest.raises(ValueError, "req1.applymarker(42)")
 
 class TestRequestCachedSetup:
-    def test_request_cachedsetup(self, testdir):
-        item1,item2 = testdir.getitems("""
-            def test_func1(self, something):
-                pass
+    def test_request_cachedsetup_defaultmodule(self, testdir):
+        reprec = testdir.inline_runsource("""
+            mysetup = ["hello",].pop
+
+            def pytest_funcarg__something(request):
+                return request.cached_setup(mysetup, scope="module")
+
+            def test_func1(something):
+                assert something == "hello"
             class TestClass:
-                def test_func2(self, something):
-                    pass
+                def test_func1a(self, something):
+                    assert something == "hello"
         """)
-        req1 = funcargs.FuncargRequest(item1)
-        l = ["hello"]
-        def setup():
-            return l.pop()
-        # cached_setup's scope defaults to 'module'
-        ret1 = req1.cached_setup(setup)
-        assert ret1 == "hello"
-        ret1b = req1.cached_setup(setup)
-        assert ret1 == ret1b
-        req2 = funcargs.FuncargRequest(item2)
-        ret2 = req2.cached_setup(setup)
-        assert ret2 == ret1
+        reprec.assertoutcome(passed=2)
 
     def test_request_cachedsetup_class(self, testdir):
-        item1, item2, item3, item4 = testdir.getitems("""
-            def test_func1(self, something):
-                pass
-            def test_func2(self, something):
-                pass
+        reprec = testdir.inline_runsource("""
+            mysetup = ["hello", "hello2"].pop
+
+            def pytest_funcarg__something(request):
+                return request.cached_setup(mysetup, scope="class")
+            def test_func1(something):
+                assert something == "hello2"
+            def test_func2(something):
+                assert something == "hello2"
             class TestClass:
                 def test_func1a(self, something):
-                    pass
+                    assert something == "hello"
                 def test_func2b(self, something):
-                    pass
+                    assert something == "hello"
         """)
-        req1 = funcargs.FuncargRequest(item2)
-        l = ["hello2", "hello"]
-        def setup():
-            return l.pop()
-
-        # module level functions setup with scope=class
-        # automatically turn "class" to "module" scope
-        ret1 = req1.cached_setup(setup, scope="class")
-        assert ret1 == "hello"
-        req2 = funcargs.FuncargRequest(item2)
-        ret2 = req2.cached_setup(setup, scope="class")
-        assert ret2 == "hello"
-
-        req3 = funcargs.FuncargRequest(item3)
-        ret3a = req3.cached_setup(setup, scope="class")
-        ret3b = req3.cached_setup(setup, scope="class")
-        assert ret3a == "hello2"
-        assert ret3b == "hello2"
-        req4 = funcargs.FuncargRequest(item4)
-        ret4 = req4.cached_setup(setup, scope="class")
-        assert ret4 == ret3a
+        reprec.assertoutcome(passed=4)
 
     def test_request_cachedsetup_extrakey(self, testdir):
         item1 = testdir.getitem("def test_func(): pass")
@@ -1351,7 +1310,7 @@
     """)
     result = testdir.runpytest()
     result.stdout.fnmatch_lines([
-        "*ERROR at setup of test_lookup_error*",
+        "*ERROR*collecting*test_funcarg_lookup_error.py*",
         "*def test_lookup_error(unknown):*",
         "*LookupError: no factory found*unknown*",
         "*available funcargs*",


diff -r 19affc442801ad53c603e3d5ac912539dc1a6533 -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e testing/test_session.py
--- a/testing/test_session.py
+++ b/testing/test_session.py
@@ -9,24 +9,24 @@
                 assert 0
             def test_other():
                 raise ValueError(23)
-            def test_two(someargs):
-                pass
+            class TestClass:
+                def test_two(self, someargs):
+                    pass
         """)
         reprec = testdir.inline_run(tfile)
         passed, skipped, failed = reprec.listoutcomes()
         assert len(skipped) == 0
         assert len(passed) == 1
-        assert len(failed) == 3
+        assert len(failed) == 2
         end = lambda x: x.nodeid.split("::")[-1]
         assert end(failed[0]) == "test_one_one"
         assert end(failed[1]) == "test_other"
-        assert end(failed[2]) == "test_two"
         itemstarted = reprec.getcalls("pytest_itemcollected")
-        assert len(itemstarted) == 4
-        colstarted = reprec.getcalls("pytest_collectstart")
-        assert len(colstarted) == 1 + 1
-        col = colstarted[1].collector
-        assert isinstance(col, pytest.Module)
+        assert len(itemstarted) == 3
+        # XXX check for failing funcarg setup
+        colreports = reprec.getcalls("pytest_collectreport")
+        assert len(colreports) == 4
+        assert colreports[1].report.failed
 
     def test_nested_import_error(self, testdir):
         tfile = testdir.makepyfile("""



https://bitbucket.org/hpk42/pytest/changeset/a644996f978e/
changeset:   a644996f978e
user:        hpk42
date:        2012-07-20 14:16:28
summary:     discover funcarg factories independently from request/Function items
affected #:  6 files

diff -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f _pytest/main.py
--- a/_pytest/main.py
+++ b/_pytest/main.py
@@ -285,13 +285,15 @@
     def _repr_failure_py(self, excinfo, style=None):
         LE = self.session.funcargmanager.FuncargLookupError
         if excinfo.errisinstance(LE):
-            request = excinfo.value.request
-            fspath, lineno, msg = request._pyfuncitem.reportinfo()
-            lines, _ = inspect.getsourcelines(request.function)
-            for i, line in enumerate(lines):
-                if line.strip().startswith('def'):
-                    return FuncargLookupErrorRepr(fspath, lineno, lines[:i+1],
-                                str(excinfo.value.msg))
+            function = excinfo.value.function
+            if function is not None:
+                fspath, lineno = getfslineno(function)
+                lines, _ = inspect.getsourcelines(function)
+                for i, line in enumerate(lines):
+                    if line.strip().startswith('def'):
+                        return FuncargLookupErrorRepr(fspath,
+                                    lineno, lines[:i+1],
+                                    str(excinfo.value.msg))
         if self.config.option.fulltrace:
             style="long"
         else:
@@ -406,8 +408,8 @@
 
 class FuncargLookupError(LookupError):
     """ could not find a factory. """
-    def __init__(self, request, msg):
-        self.request = request
+    def __init__(self, function, msg):
+        self.function = function
         self.msg = msg
 
 class FuncargManager:
@@ -417,60 +419,68 @@
     def __init__(self, session):
         self.session = session
         self.config = session.config
-        self.node2name2factory = {}
+        self.arg2facspec = {}
+        session.config.pluginmanager.register(self, "funcmanage")
+        self._holderobjseen = set()
 
-    def _discoverfactories(self, request, argname):
-        node = request._pyfuncitem
-        name2factory = self.node2name2factory.setdefault(node, {})
-        if argname not in name2factory:
-            name2factory[argname] = self.config.pluginmanager.listattr(
-                    plugins=request._plugins,
-                    attrname=self._argprefix + str(argname)
-            )
-        #else: we are called recursively
-        if not name2factory[argname]:
-            self._raiselookupfailed(request, argname)
-
-    def _getfuncarg(self, request, argname):
-        node = request._pyfuncitem
+    ### XXX this hook should be called for historic events like pytest_configure
+    ### so that we don't have to do the below pytest_collection hook
+    def pytest_plugin_registered(self, plugin):
+        #print "plugin_registered", plugin
+        nodeid = ""
         try:
-            factorylist = self.node2name2factory[node][argname]
-        except KeyError:
-            # XXX at collection time this funcarg was not know to be a
-            # requirement, would be better if it would be known
-            self._discoverfactories(request, argname)
-            factorylist = self.node2name2factory[node][argname]
-
-        if not factorylist:
-            self._raiselookupfailed(request, argname)
-        funcargfactory = factorylist.pop()
-        oldarg = request._currentarg
-        mp = monkeypatch()
-        mp.setattr(request, '_currentarg', argname)
-        try:
-            param = node.callspec.getparam(argname)
-        except (AttributeError, ValueError):
+            p = py.path.local(plugin.__file__)
+        except AttributeError:
             pass
         else:
-            mp.setattr(request, 'param', param, raising=False)
+            if p.basename.startswith("conftest.py"):
+                nodeid = p.dirpath().relto(self.session.fspath)
+        self._parsefactories(plugin, nodeid)
+
+    @pytest.mark.tryfirst
+    def pytest_collection(self, session):
+        plugins = session.config.pluginmanager.getplugins()
+        for plugin in plugins:
+            self.pytest_plugin_registered(plugin)
+
+    def _parsefactories(self, holderobj, nodeid):
+        if holderobj in self._holderobjseen:
+            return
+        #print "parsefactories", holderobj
+        self._holderobjseen.add(holderobj)
+        for name in dir(holderobj):
+            #print "check", holderobj, name
+            if name.startswith(self._argprefix):
+                fname = name[len(self._argprefix):]
+                faclist = self.arg2facspec.setdefault(fname, [])
+                obj = getattr(holderobj, name)
+                faclist.append((nodeid, obj))
+
+    def getfactorylist(self, argname, nodeid, function):
         try:
-            return funcargfactory(request=request)
-        finally:
-            mp.undo()
+            factorydef = self.arg2facspec[argname]
+        except KeyError:
+            self._raiselookupfailed(argname, function, nodeid)
+        return self._matchfactories(factorydef, nodeid)
 
-    def _raiselookupfailed(self, request, argname):
+    def _matchfactories(self, factorydef, nodeid):
+        l = []
+        for baseid, factory in factorydef:
+            #print "check", basepath, nodeid
+            if nodeid.startswith(baseid):
+                l.append(factory)
+        return l
+
+    def _raiselookupfailed(self, argname, function, nodeid):
         available = []
-        for plugin in request._plugins:
-            for name in vars(plugin):
-                if name.startswith(self._argprefix):
-                    name = name[len(self._argprefix):]
-                    if name not in available:
-                        available.append(name)
-        fspath, lineno, msg = request._pyfuncitem.reportinfo()
+        for name, facdef in self.arg2facspec.items():
+            faclist = self._matchfactories(facdef, nodeid)
+            if faclist:
+                available.append(name)
         msg = "LookupError: no factory found for argument %r" % (argname,)
         msg += "\n available funcargs: %s" %(", ".join(available),)
         msg += "\n use 'py.test --funcargs [testpath]' for help on them."
-        raise FuncargLookupError(request, msg)
+        raise FuncargLookupError(function, msg)
 
 
 class NoMatch(Exception):
@@ -715,6 +725,13 @@
                      to cache on a per-session level.
         """
 
+def getfslineno(obj):
+    # xxx let decorators etc specify a sane ordering
+    if hasattr(obj, 'place_as'):
+        obj = obj.place_as
+    fslineno = py.code.getfslineno(obj)
+    assert isinstance(fslineno[1], int), obj
+    return fslineno
 
 
 class FuncargLookupErrorRepr(TerminalRepr):


diff -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f _pytest/pytester.py
--- a/_pytest/pytester.py
+++ b/_pytest/pytester.py
@@ -388,10 +388,12 @@
         return config
 
     def getitem(self,  source, funcname="test_func"):
-        for item in self.getitems(source):
+        items = self.getitems(source)
+        for item in items:
             if item.name == funcname:
                 return item
-        assert 0, "%r item not found in module:\n%s" %(funcname, source)
+        assert 0, "%r item not found in module:\n%s\nitems: %s" %(
+                  funcname, source, items)
 
     def getitems(self,  source):
         modcol = self.getmodulecol(source)


diff -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -3,6 +3,8 @@
 import inspect
 import sys
 import pytest
+from _pytest.main import getfslineno
+from _pytest.monkeypatch import monkeypatch
 
 import _pytest
 cutdir = py.path.local(_pytest.__file__).dirpath()
@@ -192,18 +194,7 @@
         return s.replace(".[", "[")
 
     def _getfslineno(self):
-        try:
-            return self._fslineno
-        except AttributeError:
-            pass
-        obj = self.obj
-        # xxx let decorators etc specify a sane ordering
-        if hasattr(obj, 'place_as'):
-            obj = obj.place_as
-
-        self._fslineno = py.code.getfslineno(obj)
-        assert isinstance(self._fslineno[1], int), obj
-        return self._fslineno
+        return getfslineno(self.obj)
 
     def reportinfo(self):
         # XXX caching?
@@ -213,12 +204,10 @@
             fspath = sys.modules[obj.__module__].__file__
             if fspath.endswith(".pyc"):
                 fspath = fspath[:-1]
-            #assert 0
-            #fn = inspect.getsourcefile(obj) or inspect.getfile(obj)
             lineno = obj.compat_co_firstlineno
             modpath = obj.__module__
         else:
-            fspath, lineno = self._getfslineno()
+            fspath, lineno = getfslineno(obj)
             modpath = self.getmodpath()
         assert isinstance(lineno, int)
         return fspath, lineno, modpath
@@ -306,6 +295,10 @@
     def _getobj(self):
         return self._memoizedcall('_obj', self._importtestmodule)
 
+    def collect(self):
+        self.session.funcargmanager._parsefactories(self.obj, self.nodeid)
+        return super(Module, self).collect()
+
     def _importtestmodule(self):
         # we assume we are only called once per module
         try:
@@ -370,7 +363,12 @@
 
 class Instance(PyCollector):
     def _getobj(self):
-        return self.parent.obj()
+        obj = self.parent.obj()
+        return obj
+
+    def collect(self):
+        self.session.funcargmanager._parsefactories(self.obj, self.nodeid)
+        return super(Instance, self).collect()
 
     def newinstance(self):
         self.obj = self._getobj()
@@ -809,7 +807,7 @@
             else:
                 self.funcargs = {}
             self._request = req = FuncargRequest(self)
-            req._discoverfactories()
+            #req._discoverfactories()
         if callobj is not _dummy:
             self.obj = callobj
         startindex = int(self.cls is not None)
@@ -885,20 +883,28 @@
         self.funcargmanager = pyfuncitem.session.funcargmanager
         self._currentarg = None
         self.funcargnames = getfuncargnames(self.function)
+        self.parentid = pyfuncitem.parent.nodeid
 
     def _discoverfactories(self):
         for argname in self.funcargnames:
             if argname not in self._funcargs:
-                self.funcargmanager._discoverfactories(self, argname)
+                self._getfaclist(argname)
 
-    @cached_property
-    def _plugins(self):
-        extra = [obj for obj in (self.module, self.instance) if obj]
-        return self._pyfuncitem.getplugins() + extra
+    def _getfaclist(self, argname):
+        faclist = self._name2factory.get(argname, None)
+        if faclist is None:
+            faclist = self.funcargmanager.getfactorylist(argname,
+                                                         self.parentid,
+                                                         self.function)
+            self._name2factory[argname] = faclist
+        elif not faclist:
+            self.funcargmanager._raiselookupfailed(argname, self.function,
+                                                   self.parentid)
+        return faclist
 
     def raiseerror(self, msg):
         """ raise a FuncargLookupError with the given message. """
-        raise self.funcargmanager.FuncargLookupError(self, msg)
+        raise self.funcargmanager.FuncargLookupError(self.function, msg)
 
     @property
     def function(self):
@@ -1001,9 +1007,23 @@
             return self._funcargs[argname]
         except KeyError:
             pass
-        val = self.funcargmanager._getfuncarg(self, argname)
-        self._funcargs[argname] = val
-        return val
+        factorylist = self._getfaclist(argname)
+        funcargfactory = factorylist.pop()
+        node = self._pyfuncitem
+        oldarg = self._currentarg
+        mp = monkeypatch()
+        mp.setattr(self, '_currentarg', argname)
+        try:
+            param = node.callspec.getparam(argname)
+        except (AttributeError, ValueError):
+            pass
+        else:
+            mp.setattr(self, 'param', param, raising=False)
+        try:
+            self._funcargs[argname] = val = funcargfactory(request=self)
+            return val
+        finally:
+            mp.undo()
 
     def _getscopeitem(self, scope):
         if scope == "function":


diff -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f testing/test_python.py
--- a/testing/test_python.py
+++ b/testing/test_python.py
@@ -647,15 +647,14 @@
             def pytest_funcarg__something(request):
                 return 1
         """)
-        item = testdir.getitem("""
+        item = testdir.makepyfile("""
             def pytest_funcarg__something(request):
                 return request.getfuncargvalue("something") + 1
             def test_func(something):
                 assert something == 2
         """)
-        req = funcargs.FuncargRequest(item)
-        val = req.getfuncargvalue("something")
-        assert val == 2
+        reprec = testdir.inline_run()
+        reprec.assertoutcome(passed=1)
 
     def test_getfuncargvalue(self, testdir):
         item = testdir.getitem("""
@@ -1296,7 +1295,9 @@
         class MyClass:
             pass
     """)
-    clscol = modcol.collect()[0]
+    # this hook finds funcarg factories
+    rep = modcol.ihook.pytest_make_collect_report(collector=modcol)
+    clscol = rep.result[0]
     clscol.obj = lambda arg1: None
     clscol.funcargs = {}
     funcargs.fillfuncargs(clscol)
@@ -1310,7 +1311,7 @@
     """)
     result = testdir.runpytest()
     result.stdout.fnmatch_lines([
-        "*ERROR*collecting*test_funcarg_lookup_error.py*",
+        "*ERROR*test_lookup_error*",
         "*def test_lookup_error(unknown):*",
         "*LookupError: no factory found*unknown*",
         "*available funcargs*",
@@ -1633,3 +1634,49 @@
             "*test_function*basic*PASSED",
             "*test_function*advanced*FAILED",
         ])
+
+### XXX shift to test_session.py
+class TestFuncargManager:
+    def pytest_funcarg__testdir(self, request):
+        testdir = request.getfuncargvalue("testdir")
+        testdir.makeconftest("""
+            def pytest_funcarg__hello(request):
+                return "conftest"
+
+            def pytest_funcarg__fm(request):
+                return request.funcargmanager
+
+            def pytest_funcarg__item(request):
+                return request._pyfuncitem
+        """)
+        return testdir
+
+    def test_parsefactories_conftest(self, testdir):
+        testdir.makepyfile("""
+            def test_hello(item, fm):
+                for name in ("fm", "hello", "item"):
+                    faclist = fm.getfactorylist(name, item.nodeid, item.obj)
+                    assert len(faclist) == 1
+                    fac = faclist[0]
+                    assert fac.__name__ == "pytest_funcarg__" + name
+        """)
+        reprec = testdir.inline_run("-s")
+        reprec.assertoutcome(passed=1)
+
+    def test_parsefactories_conftest_and_module_and_class(self, testdir):
+        testdir.makepyfile("""
+            def pytest_funcarg__hello(request):
+                return "module"
+            class TestClass:
+                def pytest_funcarg__hello(self, request):
+                    return "class"
+                def test_hello(self, item, fm):
+                    faclist = fm.getfactorylist("hello", item.nodeid, item.obj)
+                    print faclist
+                    assert len(faclist) == 3
+                    assert faclist[0](item._request) == "conftest"
+                    assert faclist[1](item._request) == "module"
+                    assert faclist[2](item._request) == "class"
+        """)
+        reprec = testdir.inline_run("-s")
+        reprec.assertoutcome(passed=1)


diff -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f testing/test_session.py
--- a/testing/test_session.py
+++ b/testing/test_session.py
@@ -17,16 +17,16 @@
         passed, skipped, failed = reprec.listoutcomes()
         assert len(skipped) == 0
         assert len(passed) == 1
-        assert len(failed) == 2
+        assert len(failed) == 3
         end = lambda x: x.nodeid.split("::")[-1]
         assert end(failed[0]) == "test_one_one"
         assert end(failed[1]) == "test_other"
         itemstarted = reprec.getcalls("pytest_itemcollected")
-        assert len(itemstarted) == 3
+        assert len(itemstarted) == 4
         # XXX check for failing funcarg setup
-        colreports = reprec.getcalls("pytest_collectreport")
-        assert len(colreports) == 4
-        assert colreports[1].report.failed
+        #colreports = reprec.getcalls("pytest_collectreport")
+        #assert len(colreports) == 4
+        #assert colreports[1].report.failed
 
     def test_nested_import_error(self, testdir):
         tfile = testdir.makepyfile("""
@@ -225,3 +225,4 @@
     result = testdir.runpytest("--ignore=hello", "--ignore=hello2")
     assert result.ret == 0
     result.stdout.fnmatch_lines(["*1 passed*"])
+


diff -r ab0f23204d9f6dfa9e41c26bd293798d33f5255e -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f testing/test_tmpdir.py
--- a/testing/test_tmpdir.py
+++ b/testing/test_tmpdir.py
@@ -4,12 +4,18 @@
 from _pytest.tmpdir import pytest_funcarg__tmpdir, TempdirHandler
 
 def test_funcarg(testdir):
-    item = testdir.getitem("""
+    testdir.makepyfile("""
             def pytest_generate_tests(metafunc):
                 metafunc.addcall(id='a')
                 metafunc.addcall(id='b')
             def test_func(tmpdir): pass
-            """, 'test_func[a]')
+    """)
+    reprec = testdir.inline_run()
+    calls = reprec.getcalls("pytest_runtest_setup")
+    item = calls[0].item
+    # pytest_unconfigure has deleted the TempdirHandler already
+    config = item.config
+    config._tmpdirhandler = TempdirHandler(config)
     p = pytest_funcarg__tmpdir(item)
     assert p.check()
     bn = p.basename.strip("0123456789")



https://bitbucket.org/hpk42/pytest/changeset/bec67ad07ffb/
changeset:   bec67ad07ffb
user:        hpk42
date:        2012-07-20 14:16:46
summary:     extend Metafunc and write a pytest_generate_tests hook on the funcarg manager
which discovers factories
affected #:  3 files

diff -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f -r bec67ad07ffbed5d029496c588090642b95b944d _pytest/main.py
--- a/_pytest/main.py
+++ b/_pytest/main.py
@@ -443,6 +443,19 @@
         for plugin in plugins:
             self.pytest_plugin_registered(plugin)
 
+    def pytest_generate_tests(self, metafunc):
+        for argname in metafunc.funcargnames:
+            faclist = self.getfactorylist(argname, metafunc.parentid,
+                                          metafunc.function, raising=False)
+            if faclist is None:
+                continue # will raise at setup time
+            for fac in faclist:
+                marker = getattr(fac, "funcarg", None)
+                if marker is not None:
+                    params = marker.kwargs.get("params")
+                    if params is not None:
+                        metafunc.parametrize(argname, params, indirect=True)
+
     def _parsefactories(self, holderobj, nodeid):
         if holderobj in self._holderobjseen:
             return
@@ -456,12 +469,14 @@
                 obj = getattr(holderobj, name)
                 faclist.append((nodeid, obj))
 
-    def getfactorylist(self, argname, nodeid, function):
+    def getfactorylist(self, argname, nodeid, function, raising=True):
         try:
             factorydef = self.arg2facspec[argname]
         except KeyError:
-            self._raiselookupfailed(argname, function, nodeid)
-        return self._matchfactories(factorydef, nodeid)
+            if raising:
+                self._raiselookupfailed(argname, function, nodeid)
+        else:
+            return self._matchfactories(factorydef, nodeid)
 
     def _matchfactories(self, factorydef, nodeid):
         l = []


diff -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f -r bec67ad07ffbed5d029496c588090642b95b944d _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -256,7 +256,7 @@
         clscol = self.getparent(Class)
         cls = clscol and clscol.obj or None
         transfer_markers(funcobj, cls, module)
-        metafunc = Metafunc(funcobj, config=self.config,
+        metafunc = Metafunc(funcobj, parentid=self.nodeid, config=self.config,
             cls=cls, module=module)
         gentesthook = self.config.hook.pytest_generate_tests
         extra = [module]
@@ -555,10 +555,12 @@
 
 
 class Metafunc:
-    def __init__(self, function, config=None, cls=None, module=None):
+    def __init__(self, function, config=None, cls=None, module=None,
+                 parentid=""):
         self.config = config
         self.module = module
         self.function = function
+        self.parentid = parentid
         self.funcargnames = getfuncargnames(function,
                                             startindex=int(cls is not None))
         self.cls = cls
@@ -885,11 +887,6 @@
         self.funcargnames = getfuncargnames(self.function)
         self.parentid = pyfuncitem.parent.nodeid
 
-    def _discoverfactories(self):
-        for argname in self.funcargnames:
-            if argname not in self._funcargs:
-                self._getfaclist(argname)
-
     def _getfaclist(self, argname):
         faclist = self._name2factory.get(argname, None)
         if faclist is None:


diff -r a644996f978eb7a02138c8bde1b9fffdebdf6a8f -r bec67ad07ffbed5d029496c588090642b95b944d testing/test_python.py
--- a/testing/test_python.py
+++ b/testing/test_python.py
@@ -989,6 +989,7 @@
     def test_parametrize_functional(self, testdir):
         testdir.makepyfile("""
             def pytest_generate_tests(metafunc):
+                assert "test_parametrize_functional" in metafunc.parentid
                 metafunc.parametrize('x', [1,2], indirect=True)
                 metafunc.parametrize('y', [2])
             def pytest_funcarg__x(request):
@@ -1680,3 +1681,20 @@
         """)
         reprec = testdir.inline_run("-s")
         reprec.assertoutcome(passed=1)
+
+class TestFuncargMarker:
+    def test_parametrize(self, testdir):
+        testdir.makepyfile("""
+            import pytest
+            @pytest.mark.funcarg(params=["a", "b", "c"])
+            def pytest_funcarg__arg(request):
+                return request.param
+            l = []
+            def test_param(arg):
+                l.append(arg)
+            def test_result():
+                assert l == list("abc")
+        """)
+        reprec = testdir.inline_run()
+        reprec.assertoutcome(passed=4)
+



https://bitbucket.org/hpk42/pytest/changeset/cae9ed2d4230/
changeset:   cae9ed2d4230
user:        hpk42
date:        2012-07-20 14:16:46
summary:     implement funcarg factory scope marker and ScopeMismatch detection
affected #:  2 files

diff -r bec67ad07ffbed5d029496c588090642b95b944d -r cae9ed2d4230a4ade4e8f7c5fd8864004ecd3d85 _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -886,6 +886,7 @@
         self._currentarg = None
         self.funcargnames = getfuncargnames(self.function)
         self.parentid = pyfuncitem.parent.nodeid
+        self.scope = "function"
 
     def _getfaclist(self, argname):
         faclist = self._name2factory.get(argname, None)
@@ -982,6 +983,9 @@
         try:
             val = cache[cachekey]
         except KeyError:
+            __tracebackhide__ = True
+            check_scope(self.scope, scope)
+            __tracebackhide__ = False
             val = setup()
             cache[cachekey] = val
             if teardown is not None:
@@ -1007,7 +1011,6 @@
         factorylist = self._getfaclist(argname)
         funcargfactory = factorylist.pop()
         node = self._pyfuncitem
-        oldarg = self._currentarg
         mp = monkeypatch()
         mp.setattr(self, '_currentarg', argname)
         try:
@@ -1016,11 +1019,24 @@
             pass
         else:
             mp.setattr(self, 'param', param, raising=False)
-        try:
-            self._funcargs[argname] = val = funcargfactory(request=self)
-            return val
-        finally:
-            mp.undo()
+
+        # implemenet funcarg marker scope
+        marker = getattr(funcargfactory, "funcarg", None)
+        scope = None
+        if marker is not None:
+            scope = marker.kwargs.get("scope")
+        if scope is not None:
+            __tracebackhide__ = True
+            check_scope(self.scope, scope)
+            __tracebackhide__ = False
+            mp.setattr(self, "scope", scope)
+            val = self.cached_setup(lambda: funcargfactory(request=self),
+                                    scope=scope)
+        else:
+            val = funcargfactory(request=self)
+        mp.undo()
+        self._funcargs[argname] = val
+        return val
 
     def _getscopeitem(self, scope):
         if scope == "function":
@@ -1039,7 +1055,7 @@
     def addfinalizer(self, finalizer):
         """add finalizer function to be called after test function
         finished execution. """
-        self._addfinalizer(finalizer, scope="function")
+        self._addfinalizer(finalizer, scope=self.scope)
 
     def _addfinalizer(self, finalizer, scope):
         colitem = self._getscopeitem(scope)
@@ -1049,3 +1065,15 @@
     def __repr__(self):
         return "<FuncargRequest for %r>" %(self._pyfuncitem)
 
+class ScopeMismatchError(Exception):
+    """ A funcarg factory tries to access a funcargvalue/factory
+    which has a lower scope (e.g. a Session one calls a function one)
+    """
+scopes = "session module class function".split()
+def check_scope(currentscope, newscope):
+    __tracebackhide__ = True
+    i_currentscope = scopes.index(currentscope)
+    i_newscope = scopes.index(newscope)
+    if i_newscope > i_currentscope:
+        raise ScopeMismatchError("You tried to access a %r scoped funcarg "
+            "from a %r scoped one." % (newscope, currentscope))


diff -r bec67ad07ffbed5d029496c588090642b95b944d -r cae9ed2d4230a4ade4e8f7c5fd8864004ecd3d85 testing/test_python.py
--- a/testing/test_python.py
+++ b/testing/test_python.py
@@ -58,7 +58,7 @@
         ])
 
     def test_setup_teardown_class_as_classmethod(self, testdir):
-        testdir.makepyfile("""
+        testdir.makepyfile(test_mod1="""
             class TestClassMethod:
                 @classmethod
                 def setup_class(cls):
@@ -1698,3 +1698,110 @@
         reprec = testdir.inline_run()
         reprec.assertoutcome(passed=4)
 
+    def test_scope_session(self, testdir):
+        testdir.makepyfile("""
+            import pytest
+            l = []
+            @pytest.mark.funcarg(scope="module")
+            def pytest_funcarg__arg(request):
+                l.append(1)
+                return 1
+
+            def test_1(arg):
+                assert arg == 1
+            def test_2(arg):
+                assert arg == 1
+                assert len(l) == 1
+            class TestClass:
+                def test3(self, arg):
+                    assert arg == 1
+                    assert len(l) == 1
+        """)
+        reprec = testdir.inline_run()
+        reprec.assertoutcome(passed=3)
+
+    def test_scope_module_uses_session(self, testdir):
+        testdir.makepyfile("""
+            import pytest
+            l = []
+            @pytest.mark.funcarg(scope="module")
+            def pytest_funcarg__arg(request):
+                l.append(1)
+                return 1
+
+            def test_1(arg):
+                assert arg == 1
+            def test_2(arg):
+                assert arg == 1
+                assert len(l) == 1
+            class TestClass:
+                def test3(self, arg):
+                    assert arg == 1
+                    assert len(l) == 1
+        """)
+        reprec = testdir.inline_run()
+        reprec.assertoutcome(passed=3)
+
+    def test_scope_module_and_finalizer(self, testdir):
+        testdir.makeconftest("""
+            import pytest
+            finalized = []
+            created = []
+            @pytest.mark.funcarg(scope="module")
+            def pytest_funcarg__arg(request):
+                created.append(1)
+                assert request.scope == "module"
+                request.addfinalizer(lambda: finalized.append(1))
+            def pytest_funcarg__created(request):
+                return len(created)
+            def pytest_funcarg__finalized(request):
+                return len(finalized)
+        """)
+        testdir.makepyfile(
+            test_mod1="""
+                def test_1(arg, created, finalized):
+                    assert created == 1
+                    assert finalized == 0
+                def test_2(arg, created, finalized):
+                    assert created == 1
+                    assert finalized == 0""",
+            test_mod2="""
+                def test_3(arg, created, finalized):
+                    assert created == 2
+                    assert finalized == 1""",
+            test_mode3="""
+                def test_4(arg, created, finalized):
+                    assert created == 3
+                    assert finalized == 2
+            """)
+        reprec = testdir.inline_run()
+        reprec.assertoutcome(passed=4)
+
+    @pytest.mark.parametrize("method", [
+        'request.getfuncargvalue("arg")',
+        'request.cached_setup(lambda: None, scope="function")',
+    ], ids=["getfuncargvalue", "cached_setup"])
+    def test_scope_mismatch(self, testdir, method):
+        testdir.makeconftest("""
+            import pytest
+            finalized = []
+            created = []
+            @pytest.mark.funcarg(scope="function")
+            def pytest_funcarg__arg(request):
+                pass
+        """)
+        testdir.makepyfile(
+            test_mod1="""
+                import pytest
+                @pytest.mark.funcarg(scope="session")
+                def pytest_funcarg__arg(request):
+                    %s
+                def test_1(arg):
+                    pass
+            """ % method)
+        result = testdir.runpytest()
+        assert result.ret != 0
+        result.stdout.fnmatch_lines([
+            "*ScopeMismatch*You tried*function*from*session*",
+        ])
+



https://bitbucket.org/hpk42/pytest/changeset/1c8d7b1e1d9f/
changeset:   1c8d7b1e1d9f
user:        hpk42
date:        2012-07-20 14:16:49
summary:     allow registration of "funcarg" marked factories
affected #:  2 files

diff -r cae9ed2d4230a4ade4e8f7c5fd8864004ecd3d85 -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad _pytest/main.py
--- a/_pytest/main.py
+++ b/_pytest/main.py
@@ -463,11 +463,20 @@
         self._holderobjseen.add(holderobj)
         for name in dir(holderobj):
             #print "check", holderobj, name
-            if name.startswith(self._argprefix):
-                fname = name[len(self._argprefix):]
-                faclist = self.arg2facspec.setdefault(fname, [])
-                obj = getattr(holderobj, name)
-                faclist.append((nodeid, obj))
+            obj = getattr(holderobj, name)
+            # funcarg factories either have a pytest_funcarg__ prefix
+            # or are "funcarg" marked
+            if hasattr(obj, "funcarg"):
+                if name.startswith(self._argprefix):
+                    argname = name[len(self._argprefix):]
+                else:
+                    argname = name
+            elif name.startswith(self._argprefix):
+                argname = name[len(self._argprefix):]
+            else:
+                continue
+            faclist = self.arg2facspec.setdefault(argname, [])
+            faclist.append((nodeid, obj))
 
     def getfactorylist(self, argname, nodeid, function, raising=True):
         try:


diff -r cae9ed2d4230a4ade4e8f7c5fd8864004ecd3d85 -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad testing/test_python.py
--- a/testing/test_python.py
+++ b/testing/test_python.py
@@ -1805,3 +1805,25 @@
             "*ScopeMismatch*You tried*function*from*session*",
         ])
 
+    def test_register_only_with_mark(self, testdir):
+        testdir.makeconftest("""
+            import pytest
+            finalized = []
+            created = []
+            @pytest.mark.funcarg
+            def arg(request):
+                return 1
+        """)
+        testdir.makepyfile(
+            test_mod1="""
+                import pytest
+                @pytest.mark.funcarg
+                def arg(request):
+                    return request.getfuncargvalue("arg") + 1
+                def test_1(arg):
+                    assert arg == 2
+            """)
+        reprec = testdir.inline_run()
+        reprec.assertoutcome(passed=1)
+
+



https://bitbucket.org/hpk42/pytest/changeset/0f6467a7fbe8/
changeset:   0f6467a7fbe8
user:        hpk42
date:        2012-07-20 14:16:50
summary:     implement a scope/parametrized examples using the so-far new features
also fix a bug with scoping/parametrization
affected #:  8 files

diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 _pytest/__init__.py
--- a/_pytest/__init__.py
+++ b/_pytest/__init__.py
@@ -1,2 +1,2 @@
 #
-__version__ = '2.3.0.dev2'
+__version__ = '2.3.0.dev3'


diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -1030,8 +1030,11 @@
             check_scope(self.scope, scope)
             __tracebackhide__ = False
             mp.setattr(self, "scope", scope)
+            kwargs = {}
+            if hasattr(self, "param"):
+                kwargs["extrakey"] = param
             val = self.cached_setup(lambda: funcargfactory(request=self),
-                                    scope=scope)
+                                    scope=scope, **kwargs)
         else:
             val = funcargfactory(request=self)
         mp.undo()


diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 doc/en/conf.py
--- a/doc/en/conf.py
+++ b/doc/en/conf.py
@@ -17,7 +17,7 @@
 #
 # The full version, including alpha/beta/rc tags.
 # The short X.Y version.
-version = release = "2.3.0.dev5"
+version = release = "2.3.0.dev3"
 
 import sys, os
 


diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 doc/en/example/index.txt
--- a/doc/en/example/index.txt
+++ b/doc/en/example/index.txt
@@ -21,3 +21,4 @@
    markers.txt
    pythoncollection.txt
    nonpython.txt
+   newexamples.txt


diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 doc/en/example/newexamples.txt
--- /dev/null
+++ b/doc/en/example/newexamples.txt
@@ -0,0 +1,183 @@
+
+Scoping and parametrizing Funcarg factories
+---------------------------------------------------
+
+.. regendoc:wipe
+
+.. versionadded:: 2.3
+
+The ``@pytest.mark.funcarg`` marker allows 
+
+* to mark a function without a ``pytest_funcarg__`` as a factory
+* to cause parametrization and run all tests multiple times
+  with the multiple created resources
+* to set a scope which determines the level of caching
+
+Here is a simple example for defining a SMTPServer server
+object with a session scope::
+
+    # content of conftest.py
+    import pytest
+    import smtplib
+
+    @pytest.mark.funcarg(scope="session")
+    def smtp(request):
+        smtp = smtplib.SMTP("merlinux.eu")
+        request.addfinalizer(smtp.close)
+        return smtp
+
+You can now use this server connection from your tests::
+
+    # content of test_module.py
+    def test_ehlo(smtp):
+        response = smtp.ehlo()
+        assert response[0] == 250 
+        assert "merlinux" in response[1]
+        assert 0  # for demo purposes
+
+    def test_noop(smtp):
+        response = smtp.noop()
+        assert response[0] == 250
+        assert 0  # for demo purposes
+
+If you run the tests::
+
+    $ py.test -q
+    collecting ... collected 2 items
+    FF
+    ================================= FAILURES =================================
+    ________________________________ test_ehlo _________________________________
+    
+    smtp = <smtplib.SMTP instance at 0x28599e0>
+    
+        def test_ehlo(smtp):
+            response = smtp.ehlo()
+            assert response[0] == 250
+            assert "merlinux" in response[1]
+    >       assert 0  # for demo purposes
+    E       assert 0
+    
+    test_module.py:5: AssertionError
+    ________________________________ test_noop _________________________________
+    
+    smtp = <smtplib.SMTP instance at 0x28599e0>
+    
+        def test_noop(smtp):
+            response = smtp.noop()
+            assert response[0] == 250
+    >       assert 0  # for demo purposes
+    E       assert 0
+    
+    test_module.py:10: AssertionError
+    2 failed in 0.14 seconds
+
+you will see the two ``assert 0`` failing and can see that
+the same (session-scoped) object was passed into the two test functions.
+
+If you now want to test multiple servers you can simply parametrize
+the ``smtp`` factory::
+
+    # content of conftest.py
+    import pytest
+    import smtplib
+
+    @pytest.mark.funcarg(scope="session", 
+                         params=["merlinux.eu", "mail.python.org"])
+    def smtp(request):
+        smtp = smtplib.SMTP(request.param)
+        def fin():
+            print "closing", smtp
+            smtp.close()
+        request.addfinalizer(fin)
+        return smtp
+
+Only two lines changed and no test code needs to change.  Let's do
+another run::
+
+    $ py.test -q
+    collecting ... collected 4 items
+    FFFF
+    ================================= FAILURES =================================
+    __________________________ test_ehlo[merlinux.eu] __________________________
+    
+    smtp = <smtplib.SMTP instance at 0x2bf3d40>
+    
+        def test_ehlo(smtp):
+            response = smtp.ehlo()
+            assert response[0] == 250
+            assert "merlinux" in response[1]
+    >       assert 0  # for demo purposes
+    E       assert 0
+    
+    test_module.py:5: AssertionError
+    ________________________ test_ehlo[mail.python.org] ________________________
+    
+    smtp = <smtplib.SMTP instance at 0x2bf9170>
+    
+        def test_ehlo(smtp):
+            response = smtp.ehlo()
+            assert response[0] == 250
+    >       assert "merlinux" in response[1]
+    E       assert 'merlinux' in 'mail.python.org\nSIZE 10240000\nETRN\nSTARTTLS\nENHANCEDSTATUSCODES\n8BITMIME\nDSN'
+    
+    test_module.py:4: AssertionError
+    __________________________ test_noop[merlinux.eu] __________________________
+    
+    smtp = <smtplib.SMTP instance at 0x2bf3d40>
+    
+        def test_noop(smtp):
+            response = smtp.noop()
+            assert response[0] == 250
+    >       assert 0  # for demo purposes
+    E       assert 0
+    
+    test_module.py:10: AssertionError
+    ________________________ test_noop[mail.python.org] ________________________
+    
+    smtp = <smtplib.SMTP instance at 0x2bf9170>
+    
+        def test_noop(smtp):
+            response = smtp.noop()
+            assert response[0] == 250
+    >       assert 0  # for demo purposes
+    E       assert 0
+    
+    test_module.py:10: AssertionError
+    4 failed in 5.70 seconds
+    closing <smtplib.SMTP instance at 0x2bf9170>
+    closing <smtplib.SMTP instance at 0x2bf3d40>
+
+We get four failures because we are running the two tests twice with
+different ``smtp`` instantiations as defined on the factory.
+Note that with the ``mail.python.org`` connection the second tests
+fails already in ``test_ehlo`` because it wrongly expects a specific
+server string.
+
+You can look at what tests pytest collects without running them::
+
+    $ py.test --collectonly
+    =========================== test session starts ============================
+    platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev3
+    plugins: xdist, bugzilla, cache, oejskit, cli, pep8, cov
+    collecting ... collected 4 items
+    <Module 'test_module.py'>
+      <Function 'test_ehlo[merlinux.eu]'>
+      <Function 'test_ehlo[mail.python.org]'>
+      <Function 'test_noop[merlinux.eu]'>
+      <Function 'test_noop[mail.python.org]'>
+    
+    =============================  in 0.02 seconds =============================
+
+And you can run without output capturing and minimized failure reporting to check that the ``smtp`` objects are finalized at session end::
+
+    $ py.test --tb=line -q -s
+    collecting ... collected 4 items
+    FFFF
+    ================================= FAILURES =================================
+    /home/hpk/tmp/doc-exec-330/test_module.py:5: assert 0
+    /home/hpk/tmp/doc-exec-330/test_module.py:4: assert 'merlinux' in 'mail.python.org\nSIZE 10240000\nETRN\nSTARTTLS\nENHANCEDSTATUSCODES\n8BITMIME\nDSN'
+    /home/hpk/tmp/doc-exec-330/test_module.py:10: assert 0
+    /home/hpk/tmp/doc-exec-330/test_module.py:10: assert 0
+    4 failed in 6.02 seconds
+    closing <smtplib.SMTP instance at 0x1f5ef38>
+    closing <smtplib.SMTP instance at 0x1f5acf8>


diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 doc/en/resources.txt
--- a/doc/en/resources.txt
+++ b/doc/en/resources.txt
@@ -94,6 +94,8 @@
 Direct scoping of funcarg factories
 --------------------------------------------------------
 
+.. note:: Implemented
+
 Instead of calling cached_setup(), you can decorate your factory
 to state its scope::
 
@@ -116,6 +118,8 @@
 Direct parametrization of funcarg resource factories 
 ----------------------------------------------------------
 
+.. note:: Implemented
+
 Previously, funcarg factories could not directly cause parametrization.
 You needed to specify a ``@parametrize`` or implement a ``pytest_generate_tests`` hook to perform parametrization, i.e. calling a test multiple times
 with different value sets.  pytest-2.X introduces a decorator for use
@@ -154,6 +158,8 @@
 Direct usage of funcargs with funcargs factories
 ----------------------------------------------------------
 
+.. note:: Not Implemented - unclear if to.
+
 You can now directly use funcargs in funcarg factories.  Example::
 
     @pytest.mark.funcarg(scope="session")
@@ -169,6 +175,8 @@
 The "pytest_funcarg__" prefix becomes optional
 -----------------------------------------------------
 
+.. note:: Implemented
+
 When using the ``@funcarg`` decorator you do not need to use
 the ``pytest_funcarg__`` prefix any more::
 
@@ -186,6 +194,8 @@
 support for a new @setup marker
 ------------------------------------------------------
 
+.. note:: Not-Implemented, still under consideration if to.
+
 pytest for a long time offered a pytest_configure and a pytest_sessionstart
 hook which are often used to setup global resources.  This suffers from
 several problems:
@@ -252,6 +262,8 @@
 Using funcarg resources in xUnit setup methods
 ------------------------------------------------------------
 
+.. note:: Not implemented. Not clear if to.
+
 XXX Consider this feature in contrast to the @setup feature - probably
 introducing one of them is better and the @setup decorator is more flexible.
 
@@ -296,6 +308,8 @@
 the "directory" caching scope
 --------------------------------------------
 
+.. note:: Not implemented.
+
 All API accepting a scope (:py:func:`cached_setup()`  and
 the new funcarg/setup decorators) now also accept a "directory"
 specification.  This allows to restrict/cache resource values on a
@@ -304,6 +318,8 @@
 funcarg and setup discovery now happens at collection time
 ---------------------------------------------------------------------
 
+.. note:: Partially implemented - collectonly shows no extra information
+
 pytest-2.X takes care to discover funcarg factories and setup_X methods
 at collection time.  This is more efficient especially for large test suites. 
 Moreover, a call to "py.test --collectonly" should be able to show
@@ -404,7 +420,7 @@
 ISSUES
 --------------------------
 
-decorating a parametrized funcarg factory:
+decorating a parametrized funcarg factory::
 
     @pytest.mark.funcarg(scope="session", params=["mysql", "pg"])
     def db(request):


diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 setup.py
--- a/setup.py
+++ b/setup.py
@@ -24,7 +24,7 @@
         name='pytest',
         description='py.test: simple powerful testing with Python',
         long_description = long_description,
-        version='2.3.0.dev2',
+        version='2.3.0.dev3',
         url='http://pytest.org',
         license='MIT license',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],


diff -r 1c8d7b1e1d9f131def25466d8ff88e466754f2ad -r 0f6467a7fbe8a78e0e07d88de95808398ce5ac20 testing/test_python.py
--- a/testing/test_python.py
+++ b/testing/test_python.py
@@ -1826,4 +1826,17 @@
         reprec = testdir.inline_run()
         reprec.assertoutcome(passed=1)
 
-
+    def test_parametrize_and_scope(self, testdir):
+        testdir.makepyfile("""
+            import pytest
+            @pytest.mark.funcarg(scope="module", params=["a", "b", "c"])
+            def pytest_funcarg__arg(request):
+                return request.param
+            l = []
+            def test_param(arg):
+                l.append(arg)
+            def test_result():
+                assert l == list("abc")
+        """)
+        reprec = testdir.inline_run()
+        reprec.assertoutcome(passed=4)

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the pytest-commit mailing list