Laravel phpunit test cannot find TestCase object on call stack
P粉733166744
2023-08-26 20:14:38
<p>Where to run all tests
<code>php artisan test</code>
Everything works as expected and all tests run</p>
<p>Now when I run signle test <code>php artisan test --filter test_get_profile</code> I get this wired error</p>
<pre class="brush:php;toolbar:false;">An error occurred inside PHPUnit.
Message: Cannot find TestCase object on call stack
Location: D:\laragon\www\project\vendor\phpunit\phpunit\src\TextUI\TestRunner.php:68</pre>
<p>But some other tests still work, like test_login and test_register work, but when I create new tests, sometimes it works, sometimes I get this wired error</p>
<p>PS: I added the file path example<code>php artisan test tests/Feature/AccountTest.php --filter test_get_profile</code> I don’t get any errors but I don’t know to always include the file path< ;/ p>
<p>Please<strong>note that all tests are empty</strong></p>
<pre class="brush:php;toolbar:false;">public function test_get_profile(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}</pre>
<p>Does anyone know about this problem?我正在使用 laravel 10 和 phpunit 10</p>
<p>phpunit.xml:</p>
<pre class="brush:php;toolbar:false;"><phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit></pre>
<p>UserTest.php</p>
<pre class="brush:php;toolbar:false;">namespace Tests\Feature;
use Tests\TestCase;
class UserTest extends TestCase
{
public function test_login(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
public function test_register(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}</pre>
<p>AccountTest.php</p>
<pre class="brush:php;toolbar:false;">namespace Tests\Feature;
use Tests\TestCase;
class AccountTest extends TestCase
{
/*** A basic feature test example.*/
public function test_get_profile(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}</pre></p>
This is a bug in PHPUnit.
https://github.com/sebastianbergmann/phpunit/issues/5403
This is the fix -
https://github.com/sebastianbergmann/phpunit/commit/16166431bce84bb100d8e7fe867d612bdfe61776# diff-548ab8441af390a03ab9bf5e83a441aaa67c43de14b247a1426c2983b434bd89
Run Composer update to get the latest version.