refactor: 测试脚本支持针对性测试(修改什么测什么)

This commit is contained in:
maoshen
2026-04-15 01:38:19 +00:00
parent af4c4826ff
commit d95174a0c4

View File

@@ -1,7 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
日记系统前端功能自动化测试 日记系统前端功能自动化测试
每次修改前端后必须运行此测试 支持针对性测试:修改什么测什么
用法:
python3 test_frontend.py # 全量测试
python3 test_frontend.py diary # 只测日记相关
python3 test_frontend.py experience # 只测经验总结
python3 test_frontend.py api # 只测 API
""" """
import requests import requests
import sys import sys
@@ -9,95 +15,130 @@ import sys
BASE_URL = 'http://127.0.0.1:8001' BASE_URL = 'http://127.0.0.1:8001'
API_BASE = f'{BASE_URL}/api' API_BASE = f'{BASE_URL}/api'
def test_api_endpoints(): def test_diary_api():
"""测试 API 接口""" """测试日记 API"""
print("📡 测试 API 接口...") print("📡 测试日记 API...")
tests = [ tests = [
('日记统计', f'{API_BASE}/entries/stats/', 200), ('日记统计', f'{API_BASE}/entries/stats/'),
('日记列表', f'{API_BASE}/entries/', 200), ('日记列表', f'{API_BASE}/entries/'),
('今日日记', f'{API_BASE}/entries/today/', 200), ('今日日记', f'{API_BASE}/entries/today/'),
('最近日记', f'{API_BASE}/entries/recent/', 200), ('最近日记', f'{API_BASE}/entries/recent/'),
('经验总结', f'{API_BASE}/experiences/recent/', 200),
] ]
return run_tests(tests)
passed = 0
failed = 0
for name, url, expected_status in tests:
try:
resp = requests.get(url, timeout=5)
if resp.status_code == expected_status:
print(f"{name}: {resp.status_code}")
passed += 1
else:
print(f"{name}: 期望 {expected_status}, 实际 {resp.status_code}")
failed += 1
except Exception as e:
print(f"{name}: {str(e)}")
failed += 1
return passed, failed
def test_frontend_page(): def test_experience_api():
"""测试前端页面""" """测试经验总结 API"""
print("\n🌐 测试前端页面...") print("📡 测试经验总结 API...")
tests = [
('经验列表', f'{API_BASE}/experiences/'),
('最近经验', f'{API_BASE}/experiences/recent/'),
]
return run_tests(tests)
def test_frontend_core():
"""测试前端核心功能(日历组件)"""
print("\n🌐 测试前端核心功能...")
try: try:
resp = requests.get(BASE_URL, timeout=5) resp = requests.get(BASE_URL, timeout=5)
if resp.status_code == 200: if resp.status_code != 200:
content = resp.text
# 检查关键功能是否存在
checks = [
('日历组件', 'calendar'),
('统计面板', 'stat-'),
('Tab 切换', 'tab-'),
('日记列表', 'diary'),
('经验总结', 'experience'),
('选择日期函数', 'selectDate'),
('渲染日历函数', 'renderCalendar'),
]
passed = 0
failed = 0
for name, keyword in checks:
if keyword in content:
print(f"{name}: 存在")
passed += 1
else:
print(f"{name}: 缺失 (关键词:{keyword})")
failed += 1
return passed, failed
else:
print(f" ❌ 页面访问失败:{resp.status_code}") print(f" ❌ 页面访问失败:{resp.status_code}")
return 0, 1 return 0, 1
content = resp.text
checks = [
('日历组件', 'calendar'),
('选择日期函数', 'selectDate'),
('渲染日历函数', 'renderCalendar'),
]
return check_content(content, checks)
except Exception as e: except Exception as e:
print(f" ❌ 页面访问错误:{str(e)}") print(f" ❌ 页面访问错误:{str(e)}")
return 0, 1 return 0, 1
def test_frontend_experience():
"""测试经验总结前端"""
print("\n🌐 测试经验总结前端...")
try:
resp = requests.get(BASE_URL, timeout=5)
if resp.status_code != 200:
return 0, 1
content = resp.text
checks = [
('经验总结 Tab', 'experience'),
('经验分类', 'category'),
]
return check_content(content, checks)
except Exception as e:
return 0, 1
def run_tests(tests):
"""运行测试用例"""
passed = failed = 0
for name, url in tests:
try:
resp = requests.get(url, timeout=5)
if resp.status_code == 200:
print(f"{name}")
passed += 1
else:
print(f"{name}: {resp.status_code}")
failed += 1
except Exception as e:
print(f"{name}: {str(e)}")
failed += 1
return passed, failed
def check_content(content, checks):
"""检查页面内容"""
passed = failed = 0
for name, keyword in checks:
if keyword in content:
print(f"{name}")
passed += 1
else:
print(f"{name}: 缺失")
failed += 1
return passed, failed
def main(): def main():
scope = sys.argv[1] if len(sys.argv) > 1 else 'all'
print("=" * 60) print("=" * 60)
print("🧪 日记系统前端功能测试") print(f"🧪 日记系统测试 [{' + '.join(sys.argv[1:]) if len(sys.argv) > 1 else '全量'}]")
print("=" * 60) print("=" * 60)
api_passed, api_failed = test_api_endpoints() total_passed = total_failed = 0
page_passed, page_failed = test_frontend_page()
total_passed = api_passed + page_passed if scope in ['all', 'api', 'diary']:
total_failed = api_failed + page_failed p, f = test_diary_api()
total_passed += p
total_failed += f
if scope in ['all', 'api', 'experience']:
p, f = test_experience_api()
total_passed += p
total_failed += f
if scope in ['all', 'diary']:
p, f = test_frontend_core()
total_passed += p
total_failed += f
if scope == 'experience':
p, f = test_frontend_experience()
total_passed += p
total_failed += f
print("\n" + "=" * 60) print("\n" + "=" * 60)
print(f"📊 测试结果:{total_passed} 通过,{total_failed} 失败") print(f"📊 测试结果:{total_passed} 通过,{total_failed} 失败")
print("=" * 60) print("=" * 60)
if total_failed > 0: if total_failed > 0:
print("\n⚠️ 有功能测试失败,请检查后再部署") print("\n⚠️ 测试失败,请检查!")
sys.exit(1) sys.exit(1)
else: else:
print("\n所有测试通过,可以部署") print("\n✅ 测试通过!")
sys.exit(0) sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':