Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/12376.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Added :func:`pytest.register_fixture()` to register fixtures using an imperative interface.

This is an advanced function intended for use by plugins.

Normally, fixtures should be registered declaratively using the :func:`@pytest.fixture <pytest.fixture>` decorator.
Pytest looks for these fixture definitions during the collection phase and registers them automatically.
For some plugin usecases the declarative interface can be cumbersome or unviable, in which case this imperative interface can be used.
1 change: 1 addition & 0 deletions changelog/14004.deprecation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ Passing ``baseid`` to :class:`~pytest.FixtureDef` or ``nodeid`` strings to fixtu

Use the ``node`` parameter instead for fixture scoping. This enables more robust node-based
matching instead of string prefix matching.
If you've used ``nodeid=None``, pass ``node=session`` instead.

This will be removed in pytest 10.
2 changes: 2 additions & 0 deletions changelog/14513.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The private ``FixtureDef.has_location`` attribute is now deprecated and will be removed in pytest 10.
See :ref:`fixturedef-has-location-deprecated` for details.
5 changes: 5 additions & 0 deletions changelog/14513.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The order in which fixture definitions overriding each other are resolved is now determined first by their *visibility* in the collection tree rather than by the order in which they happened to be registered.

A fixture defined for a more specific node (e.g. a module or an item) now always takes precedence over one with the same name defined for a more general node (e.g. the session), even when the more general one was registered later.
Fixtures with non-comparable visibility keep the existing behavior of "last registered wins".
This change is supposed to only affect plugins which register multiple fixtures programmatically with the same name.
20 changes: 18 additions & 2 deletions doc/en/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Below is a complete list of all pytest features which are considered deprecated.
Passing ``baseid``/``nodeid`` strings to fixture registration APIs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. deprecated:: 9.2
.. deprecated:: 9.1

Passing ``baseid`` to :class:`~pytest.FixtureDef` or ``nodeid`` strings to
``FixtureManager._register_fixture`` and ``FixtureManager.parsefactories``
Expand All @@ -37,11 +37,27 @@ node-based matching instead of fragile string prefix matching.

# Use instead
fixture_manager.parsefactories(holder=plugin_obj, node=directory_node)
fixture_manager._register_fixture(name="fix", func=func, node=directory_node)
pytest.register_fixture(name="fix", func=func, node=directory_node)

The equivalent of passing ``nodeid=None`` (global visibility) is ``node=session``.

In pytest 10, the ``baseid`` and ``nodeid`` string parameters will be removed.


.. _fixturedef-has-location-deprecated:

``FixtureDef.has_location``
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. deprecated:: 9.1

The private ``FixtureDef.has_location`` attribute is deprecated and will be removed in pytest 10.

It indicated whether a fixture was found from a node or a conftest in the collection tree (as opposed to a non-conftest plugin).
It was used to determine the override order of fixtures, pushing fixtures with "no location" to the front of the override chain (such that they are chosen last).
The override order is now determined by the visibility of the fixtures in the collection tree, making this distinction obsolete.


.. _console-main:

``pytest.console_main()``
Expand Down
5 changes: 5 additions & 0 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
"Pass node instead for fixture scoping."
)

FIXTUREDEF_HAS_LOCATION_DEPRECATED = PytestRemovedIn10Warning(
"FixtureDef.has_location is deprecated and will be removed in pytest 10. "
"See https://docs.pytest.org/en/stable/deprecations.html#fixturedef-has-location-deprecated"
)

PARSEFACTORIES_NODEID_DEPRECATED = PytestRemovedIn10Warning(
"Passing nodeid string to parsefactories is deprecated. "
"Use parsefactories(holder=obj, node=node) instead."
Expand Down
Loading