Kamixitong/simple_test.py

40 lines
1.3 KiB
Python
Raw Normal View History

2025-11-15 23:57:24 +08:00
import requests
import json
# 创建会话以保持登录状态
session = requests.Session()
def test_apis():
"""直接测试API接口"""
try:
# 1. 测试创建产品(这会生成操作日志)
print("=== 测试创建产品 ===")
product_data = {
'product_name': '测试产品',
'description': '这是一个测试产品'
}
response = session.post(
"http://localhost:5000/api/v1/products",
json=product_data,
headers={'Content-Type': 'application/json'}
)
print(f"创建产品状态码: {response.status_code}")
print(f"创建产品响应: {response.text}")
# 2. 测试获取操作日志
print("\n=== 测试获取操作日志 ===")
response = session.get("http://localhost:5000/api/v1/logs")
print(f"获取日志状态码: {response.status_code}")
if response.status_code == 200:
log_data = response.json()
print(f"日志数据: {json.dumps(log_data, indent=2, ensure_ascii=False)}")
else:
print(f"获取日志失败: {response.text}")
except Exception as e:
print(f"测试过程中出现错误: {e}")
if __name__ == "__main__":
print("开始简单测试...")
test_apis()