pest-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePest Testing 4
Pest 4 测试
When to Apply
适用场景
Activate this skill when:
- Creating new tests (unit, feature, or browser)
- Modifying existing tests
- Debugging test failures
- Working with browser testing or smoke testing
- Writing architecture tests or visual regression tests
在以下场景中激活此技能:
- 创建新测试(单元、功能或浏览器测试)
- 修改现有测试
- 调试测试失败问题
- 进行浏览器测试或冒烟测试
- 编写架构测试或视觉回归测试
Documentation
文档参考
Use for detailed Pest 4 patterns and documentation.
search-docs使用查找Pest 4的详细模式和文档。
search-docsBasic Usage
基础用法
Creating Tests
创建测试
All tests must be written using Pest. Use .
php artisan make:test --pest {name}所有测试必须使用Pest编写。使用命令。
php artisan make:test --pest {name}Test Organization
测试组织规范
- Unit/Feature tests: and
tests/Featuredirectories.tests/Unit - Browser tests: directory.
tests/Browser/ - Do NOT remove tests without approval - these are core application code.
- 单元/功能测试:存放于和
tests/Feature目录。tests/Unit - 浏览器测试:存放于目录。
tests/Browser/ - 未经批准不得删除测试——这些是应用程序的核心代码部分。
Basic Test Structure
基础测试结构
<code-snippet name="Basic Pest Test Example" lang="php">
it('is true', function () {
expect(true)->toBeTrue();
});
</code-snippet><code-snippet name="Basic Pest Test Example" lang="php">
it('is true', function () {
expect(true)->toBeTrue();
});
</code-snippet>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
- 最终确定前,使用过滤器运行最小范围测试:。
php artisan test --compact --filter=testName - 运行所有测试:。
php artisan test --compact - 运行单个文件:。
php artisan test --compact tests/Feature/ExampleTest.php
Assertions
断言使用
Use specific assertions (, ) instead of :
<code-snippet name="Pest Response Assertion" lang="php">
assertSuccessful()assertNotFound()assertStatus()it('returns all', function () {
$this->postJson('/api/docs', [])->assertSuccessful();
});
</code-snippet>
| Use | Instead of |
|---|---|
| |
| |
| |
使用特定断言方法(、)替代:
<code-snippet name="Pest Response Assertion" lang="php">
assertSuccessful()assertNotFound()assertStatus()it('returns all', function () {
$this->postJson('/api/docs', [])->assertSuccessful();
});
</code-snippet>
| 推荐使用 | 替代 |
|---|---|
| |
| |
| |
Mocking
模拟操作
Import mock function before use:
use function Pest\Laravel\mock;使用模拟函数前需导入:
use function Pest\Laravel\mock;Datasets
数据集使用
Use datasets for repetitive tests (validation rules, etc.):
<code-snippet name="Pest Dataset Example" lang="php">
it('has emails', function (string $email) {
expect($email)->not->toBeEmpty();
})->with([
'james' => 'james@laravel.com',
'taylor' => 'taylor@laravel.com',
]);
</code-snippet>针对重复测试(如验证规则等)使用数据集:
<code-snippet name="Pest Dataset Example" lang="php">
it('has emails', function (string $email) {
expect($email)->not->toBeEmpty();
})->with([
'james' => 'james@laravel.com',
'taylor' => 'taylor@laravel.com',
]);
</code-snippet>Pest 4 Features
Pest 4 核心功能
| 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 |
| 功能 | 用途 |
|---|---|
| 浏览器测试 | 在真实浏览器中进行完整集成测试 |
| 冒烟测试 | 快速验证多个页面 |
| 视觉回归测试 | 对比截图检测视觉变化 |
| 测试分片 | 并行CI运行 |
| 架构测试 | 强制执行代码规范 |
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(), and model factories.assertAuthenticated() - Use for clean state per test.
RefreshDatabase - 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.
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);});
</code-snippet>浏览器测试在真实浏览器中运行,用于完整集成测试:
- 浏览器测试存放于目录。
tests/Browser/ - 使用Laravel特性,如、
Event::fake()和模型工厂。assertAuthenticated() - 使用确保每个测试拥有干净的环境。
RefreshDatabase - 与页面交互:点击、输入、滚动、选择、提交、拖放、触摸手势。
- 若有要求,在多个浏览器(Chrome、Firefox、Safari)上测试。
- 若有要求,在不同设备/视口(iPhone 14 Pro、平板)上测试。
- 适当切换配色方案(亮色/暗色模式)。
- 可截取屏幕截图或暂停测试以进行调试。
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);});
</code-snippet>Smoke Testing
冒烟测试
Quickly validate multiple pages have no JavaScript errors:
<code-snippet name="Pest Smoke Testing Example" lang="php">
$pages = visit(['/', '/about', '/contact']);
$pages->assertNoJavascriptErrors()->assertNoConsoleLogs();
</code-snippet>快速验证多个页面无JavaScript错误:
<code-snippet name="Pest Smoke Testing Example" lang="php">
$pages = visit(['/', '/about', '/contact']);
$pages->assertNoJavascriptErrors()->assertNoConsoleLogs();
</code-snippet>Visual Regression Testing
视觉回归测试
Capture and compare screenshots to detect visual changes.
捕获并对比截图以检测视觉变化。
Test Sharding
测试分片
Split tests across parallel processes for faster CI runs.
将测试拆分到多个并行进程中,以加快CI运行速度。
Architecture Testing
架构测试
Pest 4 includes architecture testing (from Pest 3):
<code-snippet name="Architecture Test Example" lang="php">
arch('controllers')
->expect('App\Http\Controllers')
->toExtendNothing()
->toHaveSuffix('Controller');
</code-snippet>Pest 4包含架构测试功能(继承自Pest 3):
<code-snippet name="Architecture Test Example" lang="php">
arch('controllers')
->expect('App\Http\Controllers')
->toExtendNothing()
->toHaveSuffix('Controller');
</code-snippet>Common Pitfalls
常见误区
- Not importing before using mock
use function Pest\Laravel\mock; - Using instead of
assertStatus(200)assertSuccessful() - Forgetting datasets for repetitive validation tests
- Deleting tests without approval
- Forgetting in browser tests
assertNoJavascriptErrors()
- 使用mock函数前未导入
use function Pest\Laravel\mock; - 使用而非
assertStatus(200)assertSuccessful() - 针对重复的验证测试未使用数据集
- 未经批准删除测试
- 在浏览器测试中忘记添加
assertNoJavascriptErrors()