Exit with non-zero exit code when explicit test selection (`--filter`, `--group`, `--testsuite`) yields no tests · Issue #6276 · sebastianbergmann/phpunit · GitHub
Skip to content

Exit with non-zero exit code when explicit test selection (--filter, --group, --testsuite) yields no tests #6276

Closed

Description

@staabm
Q A
PHPUnit version a62124f
PHP version 8.3.23
Installation Method Composer / PHAR

How to reproduce

➜  phpunit git:(main) php phpunit --filter FilterWhichDoesNotMatchAnything          
PHPUnit 12.3-ga62124f38 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.23
Configuration: /Users/m.staab/Documents/GitHub/phpunit/phpunit.xml

No tests executed!
➜  phpunit git:(main) echo $?            
0

➜  phpunit git:(main) php -v                                              
PHP 8.3.23 (cli) (built: Jul  1 2025 16:52:12) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.23, Copyright (c) Zend Technologies

Expected behavior

I think in case we do not run any tests and had a --filter defined, PHPUnit should return a non-zero exit code

Activity

  1. staabm commented on Jul 15, 2025

    @staabm
    ContributorAuthor

    I think the same should be true for --testsuite and --group based filters:

    ➜  phpunit git:(main) php phpunit --group test-abc
    PHPUnit 12.3-ga62124f38 by Sebastian Bergmann and contributors.
    
    Runtime:       PHP 8.3.23
    Configuration: /Users/m.staab/Documents/GitHub/phpunit/phpunit.xml
    
    No tests executed!
    ➜  phpunit git:(main) echo $?
    0
    ➜  phpunit git:(main) php phpunit --testsuite test-abc
    PHPUnit 12.3-ga62124f38 by Sebastian Bergmann and contributors.
    
    Runtime:       PHP 8.3.23
    Configuration: /Users/m.staab/Documents/GitHub/phpunit/phpunit.xml
    
    No tests executed!
    ➜  phpunit git:(main) echo $?                         
    0
    
  2. kubawerlos commented on Jul 15, 2025

    @kubawerlos
    Contributor

    What about running without any parameters and still getting "No tests executed!" (because of no tests)?

    What about a simple relation, "No tests executed!" -> "a non-zero exit code"?

  3. staabm commented on Jul 15, 2025

    @staabm
    ContributorAuthor

    this might work, too.

    I tried to talk about my actual use-cases. but it might work for a more general case.

    not sure about possible BC impact though.

  4. joteejotee commented on Jul 16, 2025

    @joteejotee

    I'd like to work on this issue if nobody else is working on it.

  5. staabm commented on Jul 16, 2025

    @staabm
    ContributorAuthor

    We have no signal from Sebastian yet whether a fix would be accepted (he is on vacation atm).

    But if you want to work on it - go ahead.

  6. joteejotee commented on Jul 16, 2025

    @joteejotee

    That makes sense—since we haven’t heard Sebastian’s opinion yet, I’ll hold off for now. Thanks for letting me know!

  7. stof commented on Jul 18, 2025

    @stof
    Contributor

    There is already a --fail-on-empty-test-suite config setting (the XML config file also supports it). Isn't it solving that need already ?

  8. staabm commented on Jul 18, 2025

    @staabm
    ContributorAuthor

    Isn't it solving that need already ?

    thanks, wasn't aware of --fail-on-empty-test-suite.

    I think the behaviour of --fail-on-empty-test-suite should be the default for --filter (or maybe even in general).

  9. stof commented on Jul 18, 2025

    @stof
    Contributor

    @staabm just set failOnEmptyTestSuite="true" in your XML config file (or failOnAllIssues="true").

    Right now, none of the failOn* setting defaults to true, so this is consistent.

  10. staabm commented on Jul 25, 2025

    @staabm
    ContributorAuthor

    thanks for the examples. that indeed proves that my initial problem is actually not the same as the feature --fail-on-empty-test-suite seems to provide.

    I think its a DX problem that PHPUnit reports a "success" result when its instructed to run with a certain filter but no tests match such filter. it would be useful for any tool (KI Agent, CI Job, batch file,..) invoking phpunit that no tests were run (by default, without an additional required config flag)

  11. changed the title [-]`--filter FilterWhichDoesNotMatchAnything` should exit with non-zero exit code[/-] [+]Exit with non-zero exit code when explicit test selection (--filter, --group, --testsuite) yields no tests[/+] on Apr 12, 2026
  12. changed the title [-]Exit with non-zero exit code when explicit test selection (--filter, --group, --testsuite) yields no tests[/-] [+]Exit with non-zero exit code when explicit test selection (`--filter`, `--group`, `--testsuite`) yields no tests[/+] on Apr 12, 2026
  13. added a commit that references this issue on Apr 12, 2026
    bf6b5a9
  14. sebastianbergmann commented on Apr 12, 2026

    @sebastianbergmann
    Owner

    When --filter, --exclude-filter, --group, --exclude-group, --testsuite, or --exclude-testsuite is used and no tests match, PHPUnit now exits with exit code 1 instead of 0.

    The change is a one-line modification in ShellExitCodeCalculator: the default value of $failOnEmptyTestSuite is set to true when any of those options are active, instead of unconditionally false. This means the existing failOnEmptyTestSuite logic kicks in automatically when explicit test selection yields no results.

    --do-not-fail-on-empty-test-suite (and doNotFailOnEmptyTestSuite="true" in XML) still overrides this and restores exit code 0, so there is an escape hatch for anyone who needs the old behavior.

    Without any filter/group/testsuite selection, empty test suites continue to exit with 0, no change in behavior there.

    $ php phpunit --filter FilterWhichDoesNotMatchAnything
    PHPUnit ...
    No tests executed!
    $ echo $?
    1
    
    $ php phpunit --group nonexistent-group
    PHPUnit ...
    No tests executed!
    $ echo $?
    1
    
    $ php phpunit --testsuite nonexistent-suite
    PHPUnit ...
    No tests executed!
    $ echo $?
    1
    
    $ php phpunit --filter FilterWhichDoesNotMatchAnything --do-not-fail-on-empty-test-suite
    PHPUnit ...
    No tests executed!
    $ echo $?
    0
    
  15. added a commit that references this issue on Apr 12, 2026
    bfb92ce
  16. added 4 commits that reference this issue on Apr 12, 2026
    716ce3b
    ae1cf4f
    f26c46e
    abefdcc
  17. locked as resolved and limited conversation to collaborators on May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions