# api_test_frame **Repository Path**: shanyi123/api_test_frame ## Basic Information - **Project Name**: api_test_frame - **Description**: ap自动化i测试demo - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2026-05-19 - **Last Updated**: 2026-05-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 接口自动化测试框架 基于 Python + pytest + requests + allure 构建的接口自动化测试框架。 ## 🚀 特性 - **完整的测试框架**: 基于 pytest 的测试框架,支持参数化、fixture、标记等功能 - **HTTP 客户端封装**: 封装 requests 库,支持重试、日志记录、认证等功能 - **丰富的断言工具**: 提供多种断言方法,支持状态码、响应时间、JSON 数据等验证 - **测试数据管理**: 支持 JSON 文件、Faker 生成、边界值测试等多种数据来源 - **美观的测试报告**: 集成 Allure 和 HTML 报告,提供详细的测试结果展示 - **环境配置管理**: 支持多环境配置,灵活切换测试环境 - **并行测试执行**: 支持多进程并行执行,提高测试效率 - **详细的日志记录**: 集成 loguru 日志库,记录详细的测试执行信息 ## 📁 项目结构 ``` api_test/ ├── config/ # 配置文件目录 │ └── config.yaml # 环境配置文件 ├── utils/ # 工具模块 │ ├── __init__.py │ ├── config_manager.py # 配置管理器 │ ├── http_client.py # HTTP客户端封装 │ ├── assertions.py # 断言工具 │ └── data_helper.py # 测试数据帮助类 ├── tests/ # 测试用例目录 │ ├── __init__.py │ ├── test_users.py # 用户API测试 │ └── test_posts.py # 文章API测试 ├── reports/ # 测试报告目录 │ ├── allure-results/ # Allure原始结果 │ ├── allure-report/ # Allure HTML报告 │ └── html/ # pytest HTML报告 ├── logs/ # 日志文件目录 ├── conftest.py # pytest配置文件 ├── pytest.ini # pytest配置 ├── requirements.txt # 项目依赖 ├── run_tests.py # 测试运行脚本 └── README.md # 项目说明 ``` ## 🛠️ 安装和使用 ### 1. 安装依赖 ```bash # 安装Python依赖 pip install -r requirements.txt # 或使用运行脚本安装 python run_tests.py --install ``` ### 2. 配置环境 编辑 `config/config.yaml` 文件,配置不同环境的 API 地址和参数: ```yaml environments: dev: base_url: "https://jsonplaceholder.typicode.com" timeout: 30 retry_times: 3 test: base_url: "https://test-api.example.com" timeout: 30 retry_times: 3 ``` ### 3. 运行测试 #### 使用运行脚本(推荐) ```bash # 运行所有测试 python run_tests.py # 运行指定环境的测试 python run_tests.py --env dev # 运行冒烟测试 python run_tests.py -m smoke # 并行运行测试 python run_tests.py -n 4 # 运行指定测试文件 python run_tests.py --path tests/test_users.py # 详细输出 python run_tests.py -v # 清理报告目录 python run_tests.py --clean ``` #### 直接使用 pytest ```bash # 运行所有测试 pytest # 运行指定标记的测试 pytest -m smoke # 运行指定环境 pytest --env=test # 并行执行 pytest -n 4 # 生成HTML报告 pytest --html=reports/html/report.html ``` ### 4. 查看测试报告 #### Allure 报告 ```bash # 生成并打开Allure报告 allure generate reports/allure-results -o reports/allure-report --clean allure open reports/allure-report ``` #### HTML 报告 测试完成后,HTML 报告会保存在 `reports/html/report.html` ## 📝 编写测试用例 ### 基本测试用例 ```python import pytest import allure from utils import http_client, assertions @allure.epic("用户管理") @allure.feature("用户API") class TestUsers: @allure.story("获取用户列表") @allure.title("测试获取所有用户") @pytest.mark.smoke def test_get_all_users(self, api_client): """测试获取所有用户""" with allure.step("发送GET请求获取用户列表"): response = api_client.get("/users") with allure.step("验证响应结果"): assertions.assert_status_code(response, 200) assertions.assert_response_time(response, 5.0) assertions.assert_json_array_length(response, 10) ``` ### 使用测试数据 ```python def test_create_user(self, api_client, user_data): """测试创建用户""" response = api_client.post("/users", json=user_data) assertions.assert_status_code(response, 201) ``` ### 参数化测试 ```python @pytest.mark.parametrize("user_id", [1, 2, 3]) def test_get_user_by_id(self, api_client, user_id): """测试获取指定用户""" response = api_client.get(f"/users/{user_id}") assertions.assert_status_code(response, 200) ``` ## 🏷️ 测试标记 框架支持以下测试标记: - `@pytest.mark.smoke`: 冒烟测试 - `@pytest.mark.regression`: 回归测试 - `@pytest.mark.api`: 接口测试 - `@pytest.mark.critical`: 关键测试 - `@pytest.mark.slow`: 慢速测试 使用方法: ```bash # 只运行冒烟测试 pytest -m smoke # 运行冒烟测试和关键测试 pytest -m "smoke or critical" # 排除慢速测试 pytest -m "not slow" ``` ## 🔧 工具类使用 ### HTTP 客户端 ```python from utils import http_client # GET请求 response = http_client.get("/users") # POST请求 response = http_client.post("/users", json=data) # 设置认证 http_client.set_auth_token("your_token") http_client.set_api_key("your_api_key") ``` ### 断言工具 ```python from utils import assertions # 状态码断言 assertions.assert_status_code(response, 200) # 响应时间断言 assertions.assert_response_time(response, 3.0) # JSON数据断言 assertions.assert_json_contains(response, {"id": 1}) # JSON路径断言 assertions.assert_json_path_value(response, "user.name", "John") ``` ### 测试数据生成 ```python from utils import data_helper # 生成用户数据 user_data = data_helper.generate_user_data() # 生成文章数据 post_data = data_helper.generate_post_data() # 生成无效数据 invalid_data = data_helper.generate_invalid_data("user") ``` ## 🌍 环境切换 ### 通过命令行参数 ```bash # 切换到测试环境 pytest --env=test # 使用自定义URL pytest --base-url=https://custom-api.com ``` ### 通过环境变量 ```bash # Windows set TEST_ENV=test # Linux/Mac export TEST_ENV=test ``` ## 📊 CI/CD 集成 ### GitHub Actions 示例 ```yaml name: API Tests on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: pip install -r requirements.txt - name: Run tests run: python run_tests.py --env=test -m smoke - name: Generate Allure Report if: always() run: | allure generate reports/allure-results -o reports/allure-report - name: Upload test results if: always() uses: actions/upload-artifact@v2 with: name: test-results path: reports/ ``` ## 🤝 贡献指南 1. Fork 项目 2. 创建功能分支 (`git checkout -b feature/AmazingFeature`) 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 4. 推送到分支 (`git push origin feature/AmazingFeature`) 5. 创建 Pull Request ## 📄 许可证 本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。 ## 📞 支持 如果您有任何问题或建议,请创建 [Issue](../../issues) 或联系维护者。 ## 🔗 相关链接 - [pytest 文档](https://docs.pytest.org/) - [requests 文档](https://docs.python-requests.org/) - [Allure 文档](https://docs.qameta.io/allure/) - [JSONPlaceholder API](https://jsonplaceholder.typicode.com/)