2009-10-23 02:57:21 +08:00
2010-10-11 05:45:45 +08:00
.. _mark:
2011-09-06 17:43:42 +08:00
Marking test functions with attributes
2010-10-13 18:26:14 +08:00
=================================================================
2010-01-13 23:00:33 +08:00
2010-10-14 01:30:00 +08:00
2011-11-12 06:56:06 +08:00
By using the `` pytest.mark `` helper you can easily set
2011-11-19 02:32:11 +08:00
metadata on your test functions. There are
2011-11-12 06:56:06 +08:00
some builtin markers, for example:
2009-10-23 02:57:21 +08:00
2017-08-04 09:00:11 +08:00
* :ref: `skip <skip>` - always skip a test function
2011-11-17 19:09:21 +08:00
* :ref: `skipif <skipif>` - skip a test function if a certain condition is met
* :ref: `xfail <xfail>` - produce an "expected failure" outcome if a certain
2011-11-12 06:56:06 +08:00
condition is met
2011-11-17 19:09:21 +08:00
* :ref: `parametrize <parametrizemark>` to perform multiple calls
to the same test function.
2011-11-12 06:56:06 +08:00
2011-11-19 02:32:11 +08:00
It's easy to create custom markers or to apply markers
to whole test classes or modules. See :ref: `mark examples` for examples
which also serve as documentation.
2011-11-12 06:56:06 +08:00
2015-08-28 07:11:07 +08:00
.. note ::
Marks can only be applied to tests, having no effect on
:ref: `fixtures <fixtures>` .
2010-10-14 01:30:00 +08:00
2018-06-09 22:59:02 +08:00
Raising errors on unknown marks: --strict
-----------------------------------------
2018-06-09 23:40:39 +08:00
When the `` --strict `` command-line flag is passed, any marks not registered in the `` pytest.ini `` file will trigger an error.
2018-06-09 22:59:02 +08:00
Marks can be registered like this:
.. code-block :: ini
[pytest]
markers =
slow
serial
2018-06-09 23:40:39 +08:00
This can be used to prevent users mistyping mark names by accident. Test suites that want to enforce this
2018-06-09 22:59:02 +08:00
should add `` --strict `` to `` addopts `` :
.. code-block :: ini
[pytest]
addopts = --strict
markers =
slow
serial
2018-04-05 21:30:31 +08:00
2018-05-02 06:27:53 +08:00
Marker revamp and iteration
---------------------------
2018-04-05 21:30:31 +08:00
.. versionadded :: 3.6
2018-07-17 23:19:40 +08:00
pytest's marker implementation traditionally worked by simply updating the `` __dict__ `` attribute of functions to cumulatively add markers. As a result, markers would unintentionally be passed along class hierarchies in surprising ways. Further, the API for retrieving them was inconsistent, as markers from parameterization would be stored differently than markers applied using the `` @pytest.mark `` decorator and markers added via `` node.add_marker `` .
2018-04-05 21:30:31 +08:00
2018-04-06 00:46:33 +08:00
This state of things made it technically next to impossible to use data from markers correctly without having a deep understanding of the internals, leading to subtle and hard to understand bugs in more advanced usages.
2018-04-06 01:45:30 +08:00
Depending on how a marker got declared/changed one would get either a `` MarkerInfo `` which might contain markers from sibling classes,
2018-05-31 23:43:48 +08:00
`` MarkDecorators `` when marks came from parameterization or from a `` node.add_marker `` call, discarding prior marks. Also `` MarkerInfo `` acts like a single mark, when it in fact represents a merged view on multiple marks with the same name.
2018-04-06 00:46:33 +08:00
2018-07-17 23:19:40 +08:00
On top of that markers were not accessible the same way for modules, classes, and functions/methods.
In fact, markers were only accessible in functions, even if they were declared on classes/modules.
2018-04-05 21:30:31 +08:00
2018-04-05 23:39:21 +08:00
A new API to access markers has been introduced in pytest 3.6 in order to solve the problems with the initial design, providing :func: `_pytest.nodes.Node.iter_markers` method to iterate over markers in a consistent manner and reworking the internals, which solved great deal of problems with the initial design.
2018-04-05 21:30:31 +08:00
2018-05-16 08:47:40 +08:00
.. _update marker code:
2018-05-16 08:35:27 +08:00
Updating code
~~~~~~~~~~~~~
The old `` Node.get_marker(name) `` function is considered deprecated because it returns an internal `` MarkerInfo `` object
2018-06-11 19:28:09 +08:00
which contains the merged name, `` *args `` and `` **kwargs `` of all the markers which apply to that node.
2018-05-16 08:35:27 +08:00
In general there are two scenarios on how markers should be handled:
1. Marks overwrite each other. Order matters but you only want to think of your mark as a single item. E.g.
`` log_level('info') `` at a module level can be overwritten by `` log_level('debug') `` for a specific test.
2018-07-17 23:19:40 +08:00
In this case, use `` Node.get_closest_marker(name) `` :
2018-05-16 08:35:27 +08:00
.. code-block :: python
# replace this:
2018-06-03 11:29:28 +08:00
marker = item.get_marker("log_level")
2018-05-16 08:35:27 +08:00
if marker:
level = marker.args[0]
# by this:
2018-06-03 11:29:28 +08:00
marker = item.get_closest_marker("log_level")
2018-05-16 08:35:27 +08:00
if marker:
level = marker.args[0]
2018-07-17 23:19:40 +08:00
2. Marks compose in an additive manner. E.g. `` skipif(condition) `` marks mean you just want to evaluate all of them,
2018-05-16 08:35:27 +08:00
order doesn't even matter. You probably want to think of your marks as a set here.
2018-04-10 18:57:04 +08:00
2018-05-16 08:35:27 +08:00
In this case iterate over each mark and handle their `` *args `` and `` **kwargs `` individually.
.. code-block :: python
# replace this
2018-06-03 11:29:28 +08:00
skipif = item.get_marker("skipif")
2018-05-16 08:35:27 +08:00
if skipif:
for condition in skipif.args:
# eval condition
2018-06-03 11:19:17 +08:00
...
2018-05-16 08:35:27 +08:00
# by this:
2018-06-03 11:29:28 +08:00
for skipif in item.iter_markers("skipif"):
2018-05-16 08:35:27 +08:00
condition = skipif.args[0]
# eval condition
If you are unsure or have any questions, please consider opening
`an issue <https://github.com/pytest-dev/pytest/issues> `_ .
Related issues
~~~~~~~~~~~~~~
Here is a non-exhaustive list of issues fixed by the new implementation:
2018-04-10 18:57:04 +08:00
* Marks don't pick up nested classes (`#199 <https://github.com/pytest-dev/pytest/issues/199> `_ ).
2018-07-17 23:19:40 +08:00
* Markers stain on all related classes (`#568 <https://github.com/pytest-dev/pytest/issues/568> `_ ).
2018-04-10 18:57:04 +08:00
2018-07-17 23:19:40 +08:00
* Combining marks - args and kwargs calculation (`#2897 <https://github.com/pytest-dev/pytest/issues/2897> `_ ).
2018-04-10 18:57:04 +08:00
* `` request.node.get_marker('name') `` returns `` None `` for markers applied in classes (`#902 <https://github.com/pytest-dev/pytest/issues/902> `_ ).
2018-07-17 23:19:40 +08:00
* Marks applied in parametrize are stored as markdecorator (`#2400 <https://github.com/pytest-dev/pytest/issues/2400> `_ ).
2018-04-10 18:57:04 +08:00
2018-07-17 23:19:40 +08:00
* Fix marker interaction in a backward incompatible way (`#1670 <https://github.com/pytest-dev/pytest/issues/1670> `_ ).
2018-04-10 18:57:04 +08:00
* Refactor marks to get rid of the current "marks transfer" mechanism (`#2363 <https://github.com/pytest-dev/pytest/issues/2363> `_ ).
* Introduce FunctionDefinition node, use it in generate_tests (`#2522 <https://github.com/pytest-dev/pytest/issues/2522> `_ ).
2018-07-17 23:19:40 +08:00
* Remove named marker attributes and collect markers in items (`#891 <https://github.com/pytest-dev/pytest/issues/891> `_ ).
2018-04-10 18:57:04 +08:00
* skipif mark from parametrize hides module level skipif mark (`#1540 <https://github.com/pytest-dev/pytest/issues/1540> `_ ).
* skipif + parametrize not skipping tests (`#1296 <https://github.com/pytest-dev/pytest/issues/1296> `_ ).
2018-07-17 23:19:40 +08:00
* Marker transfer incompatible with inheritance (`#535 <https://github.com/pytest-dev/pytest/issues/535> `_ ).
2018-04-10 18:57:04 +08:00
More details can be found in the `original PR <https://github.com/pytest-dev/pytest/pull/3317> `_ .
2018-04-05 23:39:21 +08:00
.. note ::
2018-04-05 21:30:31 +08:00
2018-06-03 11:19:17 +08:00
in a future major relase of pytest we will introduce class based markers,
at which points markers will no longer be limited to instances of :py:class: `Mark`