| 1 |
.. _mark: |
| 2 |
|
| 3 |
How to mark test functions with attributes |
| 4 |
=========================================== |
| 5 |
|
| 6 |
By using the ``pytest.mark`` helper you can easily set |
| 7 |
metadata on your test functions. You can find the full list of builtin markers |
| 8 |
in the :ref:`API Reference<marks ref>`. Or you can list all the markers, including |
| 9 |
builtin and custom, using the CLI - :code:`pytest --markers`. |
| 10 |
|
| 11 |
Here are some of the builtin markers: |
| 12 |
|
| 13 |
* :ref:`usefixtures <usefixtures>` - use fixtures on a test function or class |
| 14 |
* :ref:`filterwarnings <filterwarnings>` - filter certain warnings of a test function |
| 15 |
* :ref:`skip <skip>` - always skip a test function |
| 16 |
* :ref:`skipif <skipif>` - skip a test function if a certain condition is met |
| 17 |
* :ref:`xfail <xfail>` - produce an "expected failure" outcome if a certain |
| 18 |
condition is met |
| 19 |
* :ref:`parametrize <parametrizemark>` - perform multiple calls |
| 20 |
to the same test function. |
| 21 |
|
| 22 |
It's easy to create custom markers or to apply markers |
| 23 |
to whole test classes or modules. Those markers can be used by plugins, and also |
| 24 |
are commonly used to :ref:`select tests <mark run>` on the command-line with the ``-m`` option. |
| 25 |
|
| 26 |
See :ref:`mark examples` for examples which also serve as documentation. |
| 27 |
|
| 28 |
.. note:: |
| 29 |
|
| 30 |
Marks can only be applied to tests, having no effect on |
| 31 |
:ref:`fixtures <fixtures>`. |
| 32 |
|
| 33 |
|
| 34 |
Registering marks |
| 35 |
----------------- |
| 36 |
|
| 37 |
You can register custom marks in your ``pytest.ini`` file like this: |
| 38 |
|
| 39 |
.. code-block:: ini |
| 40 |
|
| 41 |
[pytest] |
| 42 |
markers = |
| 43 |
slow: marks tests as slow (deselect with '-m "not slow"') |
| 44 |
serial |
| 45 |
|
| 46 |
or in your ``pyproject.toml`` file like this: |
| 47 |
|
| 48 |
.. code-block:: toml |
| 49 |
|
| 50 |
[tool.pytest.ini_options] |
| 51 |
markers = [ |
| 52 |
"slow: marks tests as slow (deselect with '-m \"not slow\"')", |
| 53 |
"serial", |
| 54 |
] |
| 55 |
|
| 56 |
Note that everything past the ``:`` after the mark name is an optional description. |
| 57 |
|
| 58 |
Alternatively, you can register new markers programmatically in a |
| 59 |
:ref:`pytest_configure <initialization-hooks>` hook: |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
def pytest_configure(config): |
| 64 |
config.addinivalue_line( |
| 65 |
"markers", "env(name): mark test to run only on named environment" |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
Registered marks appear in pytest's help text and do not emit warnings (see the next section). It |
| 70 |
is recommended that third-party plugins always :ref:`register their markers <registering-markers>`. |
| 71 |
|
| 72 |
.. _unknown-marks: |
| 73 |
|
| 74 |
Raising errors on unknown marks |
| 75 |
------------------------------- |
| 76 |
|
| 77 |
Unregistered marks applied with the ``@pytest.mark.name_of_the_mark`` decorator |
| 78 |
will always emit a warning in order to avoid silently doing something |
| 79 |
surprising due to mistyped names. As described in the previous section, you can disable |
| 80 |
the warning for custom marks by registering them in your ``pytest.ini`` file or |
| 81 |
using a custom ``pytest_configure`` hook. |
| 82 |
|
| 83 |
When the ``--strict-markers`` command-line flag is passed, any unknown marks applied |
| 84 |
with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error. You can |
| 85 |
enforce this validation in your project by adding ``--strict-markers`` to ``addopts``: |
| 86 |
|
| 87 |
.. code-block:: ini |
| 88 |
|
| 89 |
[pytest] |
| 90 |
addopts = --strict-markers |
| 91 |
markers = |
| 92 |
slow: marks tests as slow (deselect with '-m "not slow"') |
| 93 |
serial |