| 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 :option:`-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 configuration file like this: |
| 38 |
|
| 39 |
.. tab:: toml |
| 40 |
|
| 41 |
.. code-block:: toml |
| 42 |
|
| 43 |
[pytest] |
| 44 |
markers = [ |
| 45 |
"slow: marks tests as slow (deselect with '-m \"not slow\"')", |
| 46 |
"serial", |
| 47 |
] |
| 48 |
|
| 49 |
.. tab:: ini |
| 50 |
|
| 51 |
.. code-block:: ini |
| 52 |
|
| 53 |
[pytest] |
| 54 |
markers = |
| 55 |
slow: marks tests as slow (deselect with '-m "not slow"') |
| 56 |
serial |
| 57 |
|
| 58 |
Note that everything past the ``:`` after the mark name is an optional description. |
| 59 |
|
| 60 |
Alternatively, you can register new markers programmatically in a |
| 61 |
:ref:`pytest_configure <initialization-hooks>` hook: |
| 62 |
|
| 63 |
.. code-block:: python |
| 64 |
|
| 65 |
def pytest_configure(config): |
| 66 |
config.addinivalue_line( |
| 67 |
"markers", "env(name): mark test to run only on named environment" |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
Registered marks appear in pytest's help text and do not emit warnings (see the next section). It |
| 72 |
is recommended that third-party plugins always :ref:`register their markers <registering-markers>`. |
| 73 |
|
| 74 |
.. _unknown-marks: |
| 75 |
|
| 76 |
Raising errors on unknown marks |
| 77 |
------------------------------- |
| 78 |
|
| 79 |
Unregistered marks applied with the ``@pytest.mark.name_of_the_mark`` decorator |
| 80 |
will always emit a warning in order to avoid silently doing something |
| 81 |
surprising due to mistyped names. As described in the previous section, you can disable |
| 82 |
the warning for custom marks by registering them in your configuration file or |
| 83 |
using a custom ``pytest_configure`` hook. |
| 84 |
|
| 85 |
When the :confval:`strict_markers` configuration option is set, any unknown marks applied |
| 86 |
with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error. You can |
| 87 |
enforce this validation in your project by setting :confval:`strict_markers` in your configuration: |
| 88 |
|
| 89 |
.. tab:: toml |
| 90 |
|
| 91 |
.. code-block:: toml |
| 92 |
|
| 93 |
[pytest] |
| 94 |
addopts = ["--strict-markers"] |
| 95 |
markers = [ |
| 96 |
"slow: marks tests as slow (deselect with '-m \"not slow\"')", |
| 97 |
"serial", |
| 98 |
] |
| 99 |
|
| 100 |
.. tab:: ini |
| 101 |
|
| 102 |
.. code-block:: ini |
| 103 |
|
| 104 |
[pytest] |
| 105 |
strict_markers = true |
| 106 |
markers = |
| 107 |
slow: marks tests as slow (deselect with '-m "not slow"') |
| 108 |
serial |