# Interface-Autotest **Repository Path**: naumy/Interface-Autotest ## Basic Information - **Project Name**: Interface-Autotest - **Description**: 所有代码和文件均有AI生成 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-07-31 - **Last Updated**: 2025-07-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # API自动化测试框架 这是一个使用FastAPI和pytest实现的API自动化测试框架示例。 ## 项目结构 ``` . ├── app/ │ ├── config/ # 应用配置 │ │ ├── settings.py # 基础配置 │ │ ├── database.py # 数据库配置 │ │ └── logger.py # 日志配置 │ ├── database/ # 数据库操作 │ │ └── database.py # 数据库连接和操作 │ ├── middleware/ # 中间件 │ │ └── error_handler.py # 错误处理中间件 │ ├── models/ # 数据模型 │ │ ├── __init__.py │ │ ├── category.py # 分类模型 │ │ ├── item.py # 商品模型 │ │ ├── order.py # 订单模型 │ │ └── user.py # 用户模型 │ ├── routes/ # API路由 │ │ ├── auth.py # 认证路由 │ │ ├── items.py # 商品路由 │ │ └── orders.py # 订单路由 │ ├── services/ # 业务逻辑 │ │ ├── item_service.py # 商品服务 │ │ ├── order_service.py # 订单服务 │ │ └── user_service.py # 用户服务 │ ├── utils/ # 工具函数 │ │ └── security.py # 安全相关工具 │ ├── __init__.py │ └── main.py # 应用入口 ├── tests/ │ ├── framework/ # 测试框架 │ │ ├── api_test.py # API测试基类 │ │ ├── base.py # 基础测试类 │ │ ├── data_manager.py # 数据管理器 │ │ ├── decorators.py # 测试装饰器 │ │ ├── fixtures.py # 测试夹具 │ │ ├── logger.py # 日志管理器 │ │ ├── report_manager.py # 报告管理器 │ │ └── report_sender/ # 报告发送器 │ │ ├── base_sender.py │ │ ├── email_sender.py │ │ ├── dingtalk_sender.py │ │ └── wecom_sender.py │ ├── test_cases/ # 测试用例 │ │ ├── auth/ # 认证模块测试 │ │ ├── items/ # 商品模块测试 │ │ └── order/ # 订单模块测试 │ ├── conftest.py # pytest配置 │ └── report_config.json # 报告配置 ├── alembic.ini # 数据库迁移配置 ├── pytest.ini # pytest配置 ├── requirements.txt # 项目依赖 └── README.md # 项目说明 ``` ## 架构说明 项目采用分层架构设计: 1. **模型层(Models)**:定义数据结构和验证规则 2. **服务层(Services)**:处理业务逻辑 3. **路由层(Routes)**:处理HTTP请求和响应 4. **数据库层(Database)**:处理数据存储和检索 5. **配置层(Config)**:管理应用配置 ## 安装依赖 ```bash pip install -r requirements.txt ``` ## 运行后端服务 ```bash uvicorn app.main:app --reload ``` 服务将在 http://localhost:8000 运行 ## 运行测试 ```bash # 运行所有测试 pytest # 生成HTML测试报告 pytest --html=report.html ``` ## API文档 启动服务后,可以访问以下地址查看API文档: - Swagger UI: http://localhost:8000/docs - ReDoc: http://localhost:8000/redoc ## API端点 所有API端点都以 `/api` 为前缀: - GET /api/items - 获取所有商品 - GET /api/items/{item_id} - 获取单个商品 - POST /api/items - 创建新商品 - PUT /api/items/{item_id} - 更新商品 - DELETE /api/items/{item_id} - 删除商品 ## 测试用例说明 测试用例覆盖了以下场景: 1. 获取所有商品 2. 创建新商品 3. 获取单个商品 4. 更新商品 5. 删除商品 6. 处理不存在的商品 # 后端平台与测试平台说明文档 ## 1. 系统架构 ### 1.1 整体架构图 ```mermaid graph TB subgraph 测试平台 A[测试用例] --> B[测试框架] B --> C[数据管理] B --> D[报告管理] B --> E[日志管理] C --> F[数据加载器] D --> G[报告发送器] end subgraph 后端平台 H[API服务] --> I[业务逻辑] I --> J[数据访问] J --> K[数据库] end B --> H G --> L[邮件] G --> M[钉钉] G --> N[企业微信] ``` ### 1.2 测试平台架构 ```mermaid graph LR A[测试用例] --> B[测试框架] B --> C[数据管理] B --> D[报告管理] B --> E[日志管理] C --> F1[JSON加载器] C --> F2[CSV加载器] C --> F3[Excel加载器] D --> G1[邮件发送器] D --> G2[钉钉发送器] D --> G3[企业微信发送器] ``` ## 2. 目录结构 ``` project/ ├── app/ # 应用目录 │ ├── config/ # 应用配置 │ ├── database/ # 数据库操作 │ ├── middleware/ # 中间件 │ ├── models/ # 数据模型 │ ├── routes/ # API路由 │ ├── services/ # 业务逻辑 │ ├── utils/ # 工具函数 │ ├── __init__.py │ └── main.py # 应用入口 ├── tests/ # 测试目录 │ ├── framework/ # 测试框架 │ │ ├── test_base.py # 基础测试类 │ │ ├── data_manager.py # 数据管理器 │ │ ├── report_manager.py # 报告管理器 │ │ ├── logger.py # 日志管理器 │ │ ├── config.py # 配置管理器 │ │ └── report_sender/ # 报告发送器 │ │ ├── base_sender.py │ │ ├── email_sender.py │ │ ├── dingtalk_sender.py │ │ └── wecom_sender.py │ ├── test_cases/ # 测试用例 │ │ ├── auth/ # 认证模块测试 │ │ ├── items/ # 商品模块测试 │ │ └── order/ # 订单模块测试 │ ├── conftest.py # pytest配置 │ └── report_config.json # 报告配置 ├── alembic.ini # 数据库迁移配置 ├── pytest.ini # pytest配置 ├── requirements.txt # 项目依赖 └── README.md # 项目说明 ``` ## 3. 核心功能 ### 3.1 测试框架 - 基础测试类:提供通用的测试方法和工具 - 数据管理:支持多种数据格式(JSON/CSV/Excel) - 报告管理:生成多种格式的测试报告 - 日志管理:记录测试执行过程 - 配置管理:管理测试环境配置 ### 3.2 报告发送 - 邮件发送:支持HTML格式报告 - 钉钉发送:支持文本和文件消息 - 企业微信发送:支持多种消息类型 ### 3.3 数据管理 - JSON数据:适用于简单数据结构 - CSV数据:适用于表格数据 - Excel数据:适用于复杂数据结构 ## 4. 使用说明 ### 4.1 环境准备 1. 安装依赖: ```bash pip install -r requirements.txt ``` 2. 配置环境变量: - 复制 `.env.example` 为 `.env` - 填写相应的配置信息 ### 4.2 运行测试 1. 运行所有测试: ```bash pytest tests/test_cases/ -v --alluredir=./allure-results ``` 2. 运行特定模块测试: ```bash pytest tests/test_cases/auth/ -v --alluredir=./allure-results ``` 3. 运行特定测试用例: ```bash pytest tests/test_cases/auth/test_login.py -v --alluredir=./allure-results ``` ### 4.3 查看报告 1. 生成Allure报告: ```bash allure serve ./allure-results ``` ## 5. 配置说明 ### 5.1 环境配置 ```ini # API配置 API_BASE_URL=http://localhost:8000 API_TIMEOUT=30 # 数据库配置 DB_HOST=localhost DB_PORT=3306 DB_NAME=test_db DB_USER=test_user DB_PASSWORD=test_password # 邮件配置 SMTP_SERVER=smtp.example.com SMTP_PORT=587 SMTP_USERNAME=test@example.com SMTP_PASSWORD=test_password EMAIL_FROM=test@example.com EMAIL_TO=recipient@example.com # 钉钉配置 DINGTALK_WEBHOOK=https://oapi.dingtalk.com/robot/send?access_token=your_token DINGTALK_SECRET=your_secret # 企业微信配置 WECOM_CORP_ID=your_corp_id WECOM_AGENT_ID=your_agent_id WECOM_SECRET=your_secret ``` ### 5.2 Pytest配置 ```ini [pytest] testpaths = tests/test_cases python_files = test_*.py python_classes = Test* python_functions = test_* addopts = -v --alluredir=./allure-results markers = smoke: smoke tests regression: regression tests api: api tests ui: ui tests ``` ## 6. 注意事项 1. 测试数据管理 - 确保测试数据文件格式正确 - 定期更新测试数据 - 注意敏感数据的处理 2. 环境配置 - 不同环境使用不同的配置文件 - 敏感信息使用环境变量 - 定期更新配置信息 3. 报告管理 - 定期清理历史报告 - 及时处理失败用例 - 关注测试覆盖率 4. 日志管理 - 合理设置日志级别 - 定期归档日志文件 - 关注错误日志 ## 7. 常见问题 1. 测试执行失败 - 检查环境配置 - 验证测试数据 - 查看错误日志 2. 报告发送失败 - 检查网络连接 - 验证发送配置 - 确认接收方信息 3. 数据加载错误 - 检查文件格式 - 验证数据内容 - 确认文件路径 ## 8. 维护说明 1. 日常维护 - 更新测试用例 - 维护测试数据 - 清理历史报告 2. 版本更新 - 更新依赖包 - 升级测试框架 - 优化测试用例 3. 问题处理 - 记录问题日志 - 分析失败原因 - 及时修复问题