# API接口测试 **Repository Path**: Accsen/api-interface-testing ## Basic Information - **Project Name**: API接口测试 - **Description**: API接口自动化测试 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-22 - **Last Updated**: 2026-03-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # API 接口测试项目 本项目是一个基于 Python + pytest 的接口自动化测试框架,支持参数化测试、Fixture 作用域管理、Allure 报告生成、JSON Schema 验证以及 YAML 数据读写等功能。 ## 项目结构 ``` API_Test/ ├── cases/ # 测试用例目录 │ ├── test_case01.py # 基础测试用例 │ ├── test_case02.py # Setup/Teardown 用例 │ ├── test_case03.py # 参数化测试 (pytest.mark.parametrize) │ ├── test_case04.py # Fixture 测试 │ ├── test_case05.py # Class 级别 Fixture │ ├── test_case06.py # 混合 Fixture 使用 │ ├── test_case07.py # 更多测试用例 │ ├── test_case08.py # Fixture 参数化 │ ├── test_jsonschema.py # JSON Schema 验证 │ ├── test_yaml09.py # YAML 文件读写 │ └── case_01.py # 分类测试用例 ├── tests/ # 额外测试目录 │ └── test_aaa.py ├── allure-report/ # Allure 测试报告 ├── conftest.py # pytest 全局配置 ├── my_logging.py # 日志模块 ├── requirement.txt # 项目依赖 ├── pytest.ini # pytest 配置 ├── firstYaml.yml # 测试用 YAML 文件 └── secondYaml.yml # 测试用 YAML 文件 ``` ## 功能特性 ### 1. 参数化测试 支持使用 `@pytest.mark.parametrize` 进行数据驱动测试: ```python @pytest.mark.parametrize("data", [1, 2, 3]) def test(data): ... @pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+4", 6)]) def test_eval(test_input, expected): ... ``` ### 2. Fixture 作用域 支持多种 Fixture 作用域: - `function`: 默认作用域,每个测试函数执行一次 - `class`: 类级别,整个测试类执行一次 - `session`: 会话级别,整个测试会话执行一次 ```python @pytest.fixture(scope="class") def fixture_01(): ... ``` ### 3. JSON Schema 验证 使用 JSON Schema 验证接口返回值的结构和类型。 ### 4. YAML 数据管理 支持读写 YAML 格式的测试数据: ```python def write_yaml(data): ... def read_yaml(): ... ``` ### 5. Allure 报告 集成 Allure 生成可视化测试报告: ```bash pytest --alluredir=./allure-result allure serve ./allure-result ``` ### 6. 日志记录 使用自定义日志模块 `my_logging.py` 记录测试过程。 ## 环境要求 - Python 3.12+ - pytest 8.3.2 - Allure Report - PyYAML ## 安装依赖 ```bash pip install -r requirement.txt ``` ## 运行测试 ### 运行所有测试 ```bash pytest ``` ### 运行指定测试文件 ```bash pytest cases/test_case03.py ``` ### 运行指定测试用例 ```bash pytest cases/test_case03.py::test_data ``` ### 生成 Allure 报告 ```bash pytest --alluredir=./allure-result allure serve ./allure-result ``` ### 查看日志 测试日志会记录在 `mylog.log` 文件中。 ## 测试用例说明 | 文件 | 说明 | |------|------| | `test_case01.py` | 基础测试用例 | | `test_case02.py` | 展示 Setup 和 Teardown 的使用 | | `test_case03.py` | 参数化测试示例 | | `test_case04.py` | Fixture 基础用法 | | `test_case05.py` | Class 级别 Fixture | | `test_case06.py` | Fixture 混合使用 | | `test_case07.py` | 更多测试场景 | | `test_case08.py` | Fixture 参数化 | | `test_jsonschema.py` | JSON Schema 验证示例 | | `test_yaml09.py` | YAML 文件读写示例 | ## 配置文件 ### pytest.ini pytest 运行时配置,包括测试路径、命令行选项等。 ### requirement.txt 项目依赖包列表: - pytest - allure-pytest - PyYAML - jsonschema - 其他测试相关库 ## 贡献指南 欢迎提交 Pull Request 或创建 Issue 来完善这个测试框架。 ## 许可证 本项目仅供学习交流使用。