| 1 |
.. _about-fixtures: |
| 2 |
|
| 3 |
About fixtures |
| 4 |
=============== |
| 5 |
|
| 6 |
.. seealso:: :ref:`how-to-fixtures` |
| 7 |
.. seealso:: :ref:`Fixtures reference <reference-fixtures>` |
| 8 |
|
| 9 |
pytest fixtures are designed to be explicit, modular and scalable. |
| 10 |
|
| 11 |
What fixtures are |
| 12 |
----------------- |
| 13 |
|
| 14 |
In testing, a `fixture <https://en.wikipedia.org/wiki/Test_fixture#Software>`_ |
| 15 |
provides a defined, reliable and consistent context for the tests. This could |
| 16 |
include environment (for example a database configured with known parameters) |
| 17 |
or content (such as a dataset). |
| 18 |
|
| 19 |
Fixtures define the steps and data that constitute the *arrange* phase of a |
| 20 |
test (see :ref:`test-anatomy`). In pytest, they are functions you define that |
| 21 |
serve this purpose. They can also be used to define a test's *act* phase; this |
| 22 |
is a powerful technique for designing more complex tests. |
| 23 |
|
| 24 |
The services, state, or other operating environments set up by fixtures are |
| 25 |
accessed by test functions through arguments. For each fixture used by a test |
| 26 |
function there is typically a parameter (named after the fixture) in the test |
| 27 |
function's definition. |
| 28 |
|
| 29 |
We can tell pytest that a particular function is a fixture by decorating it with |
| 30 |
:py:func:`@pytest.fixture <pytest.fixture>`. Here's a simple example of |
| 31 |
what a fixture in pytest might look like: |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
import pytest |
| 36 |
|
| 37 |
|
| 38 |
class Fruit: |
| 39 |
def __init__(self, name): |
| 40 |
self.name = name |
| 41 |
|
| 42 |
def __eq__(self, other): |
| 43 |
return self.name == other.name |
| 44 |
|
| 45 |
|
| 46 |
@pytest.fixture |
| 47 |
def my_fruit(): |
| 48 |
return Fruit("apple") |
| 49 |
|
| 50 |
|
| 51 |
@pytest.fixture |
| 52 |
def fruit_basket(my_fruit): |
| 53 |
return [Fruit("banana"), my_fruit] |
| 54 |
|
| 55 |
|
| 56 |
def test_my_fruit_in_basket(my_fruit, fruit_basket): |
| 57 |
assert my_fruit in fruit_basket |
| 58 |
|
| 59 |
Tests don't have to be limited to a single fixture, either. They can depend on |
| 60 |
as many fixtures as you want, and fixtures can use other fixtures, as well. This |
| 61 |
is where pytest's fixture system really shines. |
| 62 |
|
| 63 |
|
| 64 |
Improvements over xUnit-style setup/teardown functions |
| 65 |
----------------------------------------------------------- |
| 66 |
|
| 67 |
pytest fixtures offer dramatic improvements over the classic xUnit |
| 68 |
style of setup/teardown functions: |
| 69 |
|
| 70 |
* fixtures have explicit names and are activated by declaring their use |
| 71 |
from test functions, modules, classes or whole projects. |
| 72 |
|
| 73 |
* fixtures are implemented in a modular manner, as each fixture name |
| 74 |
triggers a *fixture function* which can itself use other fixtures. |
| 75 |
|
| 76 |
* fixture management scales from simple unit to complex |
| 77 |
functional testing, allowing to parametrize fixtures and tests according |
| 78 |
to configuration and component options, or to reuse fixtures |
| 79 |
across function, class, module or whole test session scopes. |
| 80 |
|
| 81 |
* teardown logic can be easily, and safely managed, no matter how many fixtures |
| 82 |
are used, without the need to carefully handle errors by hand or micromanage |
| 83 |
the order that cleanup steps are added. |
| 84 |
|
| 85 |
In addition, pytest continues to support :ref:`xunitsetup`. You can mix |
| 86 |
both styles, moving incrementally from classic to new style, as you |
| 87 |
prefer. You can also start out from existing :ref:`unittest.TestCase |
| 88 |
style <unittest.TestCase>`. |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
Fixture errors |
| 93 |
-------------- |
| 94 |
|
| 95 |
pytest does its best to put all the fixtures for a given test in a linear order |
| 96 |
so that it can see which fixture happens first, second, third, and so on. If an |
| 97 |
earlier fixture has a problem, though, and raises an exception, pytest will stop |
| 98 |
executing fixtures for that test and mark the test as having an error. |
| 99 |
|
| 100 |
When a test is marked as having an error, it doesn't mean the test failed, |
| 101 |
though. It just means the test couldn't even be attempted because one of the |
| 102 |
things it depends on had a problem. |
| 103 |
|
| 104 |
This is one reason why it's a good idea to cut out as many unnecessary |
| 105 |
dependencies as possible for a given test. That way a problem in something |
| 106 |
unrelated isn't causing us to have an incomplete picture of what may or may not |
| 107 |
have issues. |
| 108 |
|
| 109 |
Here's a quick example to help explain: |
| 110 |
|
| 111 |
.. code-block:: python |
| 112 |
|
| 113 |
import pytest |
| 114 |
|
| 115 |
|
| 116 |
@pytest.fixture |
| 117 |
def order(): |
| 118 |
return [] |
| 119 |
|
| 120 |
|
| 121 |
@pytest.fixture |
| 122 |
def append_first(order): |
| 123 |
order.append(1) |
| 124 |
|
| 125 |
|
| 126 |
@pytest.fixture |
| 127 |
def append_second(order, append_first): |
| 128 |
order.extend([2]) |
| 129 |
|
| 130 |
|
| 131 |
@pytest.fixture(autouse=True) |
| 132 |
def append_third(order, append_second): |
| 133 |
order += [3] |
| 134 |
|
| 135 |
|
| 136 |
def test_order(order): |
| 137 |
assert order == [1, 2, 3] |
| 138 |
|
| 139 |
|
| 140 |
If, for whatever reason, ``order.append(1)`` had a bug and it raises an exception, |
| 141 |
we wouldn't be able to know if ``order.extend([2])`` or ``order += [3]`` would |
| 142 |
also have problems. After ``append_first`` throws an exception, pytest won't run |
| 143 |
any more fixtures for ``test_order``, and it won't even try to run |
| 144 |
``test_order`` itself. The only things that would've run would be ``order`` and |
| 145 |
``append_first``. |
| 146 |
|
| 147 |
|
| 148 |
Sharing test data |
| 149 |
----------------- |
| 150 |
|
| 151 |
If you want to make test data from files available to your tests, a good way |
| 152 |
to do this is by loading these data in a fixture for use by your tests. |
| 153 |
This makes use of the automatic caching mechanisms of pytest. |
| 154 |
|
| 155 |
Another good approach is by adding the data files in the ``tests`` directory. |
| 156 |
There are also community plugins available to help to manage this aspect of |
| 157 |
testing, e.g. :pypi:`pytest-datadir` and :pypi:`pytest-datafiles`. |
| 158 |
|
| 159 |
.. _fixtures-signal-cleanup: |
| 160 |
|
| 161 |
A note about fixture cleanup |
| 162 |
---------------------------- |
| 163 |
|
| 164 |
pytest does not do any special processing for :data:`SIGTERM <signal.SIGTERM>` and |
| 165 |
``SIGQUIT`` signals (:data:`SIGINT <signal.SIGINT>` is handled naturally |
| 166 |
by the Python runtime via :class:`KeyboardInterrupt`), so fixtures that manage external resources which are important |
| 167 |
to be cleared when the Python process is terminated (by those signals) might leak resources. |
| 168 |
|
| 169 |
The reason pytest does not handle those signals to perform fixture cleanup is that signal handlers are global, |
| 170 |
and changing them might interfere with the code under execution. |
| 171 |
|
| 172 |
If fixtures in your suite need special care regarding termination in those scenarios, |
| 173 |
see :issue:`this comment <5243#issuecomment-491522595>` in the issue |
| 174 |
tracker for a possible workaround. |