必须执行 php artisan pest:install 才能启用 Laravel 测试功能,否则 tests/Pest.php 不生成、TestCase 未注入、get()/factory() 等方法不可用;Laravel 10+ 还需显式安装 pest-plugin-laravel。

直接装就行,但必须分清“只装Pest”和“装完能用”的区别——很多人装完跑不起来,不是命令错了,是漏了 Laravel 插件或初始化步骤。
composer require 命令要带哪些包
在 Laravel 项目里,composer require --dev pestphp/pest 单独执行只能跑基础断言,Laravel 的 get()、post()、数据库事务、工厂这些全失效。真正可用的最小安装组合是:
composer require --dev pestphp/pest pestphp/pest-plugin-laravel- 如果是 Laravel 10+,
pest-plugin-laravel是必须显式加上的(官方文档没强调这点,但实测不加就报Call to undefined function get()) - 非 Laravel 项目(比如纯 PHP 或 WordPress),换成对应插件:
dingo-d/wp-pest-integration-test-setup或pestphp/pest-plugin-browser
装完必须跑 php artisan pest:install
这步跳过,tests/Pest.php 不会生成,uses(Tests\TestCase::class) 不会被自动注入,所有 Laravel 测试辅助方法都不可用。常见现象包括:
-
$this->get('/')报错:找不到方法(实际是没继承TestCase) -
User::factory()->create()报错:Factory not found(因为没加载 Laravel 测试上下文) - 软删除、事件监听器、数据库事务全部不触发
执行后会自动生成 tests/Pest.php 和标准目录结构,并把 RefreshDatabase 等 trait 挂载进默认上下文。
为什么 ./vendor/bin/pest 有时不认 tests/Unit/ 下的文件
Pest 默认只扫描 tests/Feature/ 和 tests/Unit/,但前提是 tests/Pest.php 里有对应配置。打开它检查是否有这行:
工作流任务拆解与模型编排技能。使用场景:(1) 收到复杂工作任务需要拆解为可执行步骤,(2) 需要为不同步骤选择最合适的模型,(3) 需要跟踪工作流进度和模型使用情况,(4) 长时间任务卡住需要问题诊断和解决方案。
uses(Tests\TestCase::class)->in('Feature', 'Unit');
如果手动新建了 tests/Unit/UserTest.php 却没被发现,大概率是 in() 参数漏写了 'Unit',或者路径写成了 unit(大小写敏感)。
测试跑不起来还可能卡在 PHPUnit 环境上
Pest 本质是 PHPUnit 的 DSL 封装,底层仍依赖 PHPUnit。如果你的项目之前删过 phpunit.xml,或改过 节点,Pest 可能找不到测试入口。最稳做法是:
- 确认
phpunit.xml存在且未被清空 - 里面至少保留默认的
配置tests - 不要手动改
段里的includePath,Pest 会自己处理自动加载
很多“装了但 command not found”或“0 tests run”问题,根子都在这里。










