| 1 |
:orphan: |
| 2 |
|
| 3 |
.. _`funcargcompare`: |
| 4 |
|
| 5 |
pytest-2.3: reasoning for fixture/funcarg evolution |
| 6 |
============================================================= |
| 7 |
|
| 8 |
**Target audience**: Reading this document requires basic knowledge of |
| 9 |
python testing, xUnit setup methods and the (previous) basic pytest |
| 10 |
funcarg mechanism, see :ref:`historical funcargs and pytest.funcargs`. |
| 11 |
If you are new to pytest, then you can simply ignore this |
| 12 |
section and read the other sections. |
| 13 |
|
| 14 |
Shortcomings of the previous ``pytest_funcarg__`` mechanism |
| 15 |
-------------------------------------------------------------- |
| 16 |
|
| 17 |
The pre pytest-2.3 funcarg mechanism calls a factory each time a |
| 18 |
funcarg for a test function is required. If a factory wants to |
| 19 |
reuse a resource across different scopes, it often used |
| 20 |
the ``request.cached_setup()`` helper to manage caching of |
| 21 |
resources. Here is a basic example of how we could implement |
| 22 |
a per-session Database object: |
| 23 |
|
| 24 |
.. code-block:: python |
| 25 |
|
| 26 |
# content of conftest.py |
| 27 |
class Database: |
| 28 |
def __init__(self): |
| 29 |
print("database instance created") |
| 30 |
|
| 31 |
def destroy(self): |
| 32 |
print("database instance destroyed") |
| 33 |
|
| 34 |
|
| 35 |
def pytest_funcarg__db(request): |
| 36 |
return request.cached_setup( |
| 37 |
setup=DataBase, teardown=lambda db: db.destroy, scope="session" |
| 38 |
) |
| 39 |
|
| 40 |
There are several limitations and difficulties with this approach: |
| 41 |
|
| 42 |
1. Scoping funcarg resource creation is not straightforward, instead one must |
| 43 |
understand the intricate cached_setup() method mechanics. |
| 44 |
|
| 45 |
2. parametrizing the "db" resource is not straightforward: |
| 46 |
you need to apply a "parametrize" decorator or implement a |
| 47 |
:hook:`pytest_generate_tests` hook |
| 48 |
calling :py:func:`~pytest.Metafunc.parametrize` which |
| 49 |
performs parametrization at the places where the resource |
| 50 |
is used. Moreover, you need to modify the factory to use an |
| 51 |
``extrakey`` parameter containing ``request.param`` to the |
| 52 |
``Request.cached_setup`` call. |
| 53 |
|
| 54 |
3. Multiple parametrized session-scoped resources will be active |
| 55 |
at the same time, making it hard for them to affect global state |
| 56 |
of the application under test. |
| 57 |
|
| 58 |
4. there is no way you can make use of funcarg factories |
| 59 |
in xUnit setup methods. |
| 60 |
|
| 61 |
5. A non-parametrized fixture function cannot use a parametrized |
| 62 |
funcarg resource if it isn't stated in the test function signature. |
| 63 |
|
| 64 |
All of these limitations are addressed with pytest-2.3 and its |
| 65 |
improved :ref:`fixture mechanism <fixture>`. |
| 66 |
|
| 67 |
|
| 68 |
Direct scoping of fixture/funcarg factories |
| 69 |
-------------------------------------------------------- |
| 70 |
|
| 71 |
Instead of calling cached_setup() with a cache scope, you can use the |
| 72 |
:ref:`@pytest.fixture <pytest.fixture>` decorator and directly state |
| 73 |
the scope: |
| 74 |
|
| 75 |
.. code-block:: python |
| 76 |
|
| 77 |
@pytest.fixture(scope="session") |
| 78 |
def db(request): |
| 79 |
# factory will only be invoked once per session - |
| 80 |
db = DataBase() |
| 81 |
request.addfinalizer(db.destroy) # destroy when session is finished |
| 82 |
return db |
| 83 |
|
| 84 |
This factory implementation does not need to call ``cached_setup()`` anymore |
| 85 |
because it will only be invoked once per session. Moreover, the |
| 86 |
``request.addfinalizer()`` registers a finalizer according to the specified |
| 87 |
resource scope on which the factory function is operating. |
| 88 |
|
| 89 |
|
| 90 |
Direct parametrization of funcarg resource factories |
| 91 |
---------------------------------------------------------- |
| 92 |
|
| 93 |
Previously, funcarg factories could not directly cause parametrization. |
| 94 |
You needed to specify a ``@parametrize`` decorator on your test function |
| 95 |
or implement a :hook:`pytest_generate_tests` hook to perform |
| 96 |
parametrization, i.e. calling a test multiple times with different value |
| 97 |
sets. pytest-2.3 introduces a decorator for use on the factory itself: |
| 98 |
|
| 99 |
.. code-block:: python |
| 100 |
|
| 101 |
@pytest.fixture(params=["mysql", "pg"]) |
| 102 |
def db(request): ... # use request.param |
| 103 |
|
| 104 |
Here the factory will be invoked twice (with the respective "mysql" |
| 105 |
and "pg" values set as ``request.param`` attributes) and all of |
| 106 |
the tests requiring "db" will run twice as well. The "mysql" and |
| 107 |
"pg" values will also be used for reporting the test-invocation variants. |
| 108 |
|
| 109 |
This new way of parametrizing funcarg factories should in many cases |
| 110 |
allow to reuse already written factories because effectively |
| 111 |
``request.param`` was already used when test functions/classes were |
| 112 |
parametrized via |
| 113 |
:py:func:`metafunc.parametrize(indirect=True) <pytest.Metafunc.parametrize>` calls. |
| 114 |
|
| 115 |
Of course it's perfectly fine to combine parametrization and scoping: |
| 116 |
|
| 117 |
.. code-block:: python |
| 118 |
|
| 119 |
@pytest.fixture(scope="session", params=["mysql", "pg"]) |
| 120 |
def db(request): |
| 121 |
if request.param == "mysql": |
| 122 |
db = MySQL() |
| 123 |
elif request.param == "pg": |
| 124 |
db = PG() |
| 125 |
request.addfinalizer(db.destroy) # destroy when session is finished |
| 126 |
return db |
| 127 |
|
| 128 |
This would execute all tests requiring the per-session "db" resource twice, |
| 129 |
receiving the values created by the two respective invocations to the |
| 130 |
factory function. |
| 131 |
|
| 132 |
|
| 133 |
No ``pytest_funcarg__`` prefix when using @fixture decorator |
| 134 |
------------------------------------------------------------------- |
| 135 |
|
| 136 |
When using the ``@fixture`` decorator the name of the function |
| 137 |
denotes the name under which the resource can be accessed as a function |
| 138 |
argument: |
| 139 |
|
| 140 |
.. code-block:: python |
| 141 |
|
| 142 |
@pytest.fixture() |
| 143 |
def db(request): ... |
| 144 |
|
| 145 |
The name under which the funcarg resource can be requested is ``db``. |
| 146 |
|
| 147 |
You can still use the "old" non-decorator way of specifying funcarg factories |
| 148 |
aka: |
| 149 |
|
| 150 |
.. code-block:: python |
| 151 |
|
| 152 |
def pytest_funcarg__db(request): ... |
| 153 |
|
| 154 |
|
| 155 |
But it is then not possible to define scoping and parametrization. |
| 156 |
It is thus recommended to use the factory decorator. |
| 157 |
|
| 158 |
|
| 159 |
solving per-session setup / autouse fixtures |
| 160 |
-------------------------------------------------------------- |
| 161 |
|
| 162 |
pytest for a long time offered a pytest_configure and a pytest_sessionstart |
| 163 |
hook which are often used to setup global resources. This suffers from |
| 164 |
several problems: |
| 165 |
|
| 166 |
1. in distributed testing the managing process would setup test resources |
| 167 |
that are never needed because it only coordinates the test run |
| 168 |
activities of the worker processes. |
| 169 |
|
| 170 |
2. if you only perform a collection (with "--collect-only") |
| 171 |
resource-setup will still be executed. |
| 172 |
|
| 173 |
3. If a pytest_sessionstart is contained in some subdirectories |
| 174 |
conftest.py file, it will not be called. This stems from the |
| 175 |
fact that this hook is actually used for reporting, in particular |
| 176 |
the test-header with platform/custom information. |
| 177 |
|
| 178 |
Moreover, it was not easy to define a scoped setup from plugins or |
| 179 |
conftest files other than to implement a ``pytest_runtest_setup()`` hook |
| 180 |
and caring for scoping/caching yourself. And it's virtually impossible |
| 181 |
to do this with parametrization as ``pytest_runtest_setup()`` is called |
| 182 |
during test execution and parametrization happens at collection time. |
| 183 |
|
| 184 |
It follows that pytest_configure/session/runtest_setup are often not |
| 185 |
appropriate for implementing common fixture needs. Therefore, |
| 186 |
pytest-2.3 introduces :ref:`autouse fixtures` which fully |
| 187 |
integrate with the generic :ref:`fixture mechanism <fixture>` |
| 188 |
and obsolete many prior uses of pytest hooks. |
| 189 |
|
| 190 |
funcargs/fixture discovery now happens at collection time |
| 191 |
--------------------------------------------------------------------- |
| 192 |
|
| 193 |
Since pytest-2.3, discovery of fixture/funcarg factories are taken care of |
| 194 |
at collection time. This is more efficient especially for large test suites. |
| 195 |
Moreover, a call to "pytest --collect-only" should be able to in the future |
| 196 |
show a lot of setup-information and thus presents a nice method to get an |
| 197 |
overview of fixture management in your project. |
| 198 |
|
| 199 |
.. _`compatibility notes`: |
| 200 |
|
| 201 |
.. _`funcargscompat`: |
| 202 |
|
| 203 |
Conclusion and compatibility notes |
| 204 |
--------------------------------------------------------- |
| 205 |
|
| 206 |
**funcargs** were originally introduced to pytest-2.0. In pytest-2.3 |
| 207 |
the mechanism was extended and refined and is now described as |
| 208 |
fixtures: |
| 209 |
|
| 210 |
* previously funcarg factories were specified with a special |
| 211 |
``pytest_funcarg__NAME`` prefix instead of using the |
| 212 |
``@pytest.fixture`` decorator. |
| 213 |
|
| 214 |
* Factories received a ``request`` object which managed caching through |
| 215 |
``request.cached_setup()`` calls and allowed using other funcargs via |
| 216 |
``request.getfuncargvalue()`` calls. These intricate APIs made it hard |
| 217 |
to do proper parametrization and implement resource caching. The |
| 218 |
new :py:func:`pytest.fixture` decorator allows to declare the scope |
| 219 |
and let pytest figure things out for you. |
| 220 |
|
| 221 |
* if you used parametrization and funcarg factories which made use of |
| 222 |
``request.cached_setup()`` it is recommended to invest a few minutes |
| 223 |
and simplify your fixture function code to use the :ref:`@pytest.fixture` |
| 224 |
decorator instead. This will also allow to take advantage of |
| 225 |
the automatic per-resource grouping of tests. |