Skip to content

docs: clarify pytest_deselected/pytest_collection_modifyitems usage #12729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 22, 2024
Merged
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
1 change: 1 addition & 0 deletions changelog/12663.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clarify that the `pytest_deselected` hook should be called from `pytest_collection_modifyitems` hook implementations when items are deselected.
11 changes: 11 additions & 0 deletions src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ def pytest_collection_modifyitems(
"""Called after collection has been performed. May filter or re-order
the items in-place.

When items are deselected (filtered out from ``items``),
the hook :hook:`pytest_deselected` must be called explicitly
with the deselected items to properly notify other plugins,
e.g. with ``config.hook.pytest_deselected(deselected_items)``.

:param session: The pytest session object.
:param config: The pytest config object.
:param items: List of item objects.
Expand Down Expand Up @@ -454,6 +459,12 @@ def pytest_collectreport(report: CollectReport) -> None:
def pytest_deselected(items: Sequence[Item]) -> None:
"""Called for deselected test items, e.g. by keyword.

Note that this hook has two integration aspects for plugins:

- it can be *implemented* to be notified of deselected items
- it must be *called* from :hook:`pytest_collection_modifyitems`
implementations when items are deselected (to properly notify other plugins).

May be called multiple times.

:param items:
Expand Down
Loading