123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- 学习报告API接口测试脚本
- 测试学习报告相关的API接口功能
- """
- import requests
- import json
- import time
- # 配置
- BASE_URL = "http://localhost:8080/api"
- USER_ID = 1
- GOAL_ID = 1
- def test_generate_study_report():
- """测试生成学习报告"""
- print("\n=== 测试生成学习报告 ===")
- url = f"{BASE_URL}/study-reports/generate"
- params = {
- "userId": USER_ID,
- "goalId": GOAL_ID
- }
-
- try:
- response = requests.post(url, params=params, timeout=10)
- print(f"请求URL: {url}")
- print(f"请求参数: {params}")
- print(f"响应状态码: {response.status_code}")
- print(f"响应内容: {response.text}")
-
- if response.status_code == 200:
- data = response.json()
- if data.get('code') == 200:
- print("✅ 生成学习报告成功")
- return True
- else:
- print(f"❌ 生成学习报告失败: {data.get('message')}")
- return False
- else:
- print(f"❌ HTTP请求失败: {response.status_code}")
- return False
- except Exception as e:
- print(f"❌ 请求异常: {e}")
- return False
- def test_get_latest_study_report():
- """测试获取最近学习报告"""
- print("\n=== 测试获取最近学习报告 ===")
- url = f"{BASE_URL}/study-reports/latest"
- params = {"userId": USER_ID}
-
- try:
- response = requests.get(url, params=params, timeout=10)
- print(f"请求URL: {url}")
- print(f"请求参数: {params}")
- print(f"响应状态码: {response.status_code}")
- print(f"响应内容: {response.text}")
-
- if response.status_code == 200:
- data = response.json()
- if data.get('code') == 200:
- print("✅ 获取最近学习报告成功")
- return True
- else:
- print(f"❌ 获取最近学习报告失败: {data.get('message')}")
- return False
- else:
- print(f"❌ HTTP请求失败: {response.status_code}")
- return False
- except Exception as e:
- print(f"❌ 请求异常: {e}")
- return False
- def test_get_all_study_reports():
- """测试获取用户所有学习报告"""
- print("\n=== 测试获取用户所有学习报告 ===")
- url = f"{BASE_URL}/study-reports/user/{USER_ID}"
-
- try:
- response = requests.get(url, timeout=10)
- print(f"请求URL: {url}")
- print(f"响应状态码: {response.status_code}")
- print(f"响应内容: {response.text}")
-
- if response.status_code == 200:
- data = response.json()
- if data.get('code') == 200:
- reports = data.get('data', [])
- print(f"✅ 获取用户所有学习报告成功,共 {len(reports)} 个报告")
- return True
- else:
- print(f"❌ 获取用户所有学习报告失败: {data.get('message')}")
- return False
- else:
- print(f"❌ HTTP请求失败: {response.status_code}")
- return False
- except Exception as e:
- print(f"❌ 请求异常: {e}")
- return False
- def test_get_goal_study_report():
- """测试获取指定目标的学习报告"""
- print("\n=== 测试获取指定目标的学习报告 ===")
- url = f"{BASE_URL}/study-reports/latest/goal/{GOAL_ID}"
- params = {"userId": USER_ID}
-
- try:
- response = requests.get(url, params=params, timeout=10)
- print(f"请求URL: {url}")
- print(f"请求参数: {params}")
- print(f"响应状态码: {response.status_code}")
- print(f"响应内容: {response.text}")
-
- if response.status_code == 200:
- data = response.json()
- if data.get('code') == 200:
- print("✅ 获取指定目标的学习报告成功")
- return True
- else:
- print(f"❌ 获取指定目标的学习报告失败: {data.get('message')}")
- return False
- else:
- print(f"❌ HTTP请求失败: {response.status_code}")
- return False
- except Exception as e:
- print(f"❌ 请求异常: {e}")
- return False
- def main():
- """主函数"""
- print("学习报告API接口测试开始...")
- print(f"测试用户ID: {USER_ID}")
- print(f"测试目标ID: {GOAL_ID}")
-
- # 测试结果统计
- test_results = []
-
- # 执行测试
- test_results.append(test_generate_study_report())
- time.sleep(1) # 等待1秒
-
- test_results.append(test_get_latest_study_report())
- time.sleep(1)
-
- test_results.append(test_get_all_study_reports())
- time.sleep(1)
-
- test_results.append(test_get_goal_study_report())
-
- # 统计结果
- success_count = sum(test_results)
- total_count = len(test_results)
-
- print("\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("🎉 所有学习报告API接口测试通过!")
- else:
- print("⚠️ 部分学习报告API接口测试失败,请检查服务器日志")
- if __name__ == "__main__":
- main()
|