Čamo's avatar

Laravel 12 CommandFinished does not run for php artisan test

I would like to implement some logic after all tests in Laravel 12 application finished. I found this blog from 2024 https://laravel.com/docs/12.x/events#main-content which is based on CommandFinished native event. So I made a CommandFinishedListener. If I run php artisan event:list I see the lister in the list. So it is obviously registered. If I run any command it works BUT as I run php artisan test the listener does not run. All tests pass but the event does not trigger. Thanks for any help.

1 like
11 replies
Glukinho's avatar

Do you have Event::fake(); somewhere in your tests?

1 like
Čamo's avatar

No Event::fake() is not in any of tests

1 like
Glukinho's avatar

Try to place in AppServiceProvider:

public function boot(): void
{
	Event::listen(function (CommandFinished $event) {
		Log::debug($event::class . " was finished; command = {$event->command}");
	});
}

I get proper event and log record when I launch php artisan test and tests pass:

[2025-12-17 15:25:22] local.DEBUG: Illuminate\Console\Events\CommandFinished was finished; command = test
1 like
Čamo's avatar

Thank you very much, i will check it as soon as possible.

1 like
Čamo's avatar

Did you try also the Listener class? Do you have the same behaviours with test command?

1 like
Čamo's avatar

No success. This implementation behave the same way. As I run any command like cache:clear it works but as I run test it does not log anything.

1 like
Glukinho's avatar

Try to launch one single test, preferably empty or very simple, using php artisan test --filter VerySimpleTest option. Do you see event dispatched?

Čamo's avatar

No I do not see the event if the command is test. What version of Laravel do you have? Any other command works as expected but not php artisan test.

Čamo's avatar

This is not true it is related only to commands tests - when test testing some command

Please or to participate in this conversation.