226 lines
5.1 KiB
Markdown
226 lines
5.1 KiB
Markdown
|
|
# 测试规范文档
|
||
|
|
|
||
|
|
## 📋 测试策略
|
||
|
|
|
||
|
|
### 测试层次
|
||
|
|
- **单元测试**: 覆盖率 >= 80%
|
||
|
|
- **集成测试**: 覆盖核心业务流程
|
||
|
|
- **接口测试**: 覆盖所有API端点
|
||
|
|
- **端到端测试**: 覆盖用户关键路径
|
||
|
|
|
||
|
|
### 测试框架
|
||
|
|
- **后端**: Jest + Supertest
|
||
|
|
- **前端**: Vitest + Vue Test Utils
|
||
|
|
- **接口**: Postman + Newman
|
||
|
|
- **性能**: JMeter
|
||
|
|
|
||
|
|
## 🧪 单元测试规范
|
||
|
|
|
||
|
|
### 测试文件命名
|
||
|
|
```
|
||
|
|
src/services/user.service.js -> tests/unit/services/user.service.test.js
|
||
|
|
src/utils/validator.js -> tests/unit/utils/validator.test.js
|
||
|
|
src/components/UserForm.vue -> tests/unit/components/UserForm.spec.ts
|
||
|
|
```
|
||
|
|
|
||
|
|
### 测试结构
|
||
|
|
```javascript
|
||
|
|
describe('模块名称', () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
// 测试前置操作
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('方法名称', () => {
|
||
|
|
it('should 预期行为 when 条件', () => {
|
||
|
|
// Arrange - 准备
|
||
|
|
// Act - 执行
|
||
|
|
// Assert - 断言
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
```
|
||
|
|
|
||
|
|
### 示例代码
|
||
|
|
```javascript
|
||
|
|
// 后端单元测试
|
||
|
|
describe('UserService', () => {
|
||
|
|
describe('createUser', () => {
|
||
|
|
it('should create user successfully', async () => {
|
||
|
|
const userData = { phone: '13800138000' };
|
||
|
|
const mockUser = { id: 1, ...userData };
|
||
|
|
User.create = jest.fn().mockResolvedValue(mockUser);
|
||
|
|
|
||
|
|
const result = await UserService.createUser(userData);
|
||
|
|
|
||
|
|
expect(User.create).toHaveBeenCalledWith(userData);
|
||
|
|
expect(result).toEqual(mockUser);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
// 前端组件测试
|
||
|
|
describe('UserForm', () => {
|
||
|
|
it('validates required fields', async () => {
|
||
|
|
const wrapper = mount(UserForm);
|
||
|
|
await wrapper.find('form').trigger('submit');
|
||
|
|
expect(wrapper.text()).toContain('请输入真实姓名');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
```
|
||
|
|
|
||
|
|
## 🔗 接口测试规范
|
||
|
|
|
||
|
|
### Postman测试集合
|
||
|
|
```javascript
|
||
|
|
// 认证接口测试
|
||
|
|
pm.test("登录成功", function () {
|
||
|
|
pm.response.to.have.status(200);
|
||
|
|
const response = pm.response.json();
|
||
|
|
pm.expect(response.code).to.eql(200);
|
||
|
|
pm.expect(response.data.token).to.not.be.empty;
|
||
|
|
|
||
|
|
// 保存token用于后续测试
|
||
|
|
pm.environment.set("authToken", response.data.token);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 业务接口测试
|
||
|
|
pm.test("创建订单成功", function () {
|
||
|
|
pm.response.to.have.status(200);
|
||
|
|
const response = pm.response.json();
|
||
|
|
pm.expect(response.data.id).to.be.a('number');
|
||
|
|
pm.environment.set("orderId", response.data.id);
|
||
|
|
});
|
||
|
|
```
|
||
|
|
|
||
|
|
### 自动化测试脚本
|
||
|
|
```bash
|
||
|
|
# 运行Postman测试
|
||
|
|
newman run tests/api/niumall-api.postman_collection.json \
|
||
|
|
-e tests/api/test-environment.json \
|
||
|
|
--reporters cli,html \
|
||
|
|
--reporter-html-export reports/api-test-report.html
|
||
|
|
```
|
||
|
|
|
||
|
|
## 🎯 端到端测试
|
||
|
|
|
||
|
|
### 关键用户路径
|
||
|
|
1. **用户注册登录流程**
|
||
|
|
2. **订单创建到完成流程**
|
||
|
|
3. **运输跟踪流程**
|
||
|
|
4. **支付结算流程**
|
||
|
|
|
||
|
|
### Cypress测试示例
|
||
|
|
```javascript
|
||
|
|
describe('订单管理流程', () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
cy.login('client'); // 自定义命令
|
||
|
|
});
|
||
|
|
|
||
|
|
it('客户可以创建订单', () => {
|
||
|
|
cy.visit('/orders/create');
|
||
|
|
cy.get('[data-cy=supplier-select]').select('供应商A');
|
||
|
|
cy.get('[data-cy=cattle-type]').type('肉牛');
|
||
|
|
cy.get('[data-cy=quantity]').type('10');
|
||
|
|
cy.get('[data-cy=submit-btn]').click();
|
||
|
|
|
||
|
|
cy.url().should('include', '/orders/');
|
||
|
|
cy.contains('订单创建成功');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
```
|
||
|
|
|
||
|
|
## 📊 性能测试
|
||
|
|
|
||
|
|
### JMeter测试计划
|
||
|
|
```xml
|
||
|
|
<!-- 并发用户登录测试 -->
|
||
|
|
<ThreadGroup>
|
||
|
|
<elementProp name="ThreadGroup.arguments" elementType="Arguments"/>
|
||
|
|
<stringProp name="ThreadGroup.num_threads">100</stringProp>
|
||
|
|
<stringProp name="ThreadGroup.ramp_time">60</stringProp>
|
||
|
|
<stringProp name="ThreadGroup.duration">300</stringProp>
|
||
|
|
</ThreadGroup>
|
||
|
|
```
|
||
|
|
|
||
|
|
### 性能指标要求
|
||
|
|
- **响应时间**: API < 200ms, 页面 < 3s
|
||
|
|
- **并发用户**: 支持1000并发
|
||
|
|
- **错误率**: < 0.1%
|
||
|
|
- **TPS**: >= 500
|
||
|
|
|
||
|
|
## 🔍 质量检查
|
||
|
|
|
||
|
|
### 代码覆盖率
|
||
|
|
```bash
|
||
|
|
# 后端覆盖率
|
||
|
|
npm run test:coverage
|
||
|
|
|
||
|
|
# 前端覆盖率
|
||
|
|
npm run test:unit -- --coverage
|
||
|
|
```
|
||
|
|
|
||
|
|
### 测试执行
|
||
|
|
```bash
|
||
|
|
# 运行所有测试
|
||
|
|
npm test
|
||
|
|
|
||
|
|
# 运行单元测试
|
||
|
|
npm run test:unit
|
||
|
|
|
||
|
|
# 运行集成测试
|
||
|
|
npm run test:integration
|
||
|
|
|
||
|
|
# 运行E2E测试
|
||
|
|
npm run test:e2e
|
||
|
|
```
|
||
|
|
|
||
|
|
### CI/CD集成
|
||
|
|
```yaml
|
||
|
|
# .github/workflows/test.yml
|
||
|
|
name: Test Pipeline
|
||
|
|
on: [push, pull_request]
|
||
|
|
|
||
|
|
jobs:
|
||
|
|
test:
|
||
|
|
runs-on: ubuntu-latest
|
||
|
|
steps:
|
||
|
|
- uses: actions/checkout@v2
|
||
|
|
- name: Setup Node.js
|
||
|
|
uses: actions/setup-node@v2
|
||
|
|
with:
|
||
|
|
node-version: '18'
|
||
|
|
|
||
|
|
- name: Install dependencies
|
||
|
|
run: npm ci
|
||
|
|
|
||
|
|
- name: Run unit tests
|
||
|
|
run: npm run test:unit
|
||
|
|
|
||
|
|
- name: Run integration tests
|
||
|
|
run: npm run test:integration
|
||
|
|
|
||
|
|
- name: Upload coverage
|
||
|
|
uses: codecov/codecov-action@v2
|
||
|
|
```
|
||
|
|
|
||
|
|
## 📋 测试检查清单
|
||
|
|
|
||
|
|
### 提交前检查
|
||
|
|
- [ ] 所有单元测试通过
|
||
|
|
- [ ] 代码覆盖率 >= 80%
|
||
|
|
- [ ] 集成测试通过
|
||
|
|
- [ ] API测试通过
|
||
|
|
- [ ] 无ESLint错误
|
||
|
|
|
||
|
|
### 发布前检查
|
||
|
|
- [ ] 端到端测试通过
|
||
|
|
- [ ] 性能测试达标
|
||
|
|
- [ ] 安全扫描通过
|
||
|
|
- [ ] 浏览器兼容性测试
|
||
|
|
- [ ] 移动端适配测试
|
||
|
|
|
||
|
|
## 📞 测试支持
|
||
|
|
|
||
|
|
- **测试负责人**: test@niumall.com
|
||
|
|
- **质量保证**: qa@niumall.com
|
||
|
|
- **性能测试**: perf@niumall.com
|