#!/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 sys BASE_URL = 'http://127.0.0.1:8001' API_BASE = f'{BASE_URL}/api' def test_diary_api(): """测试日记 API""" print("📡 测试日记 API...") tests = [ ('日记统计', f'{API_BASE}/entries/stats/'), ('日记列表', f'{API_BASE}/entries/'), ('今日日记', f'{API_BASE}/entries/today/'), ('最近日记', f'{API_BASE}/entries/recent/'), ] return run_tests(tests) def test_experience_api(): """测试经验总结 API""" print("📡 测试经验总结 API...") tests = [ ('经验列表', f'{API_BASE}/experiences/'), ('最近经验', f'{API_BASE}/experiences/recent/'), ] return run_tests(tests) def test_frontend_core(): """测试前端核心功能(日历组件)""" print("\n🌐 测试前端核心功能...") try: resp = requests.get(BASE_URL, timeout=5) if resp.status_code != 200: print(f" ❌ 页面访问失败:{resp.status_code}") return 0, 1 content = resp.text checks = [ ('日历组件', 'calendar'), ('选择日期函数', 'selectDate'), ('渲染日历函数', 'renderCalendar'), ] return check_content(content, checks) except Exception as e: print(f" ❌ 页面访问错误:{str(e)}") 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(): scope = sys.argv[1] if len(sys.argv) > 1 else 'all' print("=" * 60) print(f"🧪 日记系统测试 [{' + '.join(sys.argv[1:]) if len(sys.argv) > 1 else '全量'}]") print("=" * 60) total_passed = total_failed = 0 if scope in ['all', 'api', 'diary']: 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(f"📊 测试结果:{total_passed} 通过,{total_failed} 失败") print("=" * 60) if total_failed > 0: print("\n⚠️ 测试失败,请检查!") sys.exit(1) else: print("\n✅ 测试通过!") sys.exit(0) if __name__ == '__main__': main()