Skip to content

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

@sclaughl

Description

@sclaughl

What's the problem this feature will solve?

The current --collect-only output uses an XML-like <Type name> format that feels a bit... vintage:

<Dir pytest>
  <Dir testing>
    <Module test_example.py>
      <Class TestFoo>
        <Function test_bar>

While this format has served us well, it's 2026 now and we have nicer ways to visualize hierarchical data. The angle-bracket format is difficult to scan visually, especially for large test suites, and doesn't leverage modern terminal capabilities.

Describe the solution you'd like

A new --collect-only-tree (or --co-tree) flag that displays collected tests as a proper tree with box-drawing characters:

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

With support for:

  • Box-drawing characters (├── └── │), ASCII fallback for non-UTF-8
  • Colors: bold for files/dirs, cyan for classes, green for test functions
  • Proper tree structure showing sibling relationships

This provides the same information but in a format that's easier to read at a glance.

Alternative Solutions

The existing --collect-only would remain unchanged for backward compatibility and tooling that may parse its output. The tree format is purely additive as a new flag.

One could write a plugin or post-process the output, but this seems useful enough to warrant inclusion in core pytest.

Additional context

I have a working implementation ready: https://github.com/sclaughl/pytest/tree/feature/collect-only-tree-flag

A few design notes:

  • Structural vs execution order: The tree shows tests grouped by their hierarchy (directory/module/class), not the order they'll run. The classic --co reflects pytest_collection_modifyitems reordering. --co-tree answers "how are tests organized?" which felt more appropriate for a tree visualization.
  • Verbosity: -q shows just summary counts, -v includes docstrings alongside test names
  • Architecture: Separated tree-building from rendering so other output formats (JSON tree, etc.) could reuse the structure if desired

Happy to adjust any of this based on feedback. And yes, I checked -- pytest-collect-formatter exists but outputs JSON/YAML/XML for machines, not a visual tree for humans.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions