Skip to content

Commit 067328a

Browse files
committed
Expand docs about registering markers
1 parent 4f2ace8 commit 067328a

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

changelog/4826.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A warning is now emitted when unknown marks are used as a decorator.
2+
This is often due to a typo, which can lead to silently broken tests.

doc/en/mark.rst

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ which also serve as documentation.
2626
:ref:`fixtures <fixtures>`.
2727

2828

29-
Raising errors on unknown marks: --strict
30-
-----------------------------------------
29+
.. _unknown-marks:
3130

32-
When the ``--strict`` command-line flag is passed, any unknown marks applied
33-
with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error.
34-
Marks defined or added by pytest or by a plugin will not trigger an error.
31+
Raising errors on unknown marks
32+
-------------------------------
3533

36-
Marks can be registered in ``pytest.ini`` like this:
34+
Unknown marks applied with the ``@pytest.mark.name_of_the_mark`` decorator
35+
will always emit a warning, in order to avoid silently doing something
36+
surprising due to mis-typed names. You can disable the warning for custom
37+
marks by registering them in ``pytest.ini`` like this:
3738

3839
.. code-block:: ini
3940
@@ -42,8 +43,11 @@ Marks can be registered in ``pytest.ini`` like this:
4243
slow
4344
serial
4445
45-
This can be used to prevent users mistyping mark names by accident. Test suites that want to enforce this
46-
should add ``--strict`` to ``addopts``:
46+
When the ``--strict`` command-line flag is passed, any unknown marks applied
47+
with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error.
48+
Marks added by pytest or by a plugin instead of the decorator will not trigger
49+
the warning or this error. Test suites that want to enforce a limited set of
50+
markers can add ``--strict`` to ``addopts``:
4751

4852
.. code-block:: ini
4953
@@ -53,6 +57,9 @@ should add ``--strict`` to ``addopts``:
5357
slow
5458
serial
5559
60+
Third-party plugins should always :ref:`register their markers <registering-markers>`
61+
so that they appear in pytest's help text and do not emit warnings.
62+
5663

5764
.. _marker-revamp:
5865

doc/en/writing_plugins.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,30 @@ the plugin manager like this:
286286
If you want to look at the names of existing plugins, use
287287
the ``--trace-config`` option.
288288

289+
290+
.. _registering-markers:
291+
292+
Registering custom markers
293+
--------------------------
294+
295+
If your plugin uses any markers, you should register them so that they appear in
296+
pytest's help text and do not :ref:`cause spurious warnings <unknown-marks>`.
297+
For example, the following plugin would register ``cool_marker`` and
298+
``mark_with`` for all users:
299+
300+
.. code-block:: python
301+
302+
def pytest_configure(config):
303+
config.addinivalue_line(
304+
"markers",
305+
"cool_marker: this one is for cool tests.",
306+
)
307+
config.addinivalue_line(
308+
"markers",
309+
"mark_with(arg, arg2): this marker takes arguments.",
310+
)
311+
312+
289313
Testing plugins
290314
---------------
291315

0 commit comments

Comments
 (0)