framework: PHPUnit warning: Test code or tested code did not remove its own exception handlers
Laravel Version
Any that uses PHPUnit ^10
PHP Version
Any
Database Driver & Version
No response
Description
Seems that, since PHPUnit 10.5.4, running any kind of test suite that boots the Laravel Framework displays the following warning multiple times: Test code or tested code did not remove its own exception handlers.
Steps To Reproduce
laravel new laravel
cd laravel
./vendor/bin/phpunit
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 7
- Comments: 27 (5 by maintainers)
I never had this issue in 10.x, but I just upgraded from latest 10.x to latest 11.x and 11.0.4 and I’m seeing this on every single test in my project
Putting the below in (what would be) my
Tests/TestCaseworked in the package I am working on and encountered this in:We updated to L11 last week and today Dependabot bumped PHPUnit to v11 and these errors starting to appear for all tests:
As of now, we can’t find any working solution. This issue points to Symfony thread which is still open and redirects back to #49622. Does anyone have a working solution? @phily245 suggestion didn’t do the trick 😦
seems like its back on latest phpunit version
I can confirm this has reappeared:
Looks like it’s been re-introduced.
For those still struggling with this. Adding
HandleExceptions::flushState();seems to do the trick.In
tests/TestsCase.phpOverride the
createApplication()method and addHandleExceptions::flushState();before returning$appIt seems like this is back in 11.
Not really. I still see a lot of those warnings even after removing the code above.
I’ve figured it out:
The issue was stemming by the fact that I had a custom PhpUnit bootstrap class I was using, and in it I was doing a
createApplication()(i.e.$app->make(Kernel::class)->bootstrap()) to be able to call some artisan commands before/after running the tests.I simply had to make sure I ran
HandleExceptions::flushState();at the end of each method where I was doingcreateApplication()in that boostrap class.Probably what was happening was that if I manually create an application outside of tests, it would add extra error/exception handlers that neither Laravel nor PhpUnit would know about, and then PhpUnit would complain about it in the end.
I’m confused. What am I supposed to do?
I tried the
HandleExceptions::flushState();bit and the tearDown bit as well both together and separately as well, and neither does anything.There’s nothing in the Laravel docs about what to do here, nor is there anything in the PhpUnit docs. I’m stuck and am not able to upgrade to Laravel 12 this way.
Following the advice above, I added
but I started getting an error:
Test code or tested code removed error handlers other than its ownThis code helped me:
I’m using
and keep getting the “Test code or tested code removed error handlers other than its own” error for each test. I tried adding this code to
createApplication()HandleExceptions ::flushState();but it doesn’t work unfortunately.I’m getting the same issue. Here is some more information in case it helps someone:
phpunit.xml
bootstrap.php
The HandleExceptions::flushState() (taken from a comment above) seems to do the trick.
ISSUE VERSION PHPUnit 12.0.5
Using the suggested
I now get the opposite error
Test code or tested code removed error handlers other than its ownWe have been digging into this issue at work with a new project. My boss found that removing newRelic in the docker container solved resolved the “Test code or tested code removed…” errors. He also mentioned there appears to be a memory leak in PHPUnit 11.3, though that has not yet been confirmed. I can confirm on my instance, removing newRelic from the docker container did resolve issues for me. Tests execute as expected now.