更新代码
Showing
1 changed file
with
41 additions
and
15 deletions
| ... | @@ -80,7 +80,7 @@ def run_api_test(cases): | ... | @@ -80,7 +80,7 @@ def run_api_test(cases): |
| 80 | # 新增:根据env_scope和当前环境判断是否执行 | 80 | # 新增:根据env_scope和当前环境判断是否执行 |
| 81 | env_scope = str(case.get('env_scope', '')).strip() | 81 | env_scope = str(case.get('env_scope', '')).strip() |
| 82 | if not should_run_in_env(env_scope, current_env): | 82 | if not should_run_in_env(env_scope, current_env): |
| 83 | print(f"⏭️ 跳过用例 case_id: {case.get('case_id')},原因:不在{current_env}环境执行范围内 (env_scope: {env_scope})") | 83 | print(f"[SKIP] case_id: {case.get('case_id')} - not in {current_env} env (env_scope: {env_scope})") |
| 84 | continue | 84 | continue |
| 85 | 85 | ||
| 86 | # 变量替换,优先用ip_host+path拼接url | 86 | # 变量替换,优先用ip_host+path拼接url |
| ... | @@ -476,27 +476,53 @@ def read_cases_from_excel(file_path, sheet_name='Sheet1'): | ... | @@ -476,27 +476,53 @@ def read_cases_from_excel(file_path, sheet_name='Sheet1'): |
| 476 | 476 | ||
| 477 | if __name__ == "__main__": | 477 | if __name__ == "__main__": |
| 478 | import sys | 478 | import sys |
| 479 | import glob | ||
| 479 | 480 | ||
| 480 | # 删除旧的测试报告 | 481 | # 创建按环境分类的报告目录并清理旧报告 |
| 481 | for f in os.listdir('.'): | 482 | deploy_env = get_deploy_env() |
| 482 | if (f.startswith('api_test_report_') and f.endswith('.xlsx')) or (f.startswith('api_test_report_') and f.endswith('.html')): | 483 | reports_base_dir = 'reports' |
| 483 | try: | 484 | env_reports_dir = os.path.join(reports_base_dir, deploy_env) |
| 484 | os.remove(f) | 485 | |
| 485 | except Exception: | 486 | # 确保目录存在 |
| 486 | pass | 487 | os.makedirs(env_reports_dir, exist_ok=True) |
| 488 | |||
| 489 | # 清理超过5个的旧报告(保留最近5个) | ||
| 490 | def cleanup_old_reports(env_dir, keep_count=5): | ||
| 491 | """保留最近N个报告,删除更旧的""" | ||
| 492 | try: | ||
| 493 | # 获取所有报告文件(只统计xlsx,因为html和xlsx是成对的) | ||
| 494 | xlsx_files = sorted(glob.glob(os.path.join(env_dir, '*.xlsx'))) | ||
| 495 | |||
| 496 | if len(xlsx_files) > keep_count: | ||
| 497 | # 删除最旧的报告(成对删除html和xlsx) | ||
| 498 | files_to_delete = xlsx_files[:-keep_count] | ||
| 499 | for xlsx_file in files_to_delete: | ||
| 500 | html_file = xlsx_file.replace('.xlsx', '.html') | ||
| 501 | try: | ||
| 502 | if os.path.exists(xlsx_file): | ||
| 503 | os.remove(xlsx_file) | ||
| 504 | if os.path.exists(html_file): | ||
| 505 | os.remove(html_file) | ||
| 506 | print(f"[CLEANUP] Deleted old report: {os.path.basename(xlsx_file)}") | ||
| 507 | except Exception as e: | ||
| 508 | print(f"[CLEANUP ERROR] Failed to delete {xlsx_file}: {e}") | ||
| 509 | except Exception as e: | ||
| 510 | print(f"[CLEANUP ERROR] {e}") | ||
| 511 | |||
| 512 | cleanup_old_reports(env_reports_dir, keep_count=5) | ||
| 487 | 513 | ||
| 488 | # 支持多个Excel文件 | 514 | # 支持多个Excel文件 |
| 489 | excel_files = [f for f in os.listdir('.') if f.startswith('api_cases') and f.endswith('.xlsx')] | 515 | excel_files = [f for f in os.listdir('.') if f.startswith('api_cases') and f.endswith('.xlsx')] |
| 490 | all_passed = True # 标记所有用例是否都通过 | 516 | all_passed = True # 标记所有用例是否都通过 |
| 491 | 517 | ||
| 492 | for excel_path in excel_files: | 518 | for excel_path in excel_files: |
| 493 | deploy_env = get_deploy_env() | 519 | report_filename = f"api_test_report_{excel_path.replace('.xlsx', '')}_{deploy_env}_{datetime.now().strftime('%Y%m%d_%H%M%S')}" |
| 494 | report_path = f"api_test_report_{excel_path.replace('.xlsx', '')}_{deploy_env}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx" | 520 | report_path = os.path.join(env_reports_dir, f"{report_filename}.xlsx") |
| 495 | html_path = report_path.replace('.xlsx', '.html') | 521 | html_path = report_path.replace('.xlsx', '.html') |
| 496 | cases = read_cases_from_excel(excel_path) | 522 | cases = read_cases_from_excel(excel_path) |
| 497 | print(f'读取到的用例({excel_path}):', cases) | 523 | print(f'[INFO] Loaded test cases from {excel_path}') |
| 498 | results = run_api_test(cases) | 524 | results = run_api_test(cases) |
| 499 | print(f'最终results({excel_path}):', results) | 525 | print(f'[INFO] Test execution completed for {excel_path}') |
| 500 | write_report_to_excel(results, report_path) | 526 | write_report_to_excel(results, report_path) |
| 501 | write_report_to_html(results, html_path) | 527 | write_report_to_html(results, html_path) |
| 502 | 528 | ||
| ... | @@ -504,12 +530,12 @@ if __name__ == "__main__": | ... | @@ -504,12 +530,12 @@ if __name__ == "__main__": |
| 504 | failed_count = sum(1 for r in results if r.get('test_result') in ['失败', '异常']) | 530 | failed_count = sum(1 for r in results if r.get('test_result') in ['失败', '异常']) |
| 505 | if failed_count > 0: | 531 | if failed_count > 0: |
| 506 | all_passed = False | 532 | all_passed = False |
| 507 | print(f"\n⚠️ {excel_path}: 有 {failed_count} 个用例执行失败") | 533 | print(f"\n[WARNING] {excel_path}: {failed_count} test cases failed") |
| 508 | 534 | ||
| 509 | # 根据结果返回退出码 | 535 | # 根据结果返回退出码 |
| 510 | if all_passed: | 536 | if all_passed: |
| 511 | print("\n✅ 所有测试用例运行成功!") | 537 | print("\n[SUCCESS] All test cases passed!") |
| 512 | sys.exit(0) # Jenkins: 执行成功 | 538 | sys.exit(0) # Jenkins: 执行成功 |
| 513 | else: | 539 | else: |
| 514 | print("\n❌ 有测试用例执行失败,请查看报告详情!") | 540 | print("\n[FAILED] Some test cases failed, check the report for details!") |
| 515 | sys.exit(1) # Jenkins: 执行失败 | 541 | sys.exit(1) # Jenkins: 执行失败 | ... | ... |
-
Please register or sign in to post a comment