From 7e8044f9b819e1b137518f5a5051f983cd1fd824 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 14 May 2019 20:49:47 -0300 Subject: [PATCH 1/4] Revamp the mark section in the docs Add an introductory section about how to register marks, including doing so programatically (#5255). --- doc/en/mark.rst | 51 +++++++++++++++++++++++++++++--------------- doc/en/reference.rst | 2 ++ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/doc/en/mark.rst b/doc/en/mark.rst index 05ffdb36c..2f96ee3ae 100644 --- a/doc/en/mark.rst +++ b/doc/en/mark.rst @@ -24,40 +24,57 @@ which also serve as documentation. :ref:`fixtures `. -.. _unknown-marks: +Registering marks +----------------- -Raising errors on unknown marks -------------------------------- - -Unknown marks applied with the ``@pytest.mark.name_of_the_mark`` decorator -will always emit a warning, in order to avoid silently doing something -surprising due to mis-typed names. You can disable the warning for custom -marks by registering them in ``pytest.ini`` like this: +You can register custom marks in your ``pytest.ini`` file like this: .. code-block:: ini [pytest] markers = - slow + slow: marks tests as slow (deselect with '-m "not slow"') serial +Note that everything after the ``:`` is an optional description. + +Alternatively, you can register new markers programatically in a +:ref:`pytest_configure ` hook: + +.. code-block:: python + + def pytest_configure(config): + config.addinivalue_line( + "markers", "env(name): mark test to run only on named environment" + ) + + +Registered marks appear in pytest's help text and do not emit warnings (see the next section). It +is recommended that third-party plugins always :ref:`register their markers `. + +.. _unknown-marks: + +Raising errors on unknown marks +------------------------------- + +Unregistered marks applied with the ``@pytest.mark.name_of_the_mark`` decorator +will always emit a warning in order to avoid silently doing something +surprising due to mis-typed names. As described in the previous section, you can disable +the warning for custom marks by registering them in your ``pytest.ini`` file or +using a custom ``pytest_configure`` hook. + When the ``--strict-markers`` command-line flag is passed, any unknown marks applied -with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error. -Marks added by pytest or by a plugin instead of the decorator will not trigger -this error. To enforce validation of markers, add ``--strict-markers`` to ``addopts``: +with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error. You can +enforce this validation in your project by adding ``--strict-markers`` to ``addopts``: .. code-block:: ini [pytest] addopts = --strict-markers markers = - slow + slow: marks tests as slow (deselect with '-m "not slow"') serial -Third-party plugins should always :ref:`register their markers ` -so that they appear in pytest's help text and do not emit warnings. - - .. _marker-revamp: Marker revamp and iteration diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 61f8034ed..f76fc7765 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -581,6 +581,8 @@ Bootstrapping hooks called for plugins registered early enough (internal and set .. autofunction:: pytest_cmdline_parse .. autofunction:: pytest_cmdline_main +.. _`initialization-hooks`: + Initialization hooks ~~~~~~~~~~~~~~~~~~~~ From a31098a74e69d72db6d9bf5b0ba1680fcdc5c265 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 14 May 2019 20:52:59 -0300 Subject: [PATCH 2/4] Move section about mark revamp and iteration to historical notes This has been in place for a long time now, since 3.6. --- doc/en/historical-notes.rst | 108 ++++++++++++++++++++++++++++++++++++ doc/en/mark.rst | 106 ----------------------------------- 2 files changed, 108 insertions(+), 106 deletions(-) diff --git a/doc/en/historical-notes.rst b/doc/en/historical-notes.rst index 145dc4529..c06796801 100644 --- a/doc/en/historical-notes.rst +++ b/doc/en/historical-notes.rst @@ -4,6 +4,114 @@ Historical Notes This page lists features or behavior from previous versions of pytest which have changed over the years. They are kept here as a historical note so users looking at old code can find documentation related to them. + +.. _marker-revamp: + +Marker revamp and iteration +--------------------------- + +.. versionchanged:: 3.6 + +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``. + +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. + +Depending on how a marker got declared/changed one would get either a ``MarkerInfo`` which might contain markers from sibling classes, +``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. + +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. + +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. + + +.. _update marker code: + +Updating code +~~~~~~~~~~~~~ + +The old ``Node.get_marker(name)`` function is considered deprecated because it returns an internal ``MarkerInfo`` object +which contains the merged name, ``*args`` and ``**kwargs`` of all the markers which apply to that node. + +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. + + In this case, use ``Node.get_closest_marker(name)``: + + .. code-block:: python + + # replace this: + marker = item.get_marker("log_level") + if marker: + level = marker.args[0] + + # by this: + marker = item.get_closest_marker("log_level") + if marker: + level = marker.args[0] + +2. Marks compose in an additive manner. E.g. ``skipif(condition)`` marks mean you just want to evaluate all of them, +order doesn't even matter. You probably want to think of your marks as a set here. + + In this case iterate over each mark and handle their ``*args`` and ``**kwargs`` individually. + + .. code-block:: python + + # replace this + skipif = item.get_marker("skipif") + if skipif: + for condition in skipif.args: + # eval condition + ... + + # by this: + for skipif in item.iter_markers("skipif"): + condition = skipif.args[0] + # eval condition + + +If you are unsure or have any questions, please consider opening +`an issue `_. + +Related issues +~~~~~~~~~~~~~~ + +Here is a non-exhaustive list of issues fixed by the new implementation: + +* Marks don't pick up nested classes (`#199 `_). + +* Markers stain on all related classes (`#568 `_). + +* Combining marks - args and kwargs calculation (`#2897 `_). + +* ``request.node.get_marker('name')`` returns ``None`` for markers applied in classes (`#902 `_). + +* Marks applied in parametrize are stored as markdecorator (`#2400 `_). + +* Fix marker interaction in a backward incompatible way (`#1670 `_). + +* Refactor marks to get rid of the current "marks transfer" mechanism (`#2363 `_). + +* Introduce FunctionDefinition node, use it in generate_tests (`#2522 `_). + +* Remove named marker attributes and collect markers in items (`#891 `_). + +* skipif mark from parametrize hides module level skipif mark (`#1540 `_). + +* skipif + parametrize not skipping tests (`#1296 `_). + +* Marker transfer incompatible with inheritance (`#535 `_). + +More details can be found in the `original PR `_. + +.. note:: + + in a future major relase of pytest we will introduce class based markers, + at which point markers will no longer be limited to instances of :py:class:`Mark`. + + cache plugin integrated into the core ------------------------------------- diff --git a/doc/en/mark.rst b/doc/en/mark.rst index 2f96ee3ae..27a3c789c 100644 --- a/doc/en/mark.rst +++ b/doc/en/mark.rst @@ -74,109 +74,3 @@ enforce this validation in your project by adding ``--strict-markers`` to ``addo markers = slow: marks tests as slow (deselect with '-m "not slow"') serial - -.. _marker-revamp: - -Marker revamp and iteration ---------------------------- - - - -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``. - -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. - -Depending on how a marker got declared/changed one would get either a ``MarkerInfo`` which might contain markers from sibling classes, -``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. - -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. - -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. - - -.. _update marker code: - -Updating code -~~~~~~~~~~~~~ - -The old ``Node.get_marker(name)`` function is considered deprecated because it returns an internal ``MarkerInfo`` object -which contains the merged name, ``*args`` and ``**kwargs`` of all the markers which apply to that node. - -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. - - In this case, use ``Node.get_closest_marker(name)``: - - .. code-block:: python - - # replace this: - marker = item.get_marker("log_level") - if marker: - level = marker.args[0] - - # by this: - marker = item.get_closest_marker("log_level") - if marker: - level = marker.args[0] - -2. Marks compose in an additive manner. E.g. ``skipif(condition)`` marks mean you just want to evaluate all of them, -order doesn't even matter. You probably want to think of your marks as a set here. - - In this case iterate over each mark and handle their ``*args`` and ``**kwargs`` individually. - - .. code-block:: python - - # replace this - skipif = item.get_marker("skipif") - if skipif: - for condition in skipif.args: - # eval condition - ... - - # by this: - for skipif in item.iter_markers("skipif"): - condition = skipif.args[0] - # eval condition - - -If you are unsure or have any questions, please consider opening -`an issue `_. - -Related issues -~~~~~~~~~~~~~~ - -Here is a non-exhaustive list of issues fixed by the new implementation: - -* Marks don't pick up nested classes (`#199 `_). - -* Markers stain on all related classes (`#568 `_). - -* Combining marks - args and kwargs calculation (`#2897 `_). - -* ``request.node.get_marker('name')`` returns ``None`` for markers applied in classes (`#902 `_). - -* Marks applied in parametrize are stored as markdecorator (`#2400 `_). - -* Fix marker interaction in a backward incompatible way (`#1670 `_). - -* Refactor marks to get rid of the current "marks transfer" mechanism (`#2363 `_). - -* Introduce FunctionDefinition node, use it in generate_tests (`#2522 `_). - -* Remove named marker attributes and collect markers in items (`#891 `_). - -* skipif mark from parametrize hides module level skipif mark (`#1540 `_). - -* skipif + parametrize not skipping tests (`#1296 `_). - -* Marker transfer incompatible with inheritance (`#535 `_). - -More details can be found in the `original PR `_. - -.. note:: - - in a future major relase of pytest we will introduce class based markers, - at which point markers will no longer be limited to instances of :py:class:`Mark`. From c6e3ff3ce5ce7d7759f9750a572fb504e1680724 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 14 May 2019 20:58:41 -0300 Subject: [PATCH 3/4] Mention "-m" in the main mark docs --- doc/en/example/markers.rst | 2 ++ doc/en/mark.rst | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/en/example/markers.rst b/doc/en/example/markers.rst index f143e448c..0c3df0497 100644 --- a/doc/en/example/markers.rst +++ b/doc/en/example/markers.rst @@ -6,6 +6,8 @@ Working with custom markers Here are some example using the :ref:`mark` mechanism. +.. _`mark run`: + Marking test functions and selecting them for a run ---------------------------------------------------- diff --git a/doc/en/mark.rst b/doc/en/mark.rst index 27a3c789c..de6ab7822 100644 --- a/doc/en/mark.rst +++ b/doc/en/mark.rst @@ -15,8 +15,10 @@ some builtin markers, for example: to the same test function. 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. +to whole test classes or modules. Those markers can be used by plugins, and also +are commonly used to :ref:`select tests ` on the command-line with the ``-m`` option. + +See :ref:`mark examples` for examples which also serve as documentation. .. note:: From e44a2ef6530ab762d64b97a583accdbb3739f1a3 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 15 May 2019 20:43:07 -0300 Subject: [PATCH 4/4] Apply suggestions from code review Co-Authored-By: Daniel Hahler --- doc/en/example/markers.rst | 2 +- doc/en/historical-notes.rst | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/en/example/markers.rst b/doc/en/example/markers.rst index 0c3df0497..b8d8bef0c 100644 --- a/doc/en/example/markers.rst +++ b/doc/en/example/markers.rst @@ -4,7 +4,7 @@ Working with custom markers ================================================= -Here are some example using the :ref:`mark` mechanism. +Here are some examples using the :ref:`mark` mechanism. .. _`mark run`: diff --git a/doc/en/historical-notes.rst b/doc/en/historical-notes.rst index c06796801..c8403728f 100644 --- a/doc/en/historical-notes.rst +++ b/doc/en/historical-notes.rst @@ -19,10 +19,13 @@ This state of things made it technically next to impossible to use data from mar Depending on how a marker got declared/changed one would get either a ``MarkerInfo`` which might contain markers from sibling classes, ``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. -On top of that markers were not accessible the same way for modules, classes, and functions/methods. +On top of that markers were not accessible in 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. -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. +A new API to access markers has been introduced in pytest 3.6 in order to solve the problems with +the initial design, providing the :func:`_pytest.nodes.Node.iter_markers` method to iterate over +markers in a consistent manner and reworking the internals, which solved a great deal of problems +with the initial design. .. _update marker code: