test:完善API测试框架

- 添加Godot内置API测试脚本
- 实现Python API客户端测试套件
- 添加快速测试和完整测试脚本
- 支持跨平台测试运行(Windows/Linux)
- 更新测试文档和使用指南
This commit is contained in:
2025-12-25 23:09:12 +08:00
parent 8980e3d558
commit 77af0bda39
9 changed files with 936 additions and 3 deletions

150
tests/api/quick_test.py Normal file
View File

@@ -0,0 +1,150 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
快速API测试脚本
用于快速验证API接口的基本功能
使用方法:
python quick_test.py
"""
import requests
import json
import sys
# API配置
API_BASE_URL = "https://whaletownend.xinghangee.icu"
def test_api_endpoint(method, endpoint, data=None, description=""):
"""测试单个API端点"""
url = f"{API_BASE_URL}{endpoint}"
print(f"\n🧪 测试: {description}")
print(f"📡 {method} {url}")
if data:
print(f"📦 数据: {json.dumps(data, ensure_ascii=False)}")
try:
if method == "GET":
response = requests.get(url, timeout=10)
elif method == "POST":
response = requests.post(url, json=data, timeout=10)
else:
print(f"❌ 不支持的方法: {method}")
return False
print(f"📊 状态码: {response.status_code}")
try:
response_data = response.json()
print(f"📄 响应: {json.dumps(response_data, ensure_ascii=False, indent=2)}")
# 检查特殊状态码
if response.status_code == 206:
print("🧪 测试模式响应")
if response_data.get("data", {}).get("verification_code"):
print(f"🔑 验证码: {response_data['data']['verification_code']}")
elif response.status_code == 409:
print("⚠️ 资源冲突")
elif response.status_code == 429:
print("⏰ 频率限制")
return 200 <= response.status_code < 300 or response.status_code == 206
except json.JSONDecodeError:
print(f"📄 原始响应: {response.text}")
return 200 <= response.status_code < 300
except requests.exceptions.Timeout:
print("❌ 请求超时")
return False
except requests.exceptions.ConnectionError:
print("❌ 连接失败")
return False
except Exception as e:
print(f"❌ 请求异常: {e}")
return False
def main():
"""主函数"""
print("🐋 WhaleTown API 快速测试")
print("=" * 50)
# 测试用例
tests = [
{
"method": "GET",
"endpoint": "/",
"description": "获取应用状态"
},
{
"method": "POST",
"endpoint": "/auth/send-email-verification",
"data": {"email": "test@example.com"},
"description": "发送邮箱验证码"
},
{
"method": "POST",
"endpoint": "/auth/send-email-verification",
"data": {"email": "invalid-email"},
"description": "发送验证码(无效邮箱)"
},
{
"method": "POST",
"endpoint": "/auth/login",
"data": {"identifier": "testuser", "password": "password123"},
"description": "用户登录"
},
{
"method": "POST",
"endpoint": "/auth/register",
"data": {
"username": "testuser_quick",
"password": "password123",
"nickname": "快速测试用户"
},
"description": "用户注册(无邮箱)"
},
{
"method": "POST",
"endpoint": "/auth/send-login-verification-code",
"data": {"identifier": "test@example.com"},
"description": "发送登录验证码"
}
]
# 执行测试
success_count = 0
total_count = len(tests)
for test in tests:
success = test_api_endpoint(
method=test["method"],
endpoint=test["endpoint"],
data=test.get("data"),
description=test["description"]
)
if success:
success_count += 1
print("✅ 测试通过")
else:
print("❌ 测试失败")
print("-" * 30)
# 打印总结
print(f"\n📊 测试总结:")
print(f"总测试数: {total_count}")
print(f"成功: {success_count}")
print(f"失败: {total_count - success_count}")
print(f"成功率: {(success_count/total_count*100):.1f}%")
if success_count == total_count:
print("🎉 所有测试通过!")
else:
print("⚠️ 部分测试失败请检查API服务")
if __name__ == "__main__":
main()