2.7 KiB
2.7 KiB
🧪 Testing Strategy
测试策略指南,AI 在编写测试时参考 (PHP Hyperf + Vue 3 双栈)
前端测试框架
| 类型 | 框架 | 配置文件 |
|---|---|---|
| 单元测试 | Vitest | vitest.config.ts |
| 集成测试 | Vitest + MSW | — |
| E2E 测试 | Playwright | playwright.config.ts |
后端测试框架
| 类型 | 框架 | 配置文件 |
|---|---|---|
| 单元测试 | PHPUnit | phpunit.xml |
| 功能测试 | PHPUnit (HTTP) | phpunit.xml |
| 静态分析 | PHPStan | phpstan.neon |
| 代码规范 | PHP CS Fixer | .php-cs-fixer.php |
测试金字塔目标
前端
- Unit: 70% (快速、大量)
- Integration: 20% (模块协作)
- E2E: 10% (关键用户路径)
后端
- Unit: 60% (Service、Repository、工具类)
- Integration: 30% (API 端点、数据库交互)
- Static Analysis: 持续 (PHPStan Level max)
覆盖率目标
| 维度 | 前端 | 后端 |
|---|---|---|
| 行覆盖 | ≥ 80% | ≥ 75% |
| 分支覆盖 | ≥ 75% | ≥ 70% |
| 关键路径 | 100% | 100% |
| Service 层 | — | 100% |
运行命令
前端
npm run test # 运行所有单元测试
npm run test:watch # 监听模式
npm run test:coverage # 覆盖率报告
npm run test:e2e # E2E 测试 (Playwright)
后端
composer test # PHPUnit 所有测试
composer test:unit # 仅单元测试
composer test:feature # 仅功能测试 (API)
composer test:coverage # PHPUnit + 覆盖率报告
composer analyse # PHPStan 静态分析
composer cs-fix # PHP CS Fixer 格式化
后端测试目录结构
tests/
├── Unit/ # 纯单元测试(不依赖框架)
│ ├── Service/
│ │ ├── OrderServiceTest.php
│ │ └── PaymentServiceTest.php
│ ├── Repository/
│ └── Utils/
├── Feature/ # 功能测试(HTTP API 级别)
│ ├── Auth/
│ │ ├── LoginTest.php
│ │ └── PermissionTest.php
│ ├── Production/
│ │ └── OrderApiTest.php
│ └── System/
│ └── HealthCheckTest.php
├── bootstrap.php
└── phpunit.xml
CI/CD 中的测试
# .github/workflows/ci.yml 中双栈测试
jobs:
frontend-test:
steps:
- npm ci
- npm run lint
- npm run type-check
- npm run test:coverage
backend-test:
services:
mysql:
image: mysql:8
redis:
image: redis:7-alpine
steps:
- composer install
- composer analyse # PHPStan
- composer test # PHPUnit
最后更新: 2026-02-24