134 lines
2.9 KiB
Plaintext
134 lines
2.9 KiB
Plaintext
---
|
||
description: "测试规范 — 测试原则 + 后端 PHPUnit/PHPStan + 覆盖率目标"
|
||
globs:
|
||
- "**/*.test.*"
|
||
- "**/*.spec.*"
|
||
- "**/__tests__/**"
|
||
- "**/vitest.config.*"
|
||
- "**/playwright.config.*"
|
||
- "Case-Database-Backend/tests/**/*.php"
|
||
- "Case-Database-Backend/phpunit.xml"
|
||
- "Case-Database-Backend/phpstan.neon"
|
||
alwaysApply: false
|
||
---
|
||
|
||
# Testing Standards
|
||
|
||
> 前端测试(Vitest / Vue Test Utils / Playwright)的具体工作流和模板
|
||
> 由 `skill-vue-testing.mdc` 自动注入,本文件不再重复。
|
||
|
||
## 通用原则
|
||
|
||
- 测试行为 (behavior),不测试实现细节
|
||
- 每个测试只验证一件事
|
||
- 不为覆盖率写无意义测试
|
||
- 测试是文档的一部分
|
||
|
||
---
|
||
|
||
## 后端测试 (PHP Hyperf)
|
||
|
||
### 测试金字塔
|
||
|
||
| 类型 | 占比 | 框架 | 目标 |
|
||
|------|------|------|------|
|
||
| Unit | 60% | PHPUnit | Service、Repository、工具类 |
|
||
| Integration | 30% | PHPUnit + TestContainer | API 端点、数据库交互 |
|
||
| Static | 持续 | PHPStan (Level max) | 类型安全、潜在 Bug |
|
||
|
||
### 覆盖率目标
|
||
|
||
- 行覆盖率 >= 75%
|
||
- 分支覆盖率 >= 70%
|
||
- Service 层 100%
|
||
- 关键业务路径 100%
|
||
|
||
### 运行命令
|
||
|
||
```bash
|
||
composer test # PHPUnit 所有测试
|
||
composer test:unit # 仅单元测试
|
||
composer test:feature # 仅功能测试 (API)
|
||
composer test:coverage # PHPUnit + 覆盖率
|
||
composer analyse # PHPStan 静态分析
|
||
```
|
||
|
||
### AAA 模式
|
||
|
||
```php
|
||
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace Tests\Unit\Service;
|
||
|
||
use App\Service\Production\OrderService;
|
||
use PHPUnit\Framework\TestCase;
|
||
|
||
class OrderServiceTest extends TestCase
|
||
{
|
||
public function testCalculateDiscountReturnsZeroBelowThreshold(): void
|
||
{
|
||
// Arrange
|
||
$service = new OrderService();
|
||
$amount = 99.0;
|
||
|
||
// Act
|
||
$discount = $service->calculateDiscount($amount);
|
||
|
||
// Assert
|
||
$this->assertEquals(0.0, $discount);
|
||
}
|
||
}
|
||
```
|
||
|
||
### PHPUnit 目录结构
|
||
|
||
```
|
||
tests/
|
||
├── Unit/ # 纯单元测试(不依赖框架)
|
||
│ ├── Service/
|
||
│ ├── Repository/
|
||
│ └── Utils/
|
||
├── Feature/ # 功能测试(HTTP API 级别)
|
||
│ ├── Auth/
|
||
│ ├── Production/
|
||
│ └── Permission/
|
||
├── bootstrap.php
|
||
└── phpunit.xml
|
||
```
|
||
|
||
### PHPStan 配置
|
||
|
||
```neon
|
||
# phpstan.neon
|
||
parameters:
|
||
level: max
|
||
paths:
|
||
- app
|
||
excludePaths:
|
||
- app/Command
|
||
ignoreErrors: []
|
||
```
|
||
|
||
---
|
||
|
||
## 新功能必须附带
|
||
|
||
### 前端
|
||
|
||
- [ ] Happy path 测试
|
||
- [ ] 边界条件测试
|
||
- [ ] 错误情况测试
|
||
- [ ] 权限/授权测试 (如涉及 v-auth / v-roles)
|
||
|
||
> 前端测试的具体写法见 `skill-vue-testing.mdc`
|
||
|
||
### 后端
|
||
|
||
- [ ] Service 单元测试
|
||
- [ ] API 功能测试 (HTTP 请求/响应)
|
||
- [ ] 权限/数据权限测试 (如涉及 DataScope)
|
||
- [ ] 事务回滚测试 (如涉及数据库写操作)
|
||
- [ ] PHPStan 通过 (`composer analyse`)
|