#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 利率定价流程 API 测试脚本 使用 Python 避免 Windows 环境下的编码问题 """ import requests import json from datetime import datetime BASE_URL = "http://localhost:8080" TOKEN = "" # 颜色代码 class Colors: GREEN = '\033[0;32m' RED = '\033[0;31m' YELLOW = '\033[1;33m' BLUE = '\033[0;34m' NC = '\033[0m' def print_header(text): print(f"{Colors.BLUE}{'='*40}{Colors.NC}") print(f"{Colors.BLUE}{text:^40}{Colors.NC}") print(f"{Colors.BLUE}{'='*40}{Colors.NC}") print() def get_token(): """获取测试 Token""" print(f"{Colors.YELLOW}[步骤 1] 获取测试 Token{Colors.NC}") print("-" * 35) response = requests.post( f"{BASE_URL}/login/test", json={"username": "admin", "password": "admin123"} ) result = response.json() if result.get("code") == 200: token = result.get("token") print(f"{Colors.GREEN}✓ 登录成功{Colors.NC}") print(f"Token: {token[:50]}...") print() return token else: print(f"{Colors.RED}✗ 登录失败{Colors.NC}") print(f"响应: {result}") exit(1) def test_api(method, url, data, description, expected_code=200): """执行 API 测试""" global TOTAL_TESTS, PASSED_TESTS, FAILED_TESTS, TEST_RESULTS TOTAL_TESTS += 1 print(f"[{TOTAL_TESTS}] 测试: {description} ... ", end="", flush=True) headers = { "Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json; charset=utf-8" } try: if method == "GET": response = requests.get(f"{BASE_URL}{url}", headers=headers) else: response = requests.post(f"{BASE_URL}{url}", headers=headers, json=data) result = response.json() actual_code = result.get("code") if actual_code == expected_code: print(f"{Colors.GREEN}✓ 通过{Colors.NC}") PASSED_TESTS += 1 TEST_RESULTS.append(f"✓ {description}") else: print(f"{Colors.RED}✗ 失败 (期望: {expected_code}, 实际: {actual_code}){Colors.NC}") FAILED_TESTS += 1 TEST_RESULTS.append(f"✗ {description} - 期望码: {expected_code}, 实际: {actual_code}") print(f" 响应: {json.dumps(result, ensure_ascii=False)[:200]}") except Exception as e: print(f"{Colors.RED}✗ 异常: {str(e)}{Colors.NC}") FAILED_TESTS += 1 TEST_RESULTS.append(f"✗ {description} - 异常: {str(e)}") def main(): global TOTAL_TESTS, PASSED_TESTS, FAILED_TESTS, TEST_RESULTS TOTAL_TESTS = 0 PASSED_TESTS = 0 FAILED_TESTS = 0 TEST_RESULTS = [] print_header("利率定价流程 API 测试套件") # 获取 Token TOKEN = get_token() # 执行测试 print(f"{Colors.YELLOW}[步骤 2] 执行测试用例{Colors.NC}") print("-" * 35) print() print(f"{Colors.BLUE}【功能测试】{Colors.NC}") # 测试 1: 个人客户信用贷款 test_api("POST", "/loanPricing/workflow/create", { "orgCode": "931000", "runType": "1", "custIsn": "CUST20250119001", "custType": "个人", "guarType": "信用", "midPerQuickPay": "true", "midPerEleDdc": "false", "midEntEleDdc": "false", "midEntWaterDdc": "false", "applyAmt": "50000", "isCleanEnt": "false", "hasSettleAcct": "true", "isManufacturing": "false", "isAgriGuar": "false", "isTaxA": "false", "isAgriLeading": "false", "loanPurpose": "consumer", "bizProof": "true", "collType": "一类", "collThirdParty": "false", "loanRate": "4.35", "custName": "张三", "idType": "身份证", "isInclusiveFinance": "true" }, "发起流程-个人客户信用贷款") # 测试 2: 企业客户抵押贷款 test_api("POST", "/loanPricing/workflow/create", { "orgCode": "931000", "runType": "1", "custIsn": "CUST20250119002", "custType": "企业", "guarType": "抵押", "applyAmt": "500000", "isCleanEnt": "true", "hasSettleAcct": "true", "isManufacturing": "true", "isTaxA": "true", "loanPurpose": "business", "collType": "一线", "collThirdParty": "false", "loanRate": "3.85", "custName": "测试科技有限公司", "idType": "统一社会信用代码", "isInclusiveFinance": "true" }, "发起流程-企业客户抵押贷款") # 测试 3: 农业担保贷款 test_api("POST", "/loanPricing/workflow/create", { "orgCode": "931000", "runType": "1", "custIsn": "CUST20250119003", "custType": "企业", "guarType": "保证", "applyAmt": "300000", "isAgriGuar": "true", "isAgriLeading": "true", "loanPurpose": "business", "loanRate": "4.15", "custName": "绿源农业合作社", "idType": "统一社会信用代码", "isInclusiveFinance": "true" }, "发起流程-农业担保贷款") # 测试 4: 个人质押贷款 test_api("POST", "/loanPricing/workflow/create", { "orgCode": "931000", "runType": "1", "custIsn": "CUST20250119004", "custType": "个人", "guarType": "质押", "applyAmt": "100000", "loanPurpose": "consumer", "collType": "二类", "loanRate": "4.25", "custName": "李四", "idType": "身份证", "isInclusiveFinance": "false" }, "发起流程-个人客户质押贷款") # 测试 5: 查询列表 test_api("GET", "/loanPricing/workflow/list?pageNum=1&pageSize=10", None, "查询流程列表-默认分页") print() print(f"{Colors.BLUE}【异常测试】{Colors.NC}") # 测试 11: 客户内码为空 test_api("POST", "/loanPricing/workflow/create", { "custType": "个人", "guarType": "信用", "applyAmt": "50000", "loanRate": "4.35" }, "异常测试-客户内码为空", 500) # 测试 12: 贷款利率为空 test_api("POST", "/loanPricing/workflow/create", { "custIsn": "TEST001", "custType": "个人", "guarType": "信用", "applyAmt": "50000" }, "异常测试-贷款利率为空", 500) # 测试 13: 查询不存在的流程 test_api("GET", "/loanPricing/workflow/NOTEXIST123", None, "异常测试-查询不存在的流程", 500) # 生成报告 print() print(f"{Colors.YELLOW}[步骤 3] 测试结果统计{Colors.NC}") print("-" * 35) print() pass_rate = int(PASSED_TESTS * 100 / TOTAL_TESTS) if TOTAL_TESTS > 0 else 0 print(f"总测试数: {TOTAL_TESTS}") print(f"通过: {Colors.GREEN}{PASSED_TESTS}{Colors.NC}") print(f"失败: {Colors.RED}{FAILED_TESTS}{Colors.NC}") print(f"通过率: {pass_rate}%") print() print("详细测试结果:") print("-" * 35) for result in TEST_RESULTS: if result.startswith("✓"): print(f"{Colors.GREEN}{result}{Colors.NC}") else: print(f"{Colors.RED}{result}{Colors.NC}") # 保存报告 report_file = f"api-test-report-{datetime.now().strftime('%Y%m%d%H%M%S')}.txt" with open(report_file, "w", encoding="utf-8") as f: f.write("利率定价流程 API 测试报告\n") f.write("=" * 36 + "\n\n") f.write(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n") f.write(f"测试环境: {BASE_URL}\n") f.write(f"测试账号: admin\n\n") f.write("测试统计\n") f.write("-" * 10 + "\n") f.write(f"总测试数: {TOTAL_TESTS}\n") f.write(f"通过数: {PASSED_TESTS}\n") f.write(f"失败数: {FAILED_TESTS}\n") f.write(f"通过率: {pass_rate}%\n\n") f.write("测试用例详情\n") f.write("-" * 14 + "\n") for result in TEST_RESULTS: f.write(result + "\n") print() print(f"测试报告已保存到: {report_file}") print_header("测试完成") return 0 if FAILED_TESTS == 0 else 1 if __name__ == "__main__": exit(main())