08ea5fbe98
添加用户管理视图、API和状态管理文件
240 lines
6.5 KiB
Markdown
240 lines
6.5 KiB
Markdown
# Everything is Suitable API Test
|
||
|
||
API 测试模块,基于 Python + pytest 框架实现,提供完整的 API 黑盒测试解决方案。
|
||
|
||
## 功能特性
|
||
|
||
- 测试数据管理:支持JSON、CSV等多种格式的测试用例与测试数据存储
|
||
- 内存数据存储:使用内存数据结构管理测试数据,无需数据库
|
||
- API测试功能:支持RESTful API的所有HTTP方法(GET、POST、PUT、DELETE等)
|
||
- 请求构造:支持请求参数构造、请求头配置、认证授权处理
|
||
- 响应验证:支持状态码检查、响应体断言、响应时间阈值验证
|
||
- 依赖处理:支持API依赖关系处理与测试用例执行顺序控制
|
||
- 测试报告:支持HTML和JSON格式的测试报告,包含可视化图表
|
||
- 命令行接口:提供完整的命令行接口,支持测试套件指定和过滤
|
||
- 日志记录:实现详细的日志记录系统
|
||
|
||
## 技术栈
|
||
|
||
- Python 3.10+
|
||
- Poetry(依赖管理)
|
||
- requests/httpx(HTTP客户端)
|
||
- pytest(测试框架)
|
||
- Jinja2(模板引擎)
|
||
- PyYAML(配置文件解析)
|
||
- Click(命令行框架)
|
||
|
||
## 项目结构
|
||
|
||
```
|
||
api/
|
||
├── src/apitest/ # API测试源代码
|
||
│ ├── models/ # 数据模型
|
||
│ │ ├── __init__.py
|
||
│ │ ├── exceptions.py # 异常定义
|
||
│ │ └── test_models.py # 测试数据模型
|
||
│ ├── client/ # HTTP客户端
|
||
│ │ ├── __init__.py
|
||
│ │ ├── api_client.py # API客户端
|
||
│ │ └── auth_manager.py # 认证管理器
|
||
│ ├── config/ # 配置管理
|
||
│ │ ├── __init__.py
|
||
│ │ ├── config_manager.py # 配置管理器
|
||
│ │ └── logger_manager.py # 日志管理器
|
||
│ ├── core/ # 核心功能
|
||
│ │ ├── __init__.py
|
||
│ │ ├── test_engine.py # 测试引擎
|
||
│ │ └── validation_engine.py # 验证引擎
|
||
│ ├── data/ # 数据管理
|
||
│ │ ├── __init__.py
|
||
│ │ └── test_data_manager.py # 测试数据管理器
|
||
│ ├── report/ # 报告生成
|
||
│ │ ├── __init__.py
|
||
│ │ └── report_manager.py # 报告管理器
|
||
│ ├── orchestrator/ # 测试编排
|
||
│ │ ├── __init__.py
|
||
│ │ └── test_orchestrator.py # 测试编排器
|
||
│ ├── utils/ # 工具类
|
||
│ │ └── __init__.py
|
||
│ ├── cli/ # 命令行接口
|
||
│ │ ├── __init__.py
|
||
│ │ └── cli_module.py # CLI模块
|
||
│ ├── __init__.py
|
||
│ ├── cli_module.py # CLI入口
|
||
│ └── main.py # 主入口
|
||
├── test_cases/ # 测试用例
|
||
│ ├── example_test_cases.json
|
||
│ ├── example_test_data.csv
|
||
│ └── parameterized_test_cases.json
|
||
├── data/ # 测试数据
|
||
│ └── .gitkeep
|
||
├── config/ # 配置文件
|
||
│ └── config.yaml
|
||
├── tests/ # 测试代码
|
||
│ ├── unit/ # 单元测试
|
||
│ │ ├── test_cli.py
|
||
│ │ ├── test_config_manager.py
|
||
│ │ ├── test_logger_manager.py
|
||
│ │ ├── test_models.py
|
||
│ │ ├── test_report_manager.py
|
||
│ │ ├── test_test_data_manager.py
|
||
│ │ ├── test_test_engine.py
|
||
│ │ ├── test_test_orchestrator.py
|
||
│ │ └── test_validation_engine.py
|
||
│ └── integration/ # 集成测试
|
||
├── pyproject.toml # Poetry配置
|
||
├── requirements.txt # Python依赖
|
||
├── setup.py # Python安装脚本
|
||
└── README.md # 本文档
|
||
```
|
||
|
||
## 安装
|
||
|
||
### 前置要求
|
||
|
||
- Python 3.10+
|
||
- Poetry 1.6.0+
|
||
|
||
### 安装步骤
|
||
|
||
```bash
|
||
# 进入 API 测试目录
|
||
cd api
|
||
|
||
# 安装依赖
|
||
poetry install
|
||
|
||
# 或者使用 requirements.txt
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
### 配置环境变量
|
||
|
||
在项目根目录的 `.env` 文件中配置 API 测试相关环境变量:
|
||
|
||
```env
|
||
# API测试环境配置
|
||
API_BASE_URL=http://localhost:8080
|
||
API_TIMEOUT=30000
|
||
API_MAX_RETRIES=3
|
||
|
||
# 认证配置
|
||
TEST_USERNAME=admin
|
||
TEST_PASSWORD=admin123
|
||
|
||
# 测试配置
|
||
TEST_PARALLEL=true
|
||
TEST_RETRY_COUNT=3
|
||
TEST_LOG_LEVEL=INFO
|
||
```
|
||
|
||
## 使用
|
||
|
||
### 基本命令
|
||
|
||
```bash
|
||
# 运行所有 API 测试
|
||
npm run test:api
|
||
|
||
# 运行 API 单元测试
|
||
npm run test:api:unit
|
||
|
||
# 并行运行 API 测试
|
||
npm run test:api:parallel
|
||
|
||
# 生成 API 测试报告
|
||
npm run test:api:report
|
||
|
||
# 格式化 API 测试代码
|
||
npm run format:api
|
||
```
|
||
|
||
### 配置文件
|
||
|
||
配置文件位于 `config/config.yaml`,包含以下配置项:
|
||
|
||
- **target**: 目标系统配置(base_url、timeout、max_retries)
|
||
- **auth**: 认证配置(login_endpoint、username、password、token_storage)
|
||
- **test**: 测试配置(data_dir、test_cases_dir、parallel、retry_count)
|
||
- **report**: 报告配置(output_dir、formats、include_details)
|
||
- **logging**: 日志配置(level、file、format、console)
|
||
- **data**: 数据管理配置(load_on_startup、auto_refresh、cache_enabled)
|
||
|
||
## 与 E2E 测试的集成
|
||
|
||
API 测试模块已整合到统一测试平台中,可以与 E2E 测试一起运行:
|
||
|
||
```bash
|
||
# 运行所有测试(E2E + API)
|
||
npm test
|
||
|
||
# 查看统一测试报告
|
||
npm run test:report
|
||
```
|
||
|
||
## 开发
|
||
|
||
### 代码规范
|
||
|
||
- 遵循PEP 8代码规范
|
||
- 使用类型注解
|
||
- 使用Google风格文档字符串
|
||
- 代码格式化使用black
|
||
- 代码检查使用flake8
|
||
- 类型检查使用mypy
|
||
|
||
### 运行测试
|
||
|
||
```bash
|
||
# 运行所有测试
|
||
poetry run pytest
|
||
|
||
# 运行单元测试
|
||
poetry run pytest tests/unit/
|
||
|
||
# 运行集成测试
|
||
poetry run pytest tests/integration/
|
||
|
||
# 生成覆盖率报告
|
||
poetry run pytest --cov=apitest --cov-report=html
|
||
```
|
||
|
||
### 代码格式化
|
||
|
||
```bash
|
||
# 格式化代码
|
||
poetry run black src/ tests/
|
||
|
||
# 检查代码风格
|
||
poetry run flake8 src/ tests/
|
||
|
||
# 类型检查
|
||
poetry run mypy src/
|
||
```
|
||
|
||
## 测试报告
|
||
|
||
API 测试报告使用 Allure 框架生成,支持:
|
||
|
||
- HTML 格式报告
|
||
- JSON 格式报告
|
||
- 测试用例详情
|
||
- 测试趋势分析
|
||
- 可视化图表
|
||
|
||
报告生成在 `../test-results/api/allure-report/` 目录下。
|
||
|
||
## 贡献
|
||
|
||
欢迎贡献代码!请阅读 [CONTRIBUTING.md](../../CONTRIBUTING.md) 了解贡献指南。
|
||
|
||
## 许可证
|
||
|
||
MIT License
|
||
|
||
## 联系方式
|
||
|
||
- 项目主页: https://github.com/yourusername/everything-is-suitable
|
||
- 问题反馈: https://github.com/yourusername/everything-is-suitable/issues
|
||
- 邮箱: test@example.com
|