pest-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pest 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
search-docs
for detailed Pest 4 patterns and documentation.
使用
search-docs
查找Pest 4的详细模式和文档。

Basic 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:
    tests/Feature
    and
    tests/Unit
    directories.
  • Browser tests:
    tests/Browser/
    directory.
  • 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 (
assertSuccessful()
,
assertNotFound()
) instead of
assertStatus()
:
<code-snippet name="Pest Response Assertion" lang="php">
it('returns all', function () { $this->postJson('/api/docs', [])->assertSuccessful(); });
</code-snippet>
UseInstead of
assertSuccessful()
assertStatus(200)
assertNotFound()
assertStatus(404)
assertForbidden()
assertStatus(403)
使用特定断言方法(
assertSuccessful()
assertNotFound()
)替代
assertStatus()
<code-snippet name="Pest Response Assertion" lang="php">
it('returns all', function () { $this->postJson('/api/docs', [])->assertSuccessful(); });
</code-snippet>
推荐使用替代
assertSuccessful()
assertStatus(200)
assertNotFound()
assertStatus(404)
assertForbidden()
assertStatus(403)

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 核心功能

FeaturePurpose
Browser TestingFull integration tests in real browsers
Smoke TestingValidate multiple pages quickly
Visual RegressionCompare screenshots for visual changes
Test ShardingParallel CI runs
Architecture TestingEnforce 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()
    ,
    assertAuthenticated()
    , and model factories.
  • Use
    RefreshDatabase
    for 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.
<code-snippet name="Pest Browser Test Example" lang="php">
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、平板)上测试。
  • 适当切换配色方案(亮色/暗色模式)。
  • 可截取屏幕截图或暂停测试以进行调试。
<code-snippet name="Pest Browser Test Example" lang="php">
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
    use function Pest\Laravel\mock;
    before using mock
  • Using
    assertStatus(200)
    instead of
    assertSuccessful()
  • Forgetting datasets for repetitive validation tests
  • Deleting tests without approval
  • Forgetting
    assertNoJavascriptErrors()
    in browser tests
  • 使用mock函数前未导入
    use function Pest\Laravel\mock;
  • 使用
    assertStatus(200)
    而非
    assertSuccessful()
  • 针对重复的验证测试未使用数据集
  • 未经批准删除测试
  • 在浏览器测试中忘记添加
    assertNoJavascriptErrors()