关于 pest-testing
面向 Bagisto 开发者的 Pest 测试技能指南,涵盖测试组织、编写、运行和调试。内容包括各包的测试配置、断言、Mock、数据集及架构测试。
主要功能
- 01说明 Bagisto 测试目录与套件
- 02提供单元和功能测试命令
- 03指导新增包的测试注册
- 04涵盖断言、Mock 与数据集
- 05介绍架构测试和测试实践
适用场景
- 01为 Bagisto 包新增 Pest 测试
- 02运行指定套件、文件或筛选测试
- 03排查测试配置和失败问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bagisto/ agent-skills --skill pest-testing安装后在 Claude Code 中输入/pest-testing确认已加载。
详细介绍
在 Bagisto 中进行 Pest 测试
适用时机
在以下情况下启用此技能:
- 创建新测试(单元测试或功能测试)
- 修改现有测试
- 调试测试失败
- 使用数据集、模拟或测试组织
- 编写架构测试
- 测试 Bagisto 软件包(Admin、Shop、Core 等)
Bagisto 测试结构
测试位置
Bagisto 测试在 packages/Webkul/{Package}/tests/ 目录下按软件包组织:
packages/Webkul/
├── Admin/
│ └── tests/
│ ├── AdminTestCase.php # Base test case
│ ├── Concerns/
│ │ └── AdminTestBench.php # Test helpers
│ └── Feature/
│ ├── ExampleTest.php
│ └── ...
├── Shop/
│ └── tests/
│ ├── ShopTestCase.php
│ ├── Concerns/
│ │ └── ShopTestBench.php
│ └── Feature/
│ ├── Checkout/
│ │ └── CheckoutTest.php
│ └── ...
├── Core/
│ └── tests/
│ ├── CoreTestCase.php
│ ├── Concerns/
│ │ └── CoreAssertions.php
│ ├── Unit/
│ └── Feature/
├── DataGrid/
│ └── tests/
│ ├── DataGridTestCase.php
│ └── Unit/
└── Installer/
└── tests/
├── InstallerTestCase.php
└── Feature/
可用测试套件
Bagisto 在 phpunit.xml 中配置了以下测试套件:
| 测试套件 | 位置 | 命令 |
|---|---|---|
| Admin 功能测试 | packages/Webkul/Admin/tests/Feature | php artisan test --testsuite="Admin Feature Test" |
| Core 单元测试 | packages/Webkul/Core/tests/Unit | php artisan test --testsuite="Core Unit Test" |
| Customer 单元测试 | packages/Webkul/Customer/tests/Unit | php artisan test --testsuite="Customer Unit Test" |
| DataGrid 单元测试 | packages/Webkul/DataGrid/tests/Unit | php artisan test --testsuite="DataGrid Unit Test" |
| EUWithdrawal 功能测试 | packages/Webkul/EUWithdrawal/tests/Feature | php artisan test --testsuite="EUWithdrawal Feature Test" |
| Installer 功能测试 | packages/Webkul/Installer/tests/Feature | php artisan test --testsuite="Installer Feature Test" |
| PayU 单元测试 | packages/Webkul/PayU/tests/Unit | php artisan test --testsuite="PayU Unit Test" |
| PayU 功能测试 | packages/Webkul/PayU/tests/Feature | php artisan test --testsuite="PayU Feature Test" |
| Razorpay 单元测试 | packages/Webkul/Razorpay/tests/Unit | php artisan test --testsuite="Razorpay Unit Test" |
| Razorpay 功能测试 | packages/Webkul/Razorpay/tests/Feature | php artisan test --testsuite="Razorpay Feature Test" |
| Shop 功能测试 | packages/Webkul/Shop/tests/Feature | php artisan test --testsuite="Shop Feature Test" |
| Stripe 单元测试 | packages/Webkul/Stripe/tests/Unit | php artisan test --testsuite="Stripe Unit Test" |
| Stripe 功能测试 | packages/Webkul/Stripe/tests/Feature | php artisan test --testsuite="Stripe Feature Test" |
Pest.php 配置
Bagisto 使用 tests/Pest.php 为各个软件包注册测试用例:
<?php
uses(Webkul\Admin\Tests\AdminTestCase::class)->in('../packages/Webkul/Admin/tests');
uses(Webkul\Core\Tests\CoreTestCase::class)->in('../packages/Webkul/Core/tests');
uses(Webkul\Customer\Tests\CustomerTestCase::class)->in('../packages/Webkul/Customer/tests');
uses(Webkul\DataGrid\Tests\DataGridTestCase::class)->in('../packages/Webkul/DataGrid/tests');
uses(Webkul\EUWithdrawal\Tests\EUWithdrawalTestCase::class)->in('../packages/Webkul/EUWithdrawal/tests');
uses(Webkul\Installer\Tests\InstallerTestCase::class)->in('../packages/Webkul/Installer/tests');
uses(Webkul\Payment\Tests\PaymentTestCase::class)->in('../packages/Webkul/Payment/tests');
uses(Webkul\PayU\Tests\PayUTestCase::class)->in('../packages/Webkul/PayU/tests');
uses(Webkul\Razorpay\Tests\RazorpayTestCase::class)->in('../packages/Webkul/Razorpay/tests');
uses(Webkul\Shop\Tests\ShopTestCase::class)->in('../packages/Webkul/Shop/tests');
uses(Webkul\Stripe\Tests\StripeTestCase::class)->in('../packages/Webkul/Stripe/tests');
测试用例结构
每个软件包都有自己的测试用例,并继承 Tests\TestCase:
// packages/Webkul/Shop/tests/ShopTestCase.php
<?php
namespace Webkul\Shop\Tests;
use Tests\TestCase;
use Webkul\Core\Tests\Concerns\CoreAssertions;
use Webkul\Shop\Tests\Concerns\ShopTestBench;
class ShopTestCase extends TestCase
{
use CoreAssertions, ShopTestBench;
}
Composer.json 自动加载配置
生产环境自动加载
软件包命名空间在根目录的 composer.json 中注册:
"autoload": {
"psr-4": {
"Webkul\\Admin\\": "packages/Webkul/Admin/src",
"Webkul\\Shop\\": "packages/Webkul/Shop/src",
"Webkul\\Core\\": "packages/Webkul/Core/src",
...
}
}
开发环境自动加载
测试命名空间在 autoload-dev 中注册:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Webkul\\Admin\\Tests\\": "packages/Webkul/Admin/tests",
"Webkul\\Core\\Tests\\": "packages/Webkul/Core/tests",
"Webkul\\DataGrid\\Tests\\": "packages/Webkul/DataGrid/tests",
"Webkul\\Installer\\Tests\\": "packages/Webkul/Installer/tests",
"Webkul\\Shop\\Tests\\": "packages/Webkul/Shop/tests"
}
}
运行测试
运行所有测试
php artisan test --compact
运行指定测试套件
php artisan test --testsuite="Shop Feature Test"
php artisan test --testsuite="Admin Feature Test"
php artisan test --testsuite="Core Unit Test"
运行指定测试文件
php artisan test --compact packages/Webkul/Shop/tests/Feature/Checkout/CheckoutTest.php
使用筛选器运行测试
php artisan test --compact --filter=testName
运行指定软件包的测试
# Shop tests
php artisan test --compact packages/Webkul/Shop/tests/
# Admin tests
php artisan test --compact packages/Webkul/Admin/tests/
# Core tests
php artisan test --compact packages/Webkul/Core/tests/
创建新测试
创建功能测试
php artisan make:test --pest packages/Webkul/Shop/tests/Feature/Checkout/MyNewTest
创建单元测试
php artisan make:test --pest --unit packages/Webkul/Core/tests/Unit/MyNewTest
基本测试结构
<?php
namespace Webkul\Shop\Tests\Feature\Checkout;
use Webkul\Shop\Tests\ShopTestCase;
it('should pass basic test', function () {
expect(true)->toBeTrue();
});
it('should return successful response', function () {
$response = $this->getJson('/api/categories');
$response->assertStatus(200);
});
断言
使用特定断言(assertSuccessful()、assertNotFound()),而不是 assertStatus():
| 使用 | 替代 |
|---|---|
assertSuccessful() | assertStatus(200) |
assertNotFound() | assertStatus(404) |
assertForbidden() | assertStatus(403) |
模拟
使用前导入模拟函数:
use function Pest\Laravel\mock;
数据集
对重复测试使用数据集:
it('has valid emails', function (string $email) {
expect($email)->not->toBeEmpty();
})->with([
'james' => '[email protected]',
'john' => '[email protected]',
]);
架构测试
Pest 3 支持架构测试,可用于强制执行代码规范:
arch('controllers')
->expect('Webkul\Admin\Http\Controllers')
->toExtendNothing()
->toHaveSuffix('Controller');
arch('models')
->expect('Webkul\Core\Models')
->toExtend('Illuminate\Database\Eloquent\Model');
arch('no debugging')
->expect(['dd', 'dump', 'ray'])
->not->toBeUsed();
为新软件包添加测试
如果要为新软件包添加测试,需要:
- 在 Pest.php 中注册: 添加测试用例绑定:
uses(Webkul\NewPackage\Tests\NewPackageTestCase::class)->in('../packages/Webkul/NewPackage/tests');
- 在 composer.json(autoload-dev)中注册:
"autoload-dev": {
"psr-4": {
"Webkul\\NewPackage\\Tests\\": "packages/Webkul/NewPackage/tests"
}
}
- 在 phpunit.xml 中注册: 添加新的 testsuite:
<testsuite name="New Package Test">
<directory suffix="Test.php">packages/Webkul/NewPackage/tests</directory>
</testsuite>
- 运行 composer dump-autoload:
composer dump-autoload
常见陷阱
- 使用 mock 前未导入
use function Pest\Laravel\mock; - 使用
assertStatus(200),而不是assertSuccessful() - 添加测试命名空间后忘记运行
composer dump-autoload - 未在
tests/Pest.php中注册测试用例 - 未在
phpunit.xml中添加测试套件以进行包专属测试 - 未经批准就删除测试
- 忘记在 composer.json 的 autoload-dev 中注册测试命名空间
测试最佳实践
- 测试正常路径、失败路径和边缘情况。
- 在测试中使用工厂创建模型。
- 遵循包中现有的测试模式。
- 使用
$this->faker或fake()生成测试数据。 - 保持测试聚焦且相互独立。
评论
支持 Markdown