Skip to content

Allow sorting tests newest first#6551

Open
tgolsen wants to merge 2 commits intosebastianbergmann:mainfrom
tgolsen:feature/sort-by-test-age
Open

Allow sorting tests newest first#6551
tgolsen wants to merge 2 commits intosebastianbergmann:mainfrom
tgolsen:feature/sort-by-test-age

Conversation

@tgolsen
Copy link

@tgolsen tgolsen commented Mar 25, 2026

This is my first contribution to PHPUnit. I tried to follow convention best as I could including copying the copyright notice on the fixture test classes. Hope that's right.

This PR introduces a --order-by newest option for chronological test sorting, running the most recently modified test files first.

When developing tests in a project with a lot of existing tests, I wanted my new ones to go first. This new execution order option prioritizes recently modified test files. Combined with --stop-on-failure it can save time for a developer manually running tests.

Changes

  • New execution order constant: TestSuiteSorter::ORDER_NEWEST
  • CLI option: --order-by newest
  • Sorting logic: Tests are ordered by file modified time

Design Decisions

  • Implements newest-first ordering only, as oldest-first has no obvious (to me) use case
  • Sorts at the test class file level rather than individual test methods (by necessity)
  • Test suites are sorted by the modification time of the first test in the suite, I thought this was a reasonable answer for what to do with suites rather than a more complex check of the content

Testing

  • Added end-to-end test with fixture files
  • Test includes setup script to maintain consistent file timestamps

Usage

# Run tests with newest files first
phpunit --order-by newest

@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/test-runner CLI test runner labels Mar 26, 2026
@codecov
Copy link

codecov bot commented Mar 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.06%. Comparing base (2dd2490) to head (f5a508e).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #6551   +/-   ##
=========================================
  Coverage     96.06%   96.06%           
- Complexity     7569     7581   +12     
=========================================
  Files           817      817           
  Lines         23415    23447   +32     
=========================================
+ Hits          22493    22525   +32     
  Misses          922      922           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Labels

feature/test-runner CLI test runner type/enhancement A new idea that should be implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants