137 lines
5.2 KiB
Python
Executable File
137 lines
5.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""
|
||
DMS合规性测试工具 - Python API测试脚本
|
||
对应原始命令:python run_api_tests.py --base-url http://127.0.0.1:5001/ --dms ./assets/doc/dms/domain.json --stages-dir ./custom_stages --custom-test-cases-dir ./custom_testcases -v -o ./test_reports/
|
||
"""
|
||
|
||
import requests
|
||
import json
|
||
import time
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
def main():
|
||
print("=== DMS合规性测试工具 - Python API测试 ===")
|
||
|
||
# 配置变量
|
||
api_server = "http://localhost:5050"
|
||
target_api = "http://127.0.0.1:5001/"
|
||
output_log = "log_dms_docker.txt"
|
||
|
||
print(f"[信息] 目标API: {target_api}")
|
||
print(f"[信息] API服务器: {api_server}")
|
||
print(f"[信息] 输出日志: {output_log}")
|
||
|
||
# 测试配置(对应原始命令行参数)
|
||
test_config = {
|
||
"base-url": target_api,
|
||
"dms": "./assets/doc/dms/domain.json",
|
||
"stages-dir": "./custom_stages",
|
||
"custom-test-cases-dir": "./custom_testcases",
|
||
"verbose": True,
|
||
"output": "./test_reports/",
|
||
"format": "json",
|
||
"generate-pdf": True,
|
||
"strictness-level": "CRITICAL",
|
||
"ignore-ssl": True
|
||
}
|
||
|
||
print("\n[信息] 测试配置:")
|
||
print(json.dumps(test_config, indent=2, ensure_ascii=False))
|
||
|
||
try:
|
||
# 首先检查API服务器是否可用
|
||
print(f"\n[信息] 检查API服务器状态...")
|
||
health_response = requests.get(f"{api_server}/", timeout=10)
|
||
if health_response.status_code == 200:
|
||
print(f"[成功] API服务器运行正常")
|
||
else:
|
||
print(f"[警告] API服务器状态异常: {health_response.status_code}")
|
||
|
||
# 执行测试
|
||
print(f"\n[信息] 开始执行测试...")
|
||
print(f"[信息] 这可能需要几分钟时间...")
|
||
|
||
start_time = time.time()
|
||
|
||
response = requests.post(
|
||
f"{api_server}/run",
|
||
json=test_config,
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=300 # 5分钟超时
|
||
)
|
||
|
||
end_time = time.time()
|
||
duration = end_time - start_time
|
||
|
||
# 保存结果到日志文件
|
||
with open(output_log, 'w', encoding='utf-8') as f:
|
||
f.write(f"=== DMS合规性测试结果 ===\n")
|
||
f.write(f"执行时间: {duration:.2f}秒\n")
|
||
f.write(f"HTTP状态码: {response.status_code}\n")
|
||
f.write(f"响应头: {dict(response.headers)}\n\n")
|
||
|
||
if response.status_code == 200:
|
||
f.write("=== 测试成功 ===\n")
|
||
try:
|
||
result_json = response.json()
|
||
f.write(json.dumps(result_json, indent=2, ensure_ascii=False))
|
||
except:
|
||
f.write("响应内容(非JSON格式):\n")
|
||
f.write(response.text)
|
||
else:
|
||
f.write("=== 测试失败 ===\n")
|
||
f.write(f"错误信息: {response.text}\n")
|
||
|
||
# 显示结果
|
||
if response.status_code == 200:
|
||
print(f"[成功] 测试完成!耗时: {duration:.2f}秒")
|
||
|
||
try:
|
||
result = response.json()
|
||
print(f"\n=== 测试结果摘要 ===")
|
||
print(f"状态: {result.get('status', '未知')}")
|
||
if 'summary' in result:
|
||
summary = result['summary']
|
||
print(f"总测试数: {summary.get('total_tests', 0)}")
|
||
print(f"通过数: {summary.get('passed_tests', 0)}")
|
||
print(f"失败数: {summary.get('failed_tests', 0)}")
|
||
print(f"跳过数: {summary.get('skipped_tests', 0)}")
|
||
|
||
if 'output_files' in result:
|
||
print(f"\n=== 生成的文件 ===")
|
||
for file_info in result['output_files']:
|
||
print(f"- {file_info}")
|
||
|
||
except Exception as e:
|
||
print(f"[警告] 解析JSON响应失败: {e}")
|
||
print(f"[信息] 原始响应已保存到: {output_log}")
|
||
else:
|
||
print(f"[错误] 测试执行失败")
|
||
print(f"[信息] HTTP状态码: {response.status_code}")
|
||
print(f"[信息] 错误信息: {response.text[:200]}...")
|
||
|
||
print(f"\n[信息] 详细日志已保存到: {output_log}")
|
||
print(f"[信息] 查看测试报告: ls -la test_reports/")
|
||
|
||
except requests.exceptions.Timeout:
|
||
print(f"[错误] 请求超时(5分钟)")
|
||
print(f"[建议] 测试可能仍在后台运行,请稍后查看test_reports/目录")
|
||
except requests.exceptions.ConnectionError:
|
||
print(f"[错误] 无法连接到API服务器: {api_server}")
|
||
print(f"[建议] 请确认Docker容器正在运行: docker-compose ps")
|
||
except Exception as e:
|
||
print(f"[错误] 执行失败: {e}")
|
||
return 1
|
||
|
||
print(f"\n=== 管理命令 ===")
|
||
print(f"- 查看服务状态: docker-compose ps")
|
||
print(f"- 查看服务日志: docker-compose logs")
|
||
print(f"- 查看测试日志: cat {output_log}")
|
||
print(f"- 查看测试报告: ls -la test_reports/")
|
||
|
||
return 0
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|