Skip to content

feat(13617): record reason for deselection and allow it to be displayed #13618

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ def pytest_collection_modifyitems(items: list[nodes.Item], config: Config) -> No
deselected = []
for colitem in items:
if colitem.nodeid.startswith(deselect_prefixes):
colitem._deselected_reason = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No no patch on attributes

f"Deselected by --deselect option: {colitem.nodeid}"
)
deselected.append(colitem)
else:
remaining.append(colitem)
Expand Down
5 changes: 4 additions & 1 deletion src/_pytest/mark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ def deselect_by_keyword(items: list[Item], config: Config) -> None:
deselected = []
for colitem in items:
if not expr.evaluate(KeywordMatcher.from_item(colitem)):
colitem._deselected_reason = (
f"Keyword expression '{keywordexpr}' did not match."
)
deselected.append(colitem)
else:
remaining.append(colitem)
Expand Down Expand Up @@ -266,9 +269,9 @@ def deselect_by_mark(items: list[Item], config: Config) -> None:
if expr.evaluate(MarkMatcher.from_markers(item.iter_markers())):
remaining.append(item)
else:
item._deselected_reason = f"Mark expression '{matchexpr}' did not match."
deselected.append(item)
if deselected:
config.hook.pytest_deselected(items=deselected)
items[:] = remaining


Expand Down
4 changes: 4 additions & 0 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Node(abc.ABC, metaclass=NodeMeta):
# Note that __dict__ is still available.
__slots__ = (
"__dict__",
"_deselected_reason",
"_nodeid",
"_store",
"config",
Expand Down Expand Up @@ -214,6 +215,9 @@ def __init__(
# Deprecated alias. Was never public. Can be removed in a few releases.
self._store = self.stash

#: A reason for deselection.
self._deselected_reason = ""

@classmethod
def from_parent(cls, parent: Node, **kw) -> Self:
"""Public constructor for Nodes.
Expand Down
7 changes: 6 additions & 1 deletion src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,12 @@ def _build_collect_only_summary_stats_line(
if errors:
main_color = _color_for_type["error"]
parts += [("%d %s" % pluralize(errors, "error"), {main_color: True})] # noqa: UP031

if self.verbosity >= 2:
for item in self.stats["deselected"]:
self.write_line(
f"Deselected: {item.nodeid}: {item._deselected_reason}",
cyan=True,
)
return parts, main_color


Expand Down
Loading