2012-08-01 20:52:51 +08:00
|
|
|
|
2012-08-04 01:08:27 +08:00
|
|
|
.. _`funcargcompare`:
|
|
|
|
|
2012-10-19 17:12:13 +08:00
|
|
|
pytest-2.3: reasoning for fixture/funcarg evolution
|
2012-08-01 20:52:51 +08:00
|
|
|
=============================================================
|
|
|
|
|
|
|
|
**Target audience**: Reading this document requires basic knowledge of
|
2012-08-04 01:08:27 +08:00
|
|
|
python testing, xUnit setup methods and the (previous) basic pytest
|
|
|
|
funcarg mechanism, see http://pytest.org/2.2.4/funcargs.html
|
2012-10-07 19:06:17 +08:00
|
|
|
If you are new to pytest, then you can simply ignore this
|
|
|
|
section and read the other sections.
|
2012-08-01 20:52:51 +08:00
|
|
|
|
|
|
|
.. currentmodule:: _pytest
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Shortcomings of the previous ``pytest_funcarg__`` mechanism
|
|
|
|
--------------------------------------------------------------
|
2012-08-01 20:52:51 +08:00
|
|
|
|
2012-08-04 01:08:27 +08:00
|
|
|
The pre pytest-2.3 funcarg mechanism calls a factory each time a
|
|
|
|
funcarg for a test function is required. If a factory wants to
|
|
|
|
re-use a resource across different scopes, it often used
|
|
|
|
the ``request.cached_setup()`` helper to manage caching of
|
2012-08-01 20:52:51 +08:00
|
|
|
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 several limitations and difficulties with this approach:
|
|
|
|
|
|
|
|
1. Scoping funcarg 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. Multiple parametrized session-scoped resources will be active
|
|
|
|
at the same time, making it hard for them to affect global state
|
|
|
|
of the application under test.
|
|
|
|
|
|
|
|
4. there is no way how you can make use of funcarg factories
|
|
|
|
in xUnit setup methods.
|
|
|
|
|
2012-10-05 20:24:44 +08:00
|
|
|
5. A non-parametrized fixture function cannot use a parametrized
|
2012-08-01 20:52:51 +08:00
|
|
|
funcarg resource if it isn't stated in the test function signature.
|
|
|
|
|
|
|
|
All of these limitations are addressed with pytest-2.3 and its
|
2012-10-07 19:06:17 +08:00
|
|
|
improved :ref:`fixture mechanism <fixture>`.
|
|
|
|
|
2012-08-01 20:52:51 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Direct scoping of fixture/funcarg factories
|
2012-08-01 20:52:51 +08:00
|
|
|
--------------------------------------------------------
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Instead of calling cached_setup() with a cache scope, you can use the
|
|
|
|
:ref:`@pytest.fixture <pytest.fixture>` decorator and directly state
|
|
|
|
the scope::
|
2012-08-01 20:52:51 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session")
|
2012-09-17 22:36:10 +08:00
|
|
|
def db(request):
|
2012-08-01 20:52:51 +08:00
|
|
|
# factory will only be invoked once per session -
|
|
|
|
db = DataBase()
|
2012-09-17 22:36:10 +08:00
|
|
|
request.addfinalizer(db.destroy) # destroy when session is finished
|
2012-08-01 20:52:51 +08:00
|
|
|
return db
|
|
|
|
|
|
|
|
This factory implementation does not need to call ``cached_setup()`` anymore
|
|
|
|
because it will only be invoked once per session. Moreover, the
|
2012-09-17 22:36:10 +08:00
|
|
|
``request.addfinalizer()`` registers a finalizer according to the specified
|
2012-08-04 01:08:27 +08:00
|
|
|
resource scope on which the factory function is operating.
|
2012-08-01 20:52:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
Direct parametrization of funcarg resource factories
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
Previously, funcarg factories could not directly cause parametrization.
|
2012-08-04 01:08:27 +08:00
|
|
|
You needed to specify a ``@parametrize`` decorator on your test function
|
|
|
|
or implement a ``pytest_generate_tests`` hook to perform
|
|
|
|
parametrization, i.e. calling a test multiple times with different value
|
|
|
|
sets. pytest-2.3 introduces a decorator for use on the factory itself::
|
2012-08-01 20:52:51 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=["mysql", "pg"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def db(request):
|
|
|
|
... # use request.param
|
2012-08-01 20:52:51 +08:00
|
|
|
|
|
|
|
Here the factory will be invoked twice (with the respective "mysql"
|
2012-09-17 22:36:10 +08:00
|
|
|
and "pg" values set as ``request.param`` attributes) and and all of
|
2012-08-01 20:52:51 +08:00
|
|
|
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
|
2012-10-09 20:35:17 +08:00
|
|
|
``request.param`` was already used when test functions/classes were
|
|
|
|
parametrized via
|
2012-08-01 20:52:51 +08:00
|
|
|
:py:func:`~_pytest.python.Metafunc.parametrize(indirect=True)` calls.
|
|
|
|
|
|
|
|
Of course it's perfectly fine to combine parametrization and scoping::
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session", params=["mysql", "pg"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def db(request):
|
|
|
|
if request.param == "mysql":
|
2012-08-01 20:52:51 +08:00
|
|
|
db = MySQL()
|
2012-09-17 22:36:10 +08:00
|
|
|
elif request.param == "pg":
|
2012-08-01 20:52:51 +08:00
|
|
|
db = PG()
|
2012-09-17 22:36:10 +08:00
|
|
|
request.addfinalizer(db.destroy) # destroy when session is finished
|
2012-08-01 20:52:51 +08:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2012-10-06 05:44:18 +08:00
|
|
|
No ``pytest_funcarg__`` prefix when using @fixture decorator
|
2012-08-01 20:52:51 +08:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2012-10-06 05:44:18 +08:00
|
|
|
When using the ``@fixture`` decorator the name of the function
|
2012-08-04 01:08:27 +08:00
|
|
|
denotes the name under which the resource can be accessed as a function
|
|
|
|
argument::
|
2012-08-01 20:52:51 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-09-17 22:36:10 +08:00
|
|
|
def db(request):
|
2012-08-01 20:52:51 +08:00
|
|
|
...
|
|
|
|
|
|
|
|
The name under which the funcarg resource can be requested is ``db``.
|
|
|
|
|
2012-08-04 01:08:27 +08:00
|
|
|
You can still use the "old" non-decorator way of specifying funcarg factories
|
2012-08-01 20:52:51 +08:00
|
|
|
aka::
|
|
|
|
|
2012-08-04 01:08:27 +08:00
|
|
|
def pytest_funcarg__db(request):
|
2012-08-01 20:52:51 +08:00
|
|
|
...
|
|
|
|
|
2012-08-13 18:58:08 +08:00
|
|
|
|
|
|
|
But it is then not possible to define scoping and parametrization.
|
|
|
|
It is thus recommended to use the factory decorator.
|
2012-08-01 20:52:51 +08:00
|
|
|
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
solving per-session setup / autouse fixtures
|
2012-08-01 20:52:51 +08:00
|
|
|
--------------------------------------------------------------
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2013-08-01 23:32:19 +08:00
|
|
|
2. if you only perform a collection (with "--collect-only")
|
2012-08-01 20:52:51 +08:00
|
|
|
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.
|
|
|
|
|
2012-08-04 01:08:27 +08:00
|
|
|
Moreover, it was not easy to define a scoped setup from plugins or
|
2012-08-01 20:52:51 +08:00
|
|
|
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,
|
2012-10-12 20:52:36 +08:00
|
|
|
pytest-2.3 introduces :ref:`autouse fixtures` which fully
|
2012-10-07 19:06:17 +08:00
|
|
|
integrate with the generic :ref:`fixture mechanism <fixture>`
|
|
|
|
and obsolete many prior uses of pytest hooks.
|
2012-08-01 20:52:51 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
funcargs/fixture discovery now happens at collection time
|
2012-08-01 20:52:51 +08:00
|
|
|
---------------------------------------------------------------------
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
pytest-2.3 takes care to discover fixture/funcarg factories
|
2012-08-01 20:52:51 +08:00
|
|
|
at collection time. This is more efficient especially for large test suites.
|
2013-08-01 23:32:19 +08:00
|
|
|
Moreover, a call to "py.test --collect-only" should be able to in the future
|
2012-08-04 01:08:27 +08:00
|
|
|
show a lot of setup-information and thus presents a nice method to get an
|
2012-10-07 19:06:17 +08:00
|
|
|
overview of fixture management in your project.
|
|
|
|
|
|
|
|
.. _`compatibility notes`:
|
|
|
|
|
|
|
|
.. _`funcargscompat`:
|
|
|
|
|
|
|
|
Conclusion and compatibility notes
|
|
|
|
---------------------------------------------------------
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
**funcargs** were originally introduced to pytest-2.0. In pytest-2.3
|
|
|
|
the mechanism was extended and refined and is now described as
|
|
|
|
fixtures:
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
* previously funcarg factories were specified with a special
|
|
|
|
``pytest_funcarg__NAME`` prefix instead of using the
|
|
|
|
``@pytest.fixture`` decorator.
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
* Factories received a ``request`` object which managed caching through
|
2012-10-07 19:06:17 +08:00
|
|
|
``request.cached_setup()`` calls and allowed using other funcargs via
|
|
|
|
``request.getfuncargvalue()`` calls. These intricate APIs made it hard
|
|
|
|
to do proper parametrization and implement resource caching. The
|
2012-10-09 20:35:17 +08:00
|
|
|
new :py:func:`pytest.fixture`` decorator allows to declare the scope
|
2012-10-07 19:06:17 +08:00
|
|
|
and let pytest figure things out for you.
|
|
|
|
|
|
|
|
* if you used parametrization and funcarg factories which made use of
|
|
|
|
``request.cached_setup()`` it is recommeneded to invest a few minutes
|
|
|
|
and simplify your fixture function code to use the :ref:`@pytest.fixture`
|
|
|
|
decorator instead. This will also allow to take advantage of
|
|
|
|
the automatic per-resource grouping of tests.
|
|
|
|
|
2012-08-01 20:52:51 +08:00
|
|
|
|