| 版本 | 日期 | 描述 | 作者 |
|---|---|---|---|
| 4.0 | 2025-10-31 | 基于实际测试结构更新Taro小程序测试策略,添加最新测试模式 | Winston |
| 3.2 | 2025-10-20 | 修正Taro测试工具引用,更新为实际使用的测试框架 | Winston |
| 3.1 | 2025-10-16 | 修正测试路径描述,更新Taro测试说明 | Winston |
| 3.0 | 2025-10-15 | 更新为出行服务项目测试策略 | Winston |
| 2.6 | 2025-10-15 | 完成遗留测试文件迁移到统一的tests目录结构 | Winston |
| 2.5 | 2025-10-14 | 更新测试文件位置到统一的tests目录结构 | Claude |
| 2.4 | 2025-09-20 | 更新测试策略与主架构文档版本一致 | Winston |
本文档定义了出行服务项目的完整测试策略,基于现有的测试基础设施和最佳实践。测试策略遵循测试金字塔模型,确保代码质量、功能稳定性和系统可靠性。
注意: 本测试策略主要针对主项目(后端API + React管理后台),Taro小程序项目有独立的测试体系,详见 Taro测试规范文档。
web/tests/unit/client/**/*.test.{ts,tsx}web/tests/unit/server/**/*.test.{ts,tsx}web/tests/integration/client/**/*.test.{ts,tsx}web/tests/integration/server/**/*.test.{ts,tsx}web/tests/e2e/**/*.test.{ts,tsx}注意: Taro小程序项目位于 mini/ 目录,是独立的项目结构,拥有自己的测试配置和运行方式。基于实际测试结构分析,当前测试体系如下:
mini/tests/components/**/*.test.{ts,tsx}mini/tests/pages/**/*.test.{ts,tsx}mini/tests/unit/**/*.test.{ts,tsx}mini/tests/setup.ts - 完整的Taro组件和API mockmini/tests/utils.ts - 测试辅助函数和mock数据生成器mini/tests/__mocks__/ - Taro API和组件mock
taroMock.ts - 统一的Taro API mock,包含所有常用APItaroMock.ts - 通过jest.config.js的moduleNameMapper重定向@tarojs/taromini/tests/__snapshots__/ - UI快照测试// vitest.config.ts - 开发环境配置
export default defineConfig({
test: {
projects: [
// Node.js 环境项目 - 后端测试
{
test: {
include: [
'tests/unit/server/**/*.test.{ts,js}',
'tests/integration/server/**/*.test.{ts,js}'
],
// ... 其他配置
}
},
// Happy DOM 环境项目 - 前端组件测试
{
test: {
include: [
'tests/unit/client/**/*.test.{ts,js,tsx,jsx}',
'tests/integration/client/**/*.test.{ts,js,tsx,jsx}'
],
// ... 其他配置
}
}
]
}
});
# GitHub Actions 测试配置
name: Test Pipeline
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- run: npm run test:api
- run: npm run test:components
integration-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_db
steps:
- run: npm run test:integration
e2e-tests:
runs-on: ubuntu-latest
steps:
- run: npm run test:e2e:chromium
| 测试类型 | 最低要求 | 目标要求 | 关键模块要求 |
|---|---|---|---|
| 单元测试 | 70% | 80% | 90% |
| 集成测试 | 50% | 60% | 70% |
| E2E测试 | 关键流程100% | 主要流程80% | - |
// 测试数据工厂模式
export function createTestUser(overrides = {}): User {
return {
id: 1,
username: 'testuser',
email: 'test@example.com',
createdAt: new Date(),
...overrides
};
}
// 使用示例
const adminUser = createTestUser({ role: 'admin' });
const inactiveUser = createTestUser({ active: false });
# 运行所有测试
npm test
# 运行单元测试
npm run test:unit
# 运行集成测试
npm run test:integration
# 运行E2E测试
npm run test:e2e:chromium
# 生成覆盖率报告
npm run test:coverage
# 进入mini目录
cd mini
# 运行所有Taro测试
pnpm test
# 运行H5环境测试
pnpm run test:h5
# 运行微信小程序环境测试
pnpm run test:weapp
# 生成覆盖率报告
pnpm run test:coverage
# 运行特定测试文件
pnpm test -- order-page.test.tsx
pnpm test -- profile.test.tsx
基于最新的 order-page.test.tsx 和 profile.test.tsx 测试文件,推荐以下测试模式:
// 1. 完整的测试环境设置
const createTestQueryClient = () => new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
// 2. 组件包装器
const Wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={createTestQueryClient()}>
{children}
</QueryClientProvider>
)
// 3. 完整的Mock策略 - 使用统一的taroMock
import taroMock from '../../tests/__mocks__/taroMock'
// 在测试中直接使用导入的mock函数
taroMock.showToast.mockClear()
taroMock.navigateTo.mockClear()
// 4. 测试数据工厂
const mockRouteData = {
id: 1,
name: '测试路线',
price: 100,
vehicleType: '商务车'
}
taroMock.ts 统一管理所有Taro API mock// 良好的测试示例 - 基于最新测试文件
describe('OrderPage', () => {
beforeEach(() => {
jest.clearAllMocks()
// 设置测试数据
})
it('应该正确渲染订单页面', () => {
render(
<Wrapper>
<OrderPage />
</Wrapper>
)
expect(screen.getByTestId('order-navbar')).toBeInTheDocument()
expect(screen.getByTestId('activity-name')).toHaveTextContent('测试活动')
})
it('应该处理支付验证逻辑', async () => {
render(
<Wrapper>
<OrderPage />
</Wrapper>
)
const payButton = screen.getByTestId('pay-button')
fireEvent.click(payButton)
await waitFor(() => {
expect(taroMock.showToast).toHaveBeenCalledWith({
title: '请先获取手机号',
icon: 'none',
duration: 2000
})
})
})
it('应该处理不同出行模式', () => {
// 测试拼车和包车模式
mockUseRouter.mockReturnValue({
params: { type: 'carpool' }
})
render(
<Wrapper>
<OrderPage />
</Wrapper>
)
expect(screen.getByTestId('service-type')).toHaveTextContent('班次信息')
})
})
[page/component].test.tsx 或 [feature].test.ts| 日期 | 版本 | 描述 |
|---|---|---|
| 2025-10-31 | 4.0 | 基于实际测试结构更新Taro小程序测试策略,添加最新测试模式 |
| 2025-10-20 | 3.2 | 修正Taro测试工具引用,更新为实际使用的测试框架 |
| 2025-10-16 | 3.1 | 修正测试路径描述,更新Taro测试说明 |
| 2025-10-15 | 3.0 | 更新为出行服务项目测试策略 |
| 2025-10-15 | 2.6 | 完成遗留测试文件迁移到统一的tests目录结构 |
| 2025-10-14 | 2.5 | 重构测试文件结构,统一到tests目录 |
| 2025-09-20 | 2.4 | 更新版本与主架构文档一致 |
| 2025-09-19 | 1.0 | 初始版本,基于现有测试基础设施 |
文档状态: 正式版 下次评审: 2025-12-31