From d95174a0c4b3c6cef4469015dcc138e2f1eac7ed Mon Sep 17 00:00:00 2001 From: maoshen Date: Wed, 15 Apr 2026 01:38:19 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B5=8B=E8=AF=95=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=94=AF=E6=8C=81=E9=92=88=E5=AF=B9=E6=80=A7=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=EF=BC=88=E4=BF=AE=E6=94=B9=E4=BB=80=E4=B9=88=E6=B5=8B?= =?UTF-8?q?=E4=BB=80=E4=B9=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_frontend.py | 173 +++++++++++++++++++++++++++++------------------ 1 file changed, 107 insertions(+), 66 deletions(-) diff --git a/test_frontend.py b/test_frontend.py index df3644e..7f124fc 100644 --- a/test_frontend.py +++ b/test_frontend.py @@ -1,7 +1,13 @@ #!/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 @@ -9,95 +15,130 @@ import sys BASE_URL = 'http://127.0.0.1:8001' API_BASE = f'{BASE_URL}/api' -def test_api_endpoints(): - """测试 API 接口""" - print("📡 测试 API 接口...") - +def test_diary_api(): + """测试日记 API""" + print("📡 测试日记 API...") tests = [ - ('日记统计', f'{API_BASE}/entries/stats/', 200), - ('日记列表', f'{API_BASE}/entries/', 200), - ('今日日记', f'{API_BASE}/entries/today/', 200), - ('最近日记', f'{API_BASE}/entries/recent/', 200), - ('经验总结', f'{API_BASE}/experiences/recent/', 200), + ('日记统计', f'{API_BASE}/entries/stats/'), + ('日记列表', f'{API_BASE}/entries/'), + ('今日日记', f'{API_BASE}/entries/today/'), + ('最近日记', f'{API_BASE}/entries/recent/'), ] - - 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 + return run_tests(tests) -def test_frontend_page(): - """测试前端页面""" - print("\n🌐 测试前端页面...") - +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: - 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: + 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("🧪 日记系统前端功能测试") + print(f"🧪 日记系统测试 [{' + '.join(sys.argv[1:]) if len(sys.argv) > 1 else '全量'}]") print("=" * 60) - api_passed, api_failed = test_api_endpoints() - page_passed, page_failed = test_frontend_page() + total_passed = total_failed = 0 - total_passed = api_passed + page_passed - total_failed = api_failed + page_failed + 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⚠️ 有功能测试失败,请检查后再部署!") + print("\n⚠️ 测试失败,请检查!") sys.exit(1) else: - print("\n✅ 所有测试通过,可以部署!") + print("\n✅ 测试通过!") sys.exit(0) if __name__ == '__main__':