最简安装只需执行 composer require pestphp/pest --dev,它会自动安装兼容版 PHPUnit、写入 require-dev、生成 tests/Pest.php 并支持 tests/ 目录扫描;Laravel 项目还需额外安装 pestphp/pest-plugin-laravel 并运行 php artisan pest:install 才能使用 get() 等辅助方法。

直接执行 composer require pestphp/pest --dev 就够了
这是最简安装路径,Pest 不是独立运行的测试引擎,而是 PHPUnit 的轻量 DSL 封装,所以它不替代 PHPUnit,也不需要你先手动装 PHPUnit —— 安装 Pest 时会自动拉取兼容版本的 phpunit/phpunit 作为依赖。
composer require pestphp/pest --dev 会:
- 把 Pest 写进 composer.json 的 require-dev 区
- 自动生成 tests/Pest.php 入口(部分旧项目可能需手动触发 ./vendor/bin/pest --init)
- 默认启用 tests/ 目录扫描,无需改 phpunit.xml 即可跑起来
常见错误现象:执行完命令后运行 ./vendor/bin/pest 报错 “Command not found”
原因通常是 Composer 的 bin-dir 被自定义过,或未刷新 autoloader —— 此时补一句 composer dump-autoload 即可解决。
Laravel 项目要多加一个插件:pestphp/pest-plugin-laravel
纯 PHP 项目装完 pestphp/pest 就能写 test() 和 it();但 Laravel 项目里,get()、post()、actingAs() 这些方法不会自动可用 —— 它们来自 Laravel 插件,不是 Pest 自带的。
必须额外安装:
composer require --dev pestphp/pest-plugin-laravel
然后运行:
php artisan pest:install
这个命令才是真正把 Laravel 测试上下文接入 Pest 的关键步骤,它会:
- 备份原 tests/TestCase.php
- 在 tests/Pest.php 中注入 uses(Tests\TestCase::class)
- 创建 tests/Feature 和 tests/Unit 目录结构
⚠️ 容易踩的坑:跳过 pest:install,直接新建 tests/Feature/HomeTest.php 并写 it('loads homepage', fn() => get('/')->assertOk()) —— 会报 Call to undefined function get(),因为没加载 Laravel 插件的辅助函数注册逻辑。
WordPress 或自定义框架需用专用集成包
Pest 本身不感知框架,所有“开箱即用”的便利都靠社区插件提供。比如 WordPress 项目不能直接用 pestphp/pest-plugin-laravel,得换:
composer require --dev dingo-d/wp-pest-integration-test-setup
再运行:
vendor/bin/wp-pest setup theme 或 vendor/bin/wp-pest setup plugin --plugin-slug=my-plugin
这类工具会:
- 注册 WordPress 核心函数(如 add_action、do_shortcode)到测试作用域
- 配置 wp-load.php 加载路径
- 提供 usefunction Pest\WordPress\test 等命名空间别名
如果你在非标准环境(比如自研 CMS)下硬套 Laravel 插件,$this->get() 会始终报错 —— 不是 Pest 问题,是上下文根本没搭对。
围绕关键发现、作用机制、临床相关性及研究局限性展开讨论。适用于撰写或优化任何生物医学论文的“讨论(Discussion)”部分——包括结果解读、与既往文献关联、阐释意外发现、界定研究局限性,以及撰写结论。当用户输入以下任一指令时也会自动触发该功能: - “write my discussion” - “help me discuss my findings” - “how do I compare to prior studies” - “write the limitations par
别忽略 tests/Pest.php 里的 uses() 调用
这个文件是 Pest 的“全局配置中枢”,所有测试都会隐式继承它声明的类和 trait。例如:
uses(Tests\TestCase::class, RefreshDatabase::class)->in('Feature');
意味着放在 tests/Feature/ 下的所有 it() 测试,自动获得数据库事务回滚能力。
但如果你把 Model 单元测试写在 tests/Unit/UserTest.php,而 uses() 没覆盖 Unit 目录,那 RefreshDatabase 就不生效 —— assertDatabaseMissing() 可能因脏数据失败。
性能影响:多个 uses() 会增加每个测试的初始化开销,但比在每个测试里重复写 use RefreshDatabase; + setUp() 更轻量。真正要注意的是路径匹配是否准确 —— in('Feature', 'Unit') 和 in('feature', 'unit') 是不同的(大小写敏感)。

















