MBE四层生态系统自动化测试 - 快速入门
📅 创建时间
2026-02-02
🚀 快速开始
1. 环境准备
Python测试环境
# 安装Python测试依赖
pip install -r requirements-test.txt
# 或单独安装
pip install pytest pytest-asyncio pytest-cov httpx
Playwright E2E测试环境
# 进入测试目录
cd tests
# 安装Node依赖
npm install
# 安装Playwright浏览器
npx playwright install
2. 运行测试
运行所有Python测试
# 从项目根目录
pytest tests/ -v
运行特定层级测试
# L1 核心层测试
pytest tests/unit/l1_core/ -v
# L2 专家市场层测试
pytest tests/unit/l2_expert/ -v
# L3 应用市场层测试
pytest tests/unit/l3_app/ -v
# L4 用户层测试
pytest tests/unit/l4_user/ -v
运行权限测试
pytest tests/permission/ -v
运行业务流程测试
pytest tests/workflow/ -v
运行性能测试
pytest tests/performance/ -v -s
# 运行特定性能测试
pytest tests/performance/test_performance_benchmarks.py -v -s # API基准
pytest tests/performance/test_concurrent_load.py -v -s # 并发负载
pytest tests/performance/test_memory_monitoring.py -v -s # 内存监控
pytest tests/performance/test_layer_performance.py -v -s # 四层性能
运行跨层权限测试
pytest tests/permission/test_cross_layer_permissions.py -v
pytest tests/permission/test_permission_boundaries.py -v
运行四层业务流程测试
pytest tests/workflow/test_four_layer_workflows.py -v
pytest tests/workflow/test_exception_workflows.py -v
运行E2E测试
cd tests
npx playwright test
📁 测试目录结构
tests/
├── unit/ # 单元测试
│ ├── l1_core/ # L1 核心引擎测试
│ ├── l2_expert/ # L2 专家市场测试
│ ├── l3_app/ # L3 应用市场测试
│ └── l4_user/ # L4 用户层测试
│
├── permission/ # 权限测试
│ └── test_role_permissions.py # 角色权限矩阵测试
│
├── workflow/ # 业务流程测试
│ └── test_complete_workflows.py # 完整业务流程测试
│
├── performance/ # 性能测试
│ └── test_api_performance.py # API性能测试
│
├── e2e/ # E2E测试 (Playwright)
│ ├── l2_expert/ # 专家市场E2E
│ ├── l3_app/ # 应用开发者E2E
│ └── l4_user/ # 最终用户E2E
│ └── student/
│ └── learning.e2e.spec.ts
│
└── helpers/ # 辅助工具
└── four_layer_auth.helper.ts # 四层认证辅助
🧪 测试类型说明
1. 权限测试
测试四层架构中各角色的权限控制是否正确。
# 运行权限测试
pytest tests/permission/test_role_permissions.py -v
# 测试内容:
# - 角色权限矩阵验证
# - 角色层级关系验证
# - API访问权限验证
# - 跨层访问控制验证
2. 业务流程测试
测试完整的业务流程是否正常运行。
# 运行业务流程测试
pytest tests/workflow/test_complete_workflows.py -v
# 测试内容:
# - L2 专家发布流程
# - L3 应用开发流程
# - L4 学生学习流程
# - L4 教师管理流程
# - MBE五步行为分析
3. 性能测试
测试API响应时间和系统吞吐量。
# 运行性能测试
pytest tests/performance/test_api_performance.py -v -s
# 测试内容:
# - 健康检查性能
# - 专家路由性能
# - 专家问答性能
# - 并发用户测试
# - 突发流量测试
# - 持续负载测试
4. E2E测试
使用Playwright进行端到端测试。
# 运行E2E测试
cd tests
npx playwright test
# 运行特定测试文件
npx playwright test e2e/l4_user/student/learning.e2e.spec.ts
# 以UI模式运行
npx playwright test --ui
# 生成HTML报告
npx playwright test --reporter=html
⚙️ 配置说明
环境变量
# API基础URL
export MBE_TEST_API="http://localhost:8000"
# 前端测试URL
export TEST_URL="http://localhost:3000"
pytest配置 (pytest.ini)
[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
asyncio_mode = auto
addopts = -v --tb=short
markers =
slow: 标记慢速测试
integration: 标记集成测试
Playwright配置 (playwright.config.ts)
export default defineConfig({
testDir: './tests',
timeout: 30000,
use: {
baseURL: process.env.TEST_URL || 'http://localhost:3000',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
},
});
📊 测试覆盖的角色
L1 核心引擎层
- Core Developer (核心开发者)
L2 专家市场层
- Expert Creator (专家创建者)
- Expert Admin (专家管理员)
L3 应用市场层
- App Developer (应用开发者)
- App Publisher (应用发布者)
- App Admin (应用管理员)
L4 最终用户层
- Student (学生)
- Teacher (教师)
- Parent (家长)
- Professional (专业人士)
🔍 测试覆盖的业务流程
专家发布流程
- 创建专家
- 上传知识库
- 训练专家
- 提交审核
- 管理员审核
- 专家上架
应用开发流程
- 开发者注册
- 获取API Key
- 获取专家列表
- 调用专家API
- 沙盒测试
学生学习流程
- 登录系统
- 浏览课程
- 开始学习
- AI对话学习
- 完成练习
- 获得XP
- 查看进度
教师管理流程
- 查看班级
- 创建课程
- AI生成内容
- 查看分析
- 发送干预
📈 性能指标阈值
| 端点 | 平均延迟 | P95延迟 | P99延迟 |
|---|---|---|---|
| 健康检查 | < 100ms | < 200ms | < 500ms |
| 专家路由 | < 300ms | < 500ms | < 1000ms |
| 专家问答 | < 2000ms | < 5000ms | < 10000ms |
| 记忆读取 | < 100ms | < 200ms | < 500ms |
| 课程列表 | < 300ms | < 500ms | < 1000ms |
🛠️ 常用命令
# 运行所有测试
pytest tests/ -v
# 运行测试并生成覆盖率报告
pytest tests/ -v --cov=src --cov-report=html
# 只运行标记为slow的测试
pytest tests/ -v -m slow
# 跳过慢速测试
pytest tests/ -v -m "not slow"
# 运行失败重试
pytest tests/ -v --reruns 3
# 并行运行测试
pytest tests/ -v -n auto
# 生成JUnit XML报告
pytest tests/ -v --junitxml=test-results.xml
📝 添加新测试
1. 添加单元测试
# tests/unit/l1_core/test_new_feature.py
import pytest
class TestNewFeature:
def test_basic_functionality(self):
"""测试基本功能"""
assert True
@pytest.mark.asyncio
async def test_async_functionality(self):
"""测试异步功能"""
assert True
2. 添加E2E测试
// tests/e2e/l4_user/new_feature.e2e.spec.ts
import { test, expect } from '@playwright/test';
test.describe('新功能测试', () => {
test('测试用例', async ({ page }) => {
await page.goto('/new-feature');
await expect(page.getByText('功能标题')).toBeVisible();
});
});
🔗 相关文档
文档版本: v1.0
最后更新: 2026-02-02