You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 10+PHPUnit 10单测报错:Cannot find TestCase object on call stack

Laravel 10 + PHPUnit 10 单测方法执行异常问题

问题现象

  • 执行php artisan test运行全部测试时,所有测试均可正常执行;
  • 执行php artisan test --filter test_get_profile单独运行指定测试方法时,触发PHPUnit内部错误:
    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
    
  • 部分测试方法(如test_login、test_register)可单独正常运行,但新建测试方法时偶尔会出现上述错误;
  • 若指定测试文件路径执行php artisan test tests/Feature/AccountTest.php --filter test_get_profile则无报错,但希望无需每次指定路径。

测试代码示例

所有测试方法均为基础空测试,示例如下:

public function test_get_profile(): void
{
    $response = $this->get('/');

    $response->assertStatus(200);
}

相关配置与测试文件代码

phpunit.xml

<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>

UserTest.php

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);
    }
}

AccountTest.php

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);
    }
}

请问有人知道该问题的原因吗?

内容的提问来源于stack exchange,提问作者d3ridi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 06:14:54