Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laracoft's avatar

php artisan test is stricter than PHPUnit

My test fails when running php artisan test but passes when I use PHPUnit directly.

It has to do with an undefined variable. I already tried this in my phpunit.xml, but it is still not throwing the exception like artisan test.

convertDeprecationsToExceptions="true"
stopOnWarning="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnWarning="true"
convertErrorsToExceptions="true"
stopOnError="true"
stopOnFailure="false"

How do I make PHPUnit behave the same? Thank you.

0 likes
2 replies
laracoft's avatar
laracoft
OP
Best Answer
Level 27

Pardon me, I did try to google before posting, somehow google likes to find me the answer after I post on Laracast.

https://coderwall.com/p/ka6o8a/catch-php-warnings-and-notices-when-unit-testing

Add 2 methods in the test class

public function setUp() {
    set_error_handler(function($errno, $errstr, $errfile, $errline) {
        throw new RuntimeException($errstr . " on line " . $errline . " in file " . $errfile);
    });
}

public function tearDown() {
    restore_error_handler();
}
jamessoncardozo's avatar

I've tried to do like this, but I get this error:

PHP Fatal error: Declaration of Tests\Feature\Livewire\Api\ApiBuzCardTest::setUp() must be compatible with Illuminate\Foundation\Testing\TestCase::setUp(): void in /var/www/virtual-card/tests/Feature/Livewire/Api/ApiBuzCardTest.php on line 39

Please or to participate in this conversation.