Skip to content

Conversation

@sclaughl
Copy link

@sclaughl sclaughl commented Jan 29, 2026

Closes #14150

Adds a new --collect-only-tree (--co-tree) flag that displays collected tests as a tree with box-drawing characters,
providing a more readable alternative to the classic <Type name> format.

pytest
└── testing
    └── test_example.py
        └── TestFoo
            └── test_bar

Features:

  • Box-drawing characters (ASCII fallback for non-UTF-8)
  • Color-coded output (directories/files bold, classes cyan, functions green)
  • Respects -q (summary only) and -v (shows docstrings)

The existing --collect-only behavior is unchanged.

Because it's no longer 2005 and we don't have to pretend we're writing XML.

The new --co-tree flag displays collected tests as a tree with box-drawing
characters (├── └── │) instead of the classic <Type name> format. Features:

- Box-drawing characters for TTY, ASCII fallback for non-TTY
- Colors: bold for files/dirs, cyan for classes, green for functions
- Custom collector types shown with (TypeName) annotation
- Proper deduplication for overlapping collection paths

Architecture:
- New CollectionTree class separates tree-building from rendering
- Classic --collect-only unchanged (preserves execution order view)
- Tree view shows structural hierarchy (groups by module/class)

Design note: --co-tree deliberately shows structural organization rather
than execution order. It does not reflect pytest_collection_modifyitems
reordering. Use --co for execution order, --co-tree for structure.
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jan 29, 2026
- Remove unused render_classic method
- Fix Unicode detection: check file encoding instead of color support
- Update docs to clarify structural vs execution order difference
- Add tests for parametrized tests and --co/--co-tree precedence
- Update test patterns to use Unicode box-drawing characters
@nicoddemus
Copy link
Member

nicoddemus commented Jan 30, 2026

Thanks @sclaughl for the PR. It seems very complete and well written.

However, I'm personally hesitant to add yet another flag (and a bunch of code to maintain) regarding this feature.

In my opinion we have 2 options:

  1. Decline the PR; while it looks good, I don't think it is worth in terms of extra flags and maintenance to the core.
  2. Replace the output from --collect-only with this new output; not sure it is worth the hassle, as this breaks backward compatibility (I'm sure there are scripts out there which parse the output of --collect-only).

What other maintainers think?

@RonnyPfannschmidt
Copy link
Member

Its tricky the tree display is not meant for parsing so replacing is acceptable

The key details of reorder vs compact need some bikeshedding

@sclaughl
Copy link
Author

I'd be happy to replace --collect-only entirely and remove the angle-bracket code (and tests). That would address the maintenance concern better than adding a new flag.

To be explicit about the design: the tree shows structural organization (by directory/module/class) and would not reflect reordering from pytest_collection_modifyitems. The hook would remain for use by features like --lf / --ff, but --collect-only would answer "how are tests organized?" rather than "what order will they run?"

For anyone who needs execution order, --co -q already shows nodeids in run sequence (sans angle brackets).

This seems like a good trade-off -- a proper tree can't represent arbitrary reordering without breaking its structure. Just wanted to flag it clearly since it's a behavior change.

Happy to rework the PR if you'd like to go in that direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --collect-only-tree flag for modern tree-style output

3 participants