LingFlow - 综合测试架构
报告日期: 2026-03-09 项目名称: 中国传统文化知识库系统 测试架构: LingFlow综合测试框架 测试类型: 端到端、性能、安全、兼容性、稳定性
📊 测试架构概述
测试类型
| 测试类型 | 测试目标 | 执行方式 | 预计时间 |
|---|---|---|---|
| 端到端测试 | 完整用户流程 | Selenium/Playwright | 5-10分钟 |
| 实际性能测试 | 真实性能指标 | Lighthouse/JMeter | 10-20分钟 |
| 实际安全测试 | 安全漏洞扫描 | OWASP ZAP | 15-30分钟 |
| 兼容性测试 | 多平台兼容性 | BrowserStack | 20-40分钟 |
| 稳定性测试 | 长时间稳定性 | 自定义压力测试 | 30-60分钟 |
总体时间
- 最短时间: 80-120分钟 (1.3-2小时)
- 推荐时间: 150-240分钟 (2.5-4小时)
🏗️ 架构设计
1. 测试架构层次
┌─────────────────────────────────────────────────────────────┐
│ LingFlow综合测试架构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ 端到端测试 │ │ 性能测试 │ │ 安全测试 │ │
│ │ (E2E) │ │ (Performance)│ │ (Security) │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ 兼容性测试 │ │ 稳定性测试 │ │ 报告生成 │ │
│ │ (Compat) │ │ (Stability) │ │ (Report) │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
├─────────────────────────────────────────────────────────────┤
│ 测试协调层 │
│ (Test Coordinator) │
├─────────────────────────────────────────────────────────────┤
│ 基础设施层 │
│ (Infrastructure Layer) │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Selenium │ │ Lighthouse │ │ OWASP ZAP │ │
│ │ WebDriver │ │ CLI │ │ Scanner │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ BrowserStack│ │ JMeter │ │ Custom │ │
│ │ Cloud │ │ Load Test │ │ Stress Test │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
2. 核心组件
2.1 测试协调器
- 统一调度所有测试
- 管理测试依赖关系
- 收集测试结果
- 生成综合报告
2.2 基础设施层
- Selenium WebDriver: 端到端测试
- Lighthouse CLI: 性能测试
- OWASP ZAP: 安全测试
- BrowserStack Cloud: 兼容性测试
- JMeter: 负载测试
- Custom Stress Test: 稳定性测试
🧪 1. 端到端测试 (E2E Test)
测试目标
- 完整用户流程测试
- 跨页面功能测试
- 用户交互测试
- 数据流测试
测试场景
- 用户注册登录流程
- 用户注册
- 用户登录
-
用户登出
-
文献搜索流程
- 搜索中医文献
- 搜索佛学文献
-
搜索道家文献
-
文献浏览流程
- 浏览文献库
- 查看文献详情
-
下载文献
-
AI智能搜索流程
- 输入搜索关键词
- AI检索结果
-
查看相关推荐
-
系统监控流程
- 查看系统状态
- 查看资源监控
- 查看错误日志
测试工具
- Playwright: 现代化的端到端测试框架
- Selenium: 经典的浏览器自动化工具
- Cypress: 快速、可靠的测试框架
测试代码
from playwright.sync_api import sync_playwright
def test_user_registration_login():
"""测试用户注册登录流程"""
with sync_playwright() as p:
# 启动浏览器
browser = p.chromium.launch(headless=False)
page = browser.new_page()
# 访问首页
page.goto("http://localhost:3000")
# 测试用户注册
page.click("text=注册")
page.fill("input[name='username']", "test_user")
page.fill("input[name='email']", "test@example.com")
page.fill("input[name='password']", "password123")
page.click("button[type='submit']")
# 验证注册成功
assert page.is_visible("text=注册成功")
# 测试用户登录
page.click("text=登录")
page.fill("input[name='username']", "test_user")
page.fill("input[name='password']", "password123")
page.click("button[type='submit']")
# 验证登录成功
assert page.is_visible("text=欢迎,test_user")
# 测试用户登出
page.click("text=登出")
# 验证登出成功
assert page.is_visible("text=登录")
# 关闭浏览器
browser.close()
预计时间
- 单次执行: 5-10分钟
- 多次执行: 10-20分钟
🚀 2. 实际性能测试
测试目标
- 首屏加载性能
- API响应性能
- 资源加载性能
- 运行时性能
测试指标
| 指标 | 目标值 | 测试工具 |
|---|---|---|
| 首屏加载时间(LCP) | ≤2秒 | Lighthouse |
| 首次内容绘制(FCP) | ≤1秒 | Lighthouse |
| 首次输入延迟(FID) | ≤100ms | Lighthouse |
| 累积布局偏移(CLS) | ≤0.1 | Lighthouse |
| API响应时间 | ≤1.5秒 | JMeter |
| 资源加载大小 | ≤500KB | Lighthouse |
| 并发用户支持 | ≥100用户 | JMeter |
测试工具
- Lighthouse CLI: 性能指标测试
- WebPageTest: Web性能测试
- JMeter: 负载测试和压力测试
测试代码
import subprocess
import json
def test_lighthouse_performance():
"""使用Lighthouse测试性能"""
# 运行Lighthouse测试
result = subprocess.run([
"lighthouse",
"http://localhost:3000",
"--output=json",
"--output-path=./test_results/lighthouse_report",
"--chrome-flags=--headless"
], capture_output=True, text=True)
# 解析测试结果
with open("./test_results/lighthouse_report.report.json", 'r') as f:
report = json.load(f)
# 获取性能指标
lcp = report['audits']['largest-contentful-paint']['score']
fcp = report['audits']['first-contentful-paint']['score']
fid = report['audits']['max-potential-fid']['score']
cls = report['audits']['cumulative-layout-shift']['score']
performance_score = report['categories']['performance']['score']
# 验证性能指标
assert performance_score >= 90, f"性能分数不达标: {performance_score}"
assert lcp >= 0.9, f"LCP不达标: {lcp}"
assert fcp >= 0.9, f"FCP不达标: {fcp}"
assert fid >= 0.9, f"FID不达标: {fid}"
assert cls >= 0.9, f"CLS不达标: {cls}"
print(f"✅ 性能测试通过: {performance_score}/100")
def test_api_performance():
"""使用JMeter测试API性能"""
# 运行JMeter测试
result = subprocess.run([
"jmeter",
"-n",
"-t", "./test_jmx/api_performance_test.jmx",
"-l", "./test_results/api_performance_results.jtl",
"-e", "-o", "./test_results/api_performance_report"
], capture_output=True, text=True)
# 解析测试结果
# TODO: 解析JMeter结果
print("✅ API性能测试完成")
预计时间
- Lighthouse测试: 10-15分钟
- JMeter测试: 5-10分钟
- 总计: 10-20分钟
🔒 3. 实际安全测试
测试目标
- OWASP Top 10漏洞
- XSS攻击
- CSRF攻击
- SQL注入
- 敏感信息泄露
测试工具
- OWASP ZAP: 安全漏洞扫描
- Burp Suite: 安全测试工具
- SQLMap: SQL注入测试
测试代码
import subprocess
import json
def test_owasp_zap_security():
"""使用OWASP ZAP测试安全"""
# 启动ZAP代理
zap_proxy = subprocess.Popen([
"zap.sh",
"-daemon",
"-host", "0.0.0.0",
"-port", "8080",
"-config", "api.disablekey=true"
])
try:
# 等待ZAP启动
time.sleep(10)
# 运行ZAP扫描
result = subprocess.run([
"zap-cli",
"quick-scan",
"http://localhost:3000",
"-r", "./test_results/zap_report.html"
], capture_output=True, text=True)
# 解析ZAP报告
with open("./test_results/zap_report.html", 'r') as f:
report = f.read()
# 检查是否有高危漏洞
if "High Risk" in report or "Critical Risk" in report:
print("❌ 发现高危安全漏洞")
return False
else:
print("✅ 安全测试通过")
return True
finally:
# 关闭ZAP代理
zap_proxy.terminate()
def test_xss_attack():
"""测试XSS攻击"""
# 构造XSS攻击载荷
xss_payload = "<script>alert('XSS')</script>"
# 测试搜索功能
response = requests.post("http://localhost:3000/api/search", json={
"query": xss_payload
})
# 验证XSS防护
assert xss_payload not in response.text, "XSS攻击成功,防护失败"
assert "alert" not in response.text, "XSS攻击成功,防护失败"
print("✅ XSS防护测试通过")
def test_sql_injection():
"""测试SQL注入"""
# 构造SQL注入载荷
sql_payload = "' OR '1'='1"
# 测试登录功能
response = requests.post("http://localhost:3000/api/login", json={
"username": sql_payload,
"password": "password"
})
# 验证SQL注入防护
assert response.status_code == 400, "SQL注入成功,防护失败"
assert "invalid" in response.text.lower(), "SQL注入成功,防护失败"
print("✅ SQL注入防护测试通过")
预计时间
- OWASP ZAP扫描: 15-30分钟
- 其他安全测试: 5-10分钟
- 总计: 15-30分钟
🌐 4. 兼容性测试
测试目标
- 跨浏览器兼容性
- 跨设备兼容性
- 跨操作系统兼容性
测试环境
| 浏览器 | 版本 | 平台 | 设备 |
|---|---|---|---|
| Chrome | ≥100 | Windows/Mac/Linux | PC/Mobile |
| Firefox | ≥95 | Windows/Mac/Linux | PC/Mobile |
| Safari | ≥15 | Mac/iOS | PC/Mobile |
| Edge | ≥100 | Windows/Mac | PC/Mobile |
| 360浏览器 | ≥13 | Windows | PC |
| QQ浏览器 | ≥11 | Windows/Mac | PC |
测试工具
- BrowserStack Cloud: 真实设备云测试
- Sauce Labs: 跨浏览器测试
- LambdaTest: 兼容性测试
测试代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def test_cross_browser_compatibility():
"""测试跨浏览器兼容性"""
# 测试浏览器列表
browsers = [
("chrome", "chromedriver"),
("firefox", "geckodriver"),
("edge", "msedgedriver")
]
for browser_name, driver_name in browsers:
print(f"🌐 测试 {browser_name} 浏览器...")
# 创建浏览器驱动
if browser_name == "chrome":
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
elif browser_name == "firefox":
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("-headless")
driver = webdriver.Firefox(options=options)
elif browser_name == "edge":
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Edge(options=options)
try:
# 访问首页
driver.get("http://localhost:3000")
# 验证页面加载
assert "中国传统文化知识库" in driver.title
assert driver.find_element("tag", "h1").is_displayed()
# 测试核心功能
driver.find_element("input[placeholder='搜索...']").send_keys("中医")
driver.find_element("button[type='submit']").click()
# 验证搜索结果
assert len(driver.find_elements("class", "result-item")) > 0
print(f"✅ {browser_name} 浏览器测试通过")
except Exception as e:
print(f"❌ {browser_name} 浏览器测试失败: {e}")
finally:
# 关闭浏览器
driver.quit()
def test_responsive_design():
"""测试响应式设计"""
# 创建浏览器驱动
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
try:
# 访问首页
driver.get("http://localhost:3000")
# 测试不同屏幕尺寸
screen_sizes = [
(1920, 1080), # PC
(768, 1024), # Tablet
(375, 667) # Mobile
]
for width, height in screen_sizes:
print(f"📱 测试屏幕尺寸: {width}x{height}")
# 设置窗口大小
driver.set_window_size(width, height)
# 验证布局
if width >= 768:
# PC/Tablet布局
assert driver.find_element("class", "sidebar").is_displayed()
assert driver.find_element("class", "main-content").is_displayed()
else:
# Mobile布局
assert not driver.find_element("class", "sidebar").is_displayed()
assert driver.find_element("class", "mobile-menu").is_displayed()
print(f"✅ 屏幕尺寸 {width}x{height} 测试通过")
finally:
# 关闭浏览器
driver.quit()
预计时间
- 跨浏览器测试: 10-20分钟
- 响应式测试: 5-10分钟
- 总计: 20-40分钟
📊 5. 稳定性测试
测试目标
- 长时间运行稳定性
- 压力测试
- 故障恢复测试
- 内存泄漏测试
测试工具
- Custom Stress Test: 自定义压力测试
- JMeter: 负载测试
- Memory Profiler: 内存分析
测试代码
import time
import psutil
import requests
from concurrent.futures import ThreadPoolExecutor
def test_long_running_stability():
"""测试长时间运行稳定性"""
print("🔄 开始长时间稳定性测试...")
# 测试时间: 1小时
test_duration = 3600 # 秒
start_time = time.time()
error_count = 0
while time.time() - start_time < test_duration:
try:
# 随机测试API
api_url = f"http://localhost:3000/api/search"
response = requests.get(api_url, params={"query": "中医"})
if response.status_code != 200:
error_count += 1
print(f"❌ API错误: {response.status_code}")
# 测试内存泄漏
process = psutil.Process(os.getpid())
memory_info = process.memory_info()
print(f"📊 内存使用: {memory_info.rss / 1024 / 1024:.2f}MB")
# 间隔检查
time.sleep(60) # 每分钟检查一次
except Exception as e:
error_count += 1
print(f"❌ 测试异常: {e}")
# 验证稳定性
if error_count == 0:
print("✅ 长时间稳定性测试通过")
return True
else:
print(f"❌ 长时间稳定性测试失败,错误次数: {error_count}")
return False
def test_concurrent_users():
"""测试并发用户"""
print("👥 开始并发用户测试...")
# 并发用户数
concurrent_users = 100
# 测试API
def test_user_api(user_id):
try:
start_time = time.time()
response = requests.get(
"http://localhost:3000/api/search",
params={"query": f"用户{user_id}"}
)
duration = time.time() - start_time
return {
"user_id": user_id,
"status": response.status_code,
"duration": duration
}
except Exception as e:
return {
"user_id": user_id,
"status": 500,
"duration": 0,
"error": str(e)
}
# 创建线程池
with ThreadPoolExecutor(max_workers=concurrent_users) as executor:
# 提交所有任务
futures = [
executor.submit(test_user_api, user_id)
for user_id in range(concurrent_users)
]
# 等待所有任务完成
results = [future.result() for future in futures]
# 分析结果
success_count = sum(1 for r in results if r["status"] == 200)
avg_duration = sum(r["duration"] for r in results) / len(results)
max_duration = max(r["duration"] for r in results)
print(f"✅ 并发用户测试完成")
print(f" 成功: {success_count}/{concurrent_users}")
print(f" 平均响应时间: {avg_duration*1000:.2f}ms")
print(f" 最大响应时间: {max_duration*1000:.2f}ms")
# 验证并发性能
assert success_count >= 95, f"成功率过低: {success_count}/{concurrent_users}"
assert avg_duration <= 3, f"平均响应时间过长: {avg_duration}秒"
assert max_duration <= 5, f"最大响应时间过长: {max_duration}秒"
print("✅ 并发用户测试通过")
def test_fault_recovery():
"""测试故障恢复"""
print("🔧 开始故障恢复测试...")
# 1. 测试网络故障恢复
print("🌐 测试网络故障恢复...")
# 模拟网络故障
# TODO: 模拟网络故障
# 验证恢复
# TODO: 验证恢复
# 2. 测试数据库故障恢复
print("🗄️ 测试数据库故障恢复...")
# 模拟数据库故障
# TODO: 模拟数据库故障
# 验证恢复
# TODO: 验证恢复
# 3. 测试服务故障恢复
print("⚙️ 测试服务故障恢复...")
# 模拟服务故障
# TODO: 模拟服务故障
# 验证恢复
# TODO: 验证恢复
print("✅ 故障恢复测试完成")
预计时间
- 长时间稳定性测试: 30-60分钟
- 并发用户测试: 5-10分钟
- 故障恢复测试: 10-20分钟
- 总计: 30-60分钟
📊 测试执行流程
1. 测试准备
# 安装依赖
pip install playwright selenium lighthouse-jet-runner zaproxy
# 安装浏览器
playwright install
# 下载OWASP ZAP
wget https://github.com/zaproxy/zaproxy/releases/download/v2.13.0/ZAP_2.13.0_Linux.tar.gz
tar -xzf ZAP_2.13.0_Linux.tar.gz
2. 测试执行
from comprehensive_test_runner import ComprehensiveTestRunner
# 创建测试运行器
runner = ComprehensiveTestRunner()
# 运行所有测试
runner.run_all_tests()
# 生成测试报告
runner.generate_report()
3. 测试报告
测试报告包含以下内容:
- 测试摘要
- 总测试数
- 通过数
- 失败数
-
跳过数
-
各维度测试结果
- 端到端测试结果
- 性能测试结果
- 安全测试结果
- 兼容性测试结果
-
稳定性测试结果
-
详细测试结果
- 每个测试的详细结果
- 失败原因分析
- 优化建议
📊 测试结果示例
测试摘要
| 测试类型 | 总数 | 通过 | 失败 | 跳过 |
|---|---|---|---|---|
| 端到端测试 | 15 | 15 | 0 | 0 |
| 性能测试 | 20 | 18 | 2 | 0 |
| 安全测试 | 25 | 25 | 0 | 0 |
| 兼容性测试 | 30 | 28 | 2 | 0 |
| 稳定性测试 | 10 | 10 | 0 | 0 |
| 总计 | 100 | 96 | 4 | 0 |
测试结论
通过率: 96/100 (96%) 测试结论: ✅ 通过 上线建议: ✅ 建议上线
报告生成: 2026-03-09 测试架构: LingFlow综合测试框架 测试类型: 端到端、性能、安全、兼容性、稳定性 预计时间: 80-120分钟 (1.3-2小时)