Pest PHP 测试开发
pest-testing
仅在 Laravel 项目中使用此技能进行 Pest PHP 测试。每当编写、编辑、修复或重构任何测试时触发——包括修复代码更改后损坏的测试、添加断言、将 PHPUnit 转换为 Pest、添加数据集以及 TDD 工作流。当用户询问如何用 Pest 编写某些内容、提及测试文件或目录(tests/Feature、tests/Unit、tests/Browser),或需要浏览器测试、对多个页面进行冒烟测试以检查 JS 错误、或需要架构测试时,务必激活。涵盖:test()/it()/expect() 语法、数据集(datasets)、模拟(mocking)、浏览器测试(visit/click/fill)、冒烟测试、arch()、Livewire 组件测试、RefreshDatabase 以及所有 Pest 4 功能。不用于工厂(factories)、种子文件(seeders)、迁移(migrations)、控制器(controllers)、模型(models)或非测试 PHP 代码。
- License 许可
- MIT
- 最近更新
- 2026-11-12
- Stars
- ★ 6
- Forks
- 1
01 前言 Front Matter
- name
- pest-testing
- license
- MIT
- metadata
- { "author": "laravel" }
- description
- Use this skill for Pest PHP testing in Laravel projects only. Trigger whenever any test is being written, edited, fixed, or refactored — including fixing tests that broke after a code change, adding assertions, converting PHPUnit to Pest, adding datasets, and TDD workflows. Always activate when the user asks how to write something in Pest, mentions test files or directories (tests/Feature, tests/Unit, tests/Browser), or needs browser testing, smoke testing multiple pages for JS errors, or architecture tests. Covers: test()/it()/expect() syntax, datasets, mocking, browser testing (visit/click/fill), smoke testing, arch(), Livewire component tests, RefreshDatabase, and all Pest 4 features. Do not use for factories, seeders, migrations, controllers, models, or non-test PHP code.
03 SKILL.md 正文
Pest Testing 4
Documentation
Use search-docs for detailed Pest 4 patterns and documentation.
Basic Usage
Creating Tests
All tests must be written using Pest. Use php artisan make:test --pest {name}.
Test Organization
- Unit/Feature tests:
tests/Featureandtests/Unitdirectories. - Browser tests:
tests/Browser/directory. - Do NOT remove tests without approval - these are core application code.
Basic Test Structure
Pest supports both test() and it() functions. Before writing new tests, check existing test files in the same directory to match the project's convention. Use test() if existing tests use test(), or it() if they use it().
<!-- Basic Pest Test Example -->
it('is true', function () {
expect(true)->toBeTrue();
});
Running Tests
- Run minimal tests with filter before finalizing:
php artisan test --compact --filter=testName. - Run all tests:
php artisan test --compact. - Run file:
php artisan test --compact tests/Feature/ExampleTest.php.
Assertions
Use specific assertions (assertSuccessful(), assertNotFound()) instead of assertStatus():
<!-- Pest Response Assertion -->
it('returns all', function () {
$this->postJson('/api/docs', [])->assertSuccessful();
});
| Use | Instead of |
|---|---|
assertSuccessful() |
assertStatus(200) |
assertNotFound() |
assertStatus(404) |
assertForbidden() |
assertStatus(403) |
Mocking
Import mock function before use: use function Pest\Laravel\mock;
Datasets
Use datasets for repetitive tests (validation rules, etc.):
<!-- Pest Dataset Example -->
it('has emails', function (string $email) {
expect($email)->not->toBeEmpty();
})->with([
'james' => 'james@laravel.com',
'taylor' => 'taylor@laravel.com',
]);
Pest 4 Features
| Feature | Purpose |
|---|---|
| Browser Testing | Full integration tests in real browsers |
| Smoke Testing | Validate multiple pages quickly |
| Visual Regression | Compare screenshots for visual changes |
| Test Sharding | Parallel CI runs |
| Architecture Testing | Enforce code conventions |
Browser Test Example
Browser tests run in real browsers for full integration testing:
- Browser tests live in
tests/Browser/. - Use Laravel features like
Event::fake(),assertAuthenticated(), and model factories. - Use
RefreshDatabasefor clean state per test. - Interact with page: click, type, scroll, select, submit, drag-and-drop, touch gestures.
- Test on multiple browsers (Chrome, Firefox, Safari) if requested.
- Test on different devices/viewports (iPhone 14 Pro, tablets) if requested.
- Switch color schemes (light/dark mode) when appropriate.
- Take screenshots or pause tests for debugging.
<!-- Pest Browser Test Example -->
it('may reset the password', function () {
Notification::fake();
$this->actingAs(User::factory()->create());
$page = visit('/sign-in');
$page->assertSee('Sign In')
->assertNoJavaScriptErrors()
->click('Forgot Password?')
->fill('email', 'nuno@laravel.com')
->click('Send Reset Link')
->assertSee('We have emailed your password reset link!');
Notification::assertSent(ResetPassword::class);
});
Smoke Testing
Quickly validate multiple pages have no JavaScript errors:
<!-- Pest Smoke Testing Example -->
$pages = visit(['/', '/about', '/contact']);
$pages->assertNoJavaScriptErrors()->assertNoConsoleLogs();
Visual Regression Testing
Capture and compare screenshots to detect visual changes.
Test Sharding
Split tests across parallel processes for faster CI runs.
Architecture Testing
Pest 4 includes architecture testing (from Pest 3):
<!-- Architecture Test Example -->
arch('controllers')
->expect('App\Http\Controllers')
->toExtendNothing()
->toHaveSuffix('Controller');
Common Pitfalls
- Not importing
use function Pest\Laravel\mock;before using mock - Using
assertStatus(200)instead ofassertSuccessful() - Forgetting datasets for repetitive validation tests
- Deleting tests without approval
- Forgetting
assertNoJavaScriptErrors()in browser tests
04 同仓库更多 Skills
Laravel 测试设计与评审
testing-best-practices
Laravel 测试设计与评审。适用于选择覆盖范围、测试命名或结构安排、选择断言或测试数据、隔离依赖、测试 HTTP 或安全边界、提升测试套件性能,以及评估测试价值。Pest 和 PHPUnit 语法请使用框架指南或 search-docs 查询。
Tailwind CSS 界面样式开发
tailwindcss-development
当用户消息以任何形式包含 'tailwind' 时必须调用。以下情形也应调用:构建响应式网格布局(多列卡片网格、商品网格)、flex/grid 页面结构(带侧边栏的仪表盘、固定顶栏、移动端切换导航)、UI 组件样式设计(卡片、表格、导航栏、定价区块、表单、输入框、徽章)、添加暗色模式变体、修复间距或排版问题,以及 Tailwind v3/v4 相关工作。核心用途:在 HTML 模板(Blade、JSX、Vue)中编写或修复 Tailwind 工具类。以下情形跳过:后端 PHP 逻辑、数据库查询、API 路由、不含 HTML/CSS 组件的 JavaScript、CSS 文件审查、构建工具配置,以及原生 CSS。
Laravel Slug 与自愈 URL 开发
sluggable-development
适用场景:让 Eloquent 模型的某个字段成为 slug、为 slug 列生成迁移、切换到自愈 URL,或在路由中根据 slug 解析模型。当请求提及 slug、永久链接、spatie/laravel-sluggable 包、HasSlug 或 HasTranslatableSlug trait、#[Sluggable] 特性、SlugOptions、findBySlug、自愈 URL,或 Laravel 项目中的旧 slug 重定向时触发。内容涵盖:特性(attribute)与 trait 的选择、生成唯一 slug、配置分隔符/长度/语言/作用域/唯一性、防止 slug 被覆盖、通过 spatie/laravel-translatable 实现多语言 slug、基于 slug 列的路由模型绑定、自愈路由键、通过 SelfHealing facade 自定义 308 重定向,以及通过 config/sluggable.php 覆盖默认操作。不适用于一次性 URL 安全字符串、签名 URL,或结果不存储到模型上的 Laravel 内置 Str::slug 辅助函数。
Laravel Pulse 监控配置与卡片开发
pulse-development
处理 Laravel Pulse 的安装配置与自定义卡片开发。在以下情形触发:安装 Pulse;配置仪表盘或授权门禁(Gate);设置记录器(recorder)与过滤规则;构建自定义 Livewire 卡片;通过 Redis 摄取或采样进行优化;或用户提及 /pulse、pulse:check、pulse:work、Pulse::record(),以及应用监控相关内容。
Laravel 媒体库开发
medialibrary-development
构建并使用 spatie/laravel-medialibrary 的功能,包括将文件与 Eloquent 模型关联、定义媒体集合与转换、生成响应式图片,以及获取媒体 URL 和路径。
Livewire 组件开发
livewire-development
适用于任何涉及 Livewire 的任务或问题。当用户提及 Livewire、wire: 指令,或 wire:model、wire:click、wire:sort、islands 等 Livewire 专有概念时,调用此技能。涵盖构建新组件、调试响应式问题、实时表单验证、拖放操作、加载状态、从 Livewire 3 迁移到 4、组件格式转换(SFC/MFC/基于类)以及性能优化。不适用于非 Livewire 的响应式 UI(React、Vue、仅 Alpine、Inertia.js),也不适用于不含 Livewire 的标准 Laravel 表单。