# iftest **Repository Path**: jiayifei/iftest ## Basic Information - **Project Name**: iftest - **Description**: 接口自动化模版 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-10-16 - **Last Updated**: 2023-10-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 简介 ## 参考文档 - fastapi:https://fastapi.tiangolo.com/zh/ - pytest:https://docs.pytest.org/en/7.4.x/ - allure:https://docs.qameta.io/allure/ - requests:https://requests.readthedocs.io/en/latest/ ## fastgpt 简单入门 - 定义接口 ```python from fastapi import FastAPI app = FastAPI() @app.get("/") async def get_demo(): return { "msg": "hello world" } ``` - 参数传递 ```python from fastapi import FastAPI app = FastAPI() # 路径参数 @app.get("/items/{item_id}") async def read_item(item_id: int): return {"item_id": item_id} # 查询参数:路径参数中没有声明,方法入参中声明了的参数,均为查询参数 ``` ```python from fastapi import FastAPI from pydantic import BaseModel class Item(BaseModel): name: str description: str | None = None price: float tax: float | None = None app = FastAPI() @app.post("/items/") async def create_item(item: Item): return item ``` ## pytest + allure 简单入门 - 脚本编写:需要定义 ```python # 业务逻辑 # 以下为测试的功能名称定义为 Config,在步骤【测试应用配置接口】中上报了请求与响应,并断言响应码为 200 # 'epic'项目名定为[kehua,maker], 'feature'功能名根据实际情况添加 import allure import requests import pytest @allure.epic("kehua") @allure.feature('AppConfig') def test_system_config(): with allure.step('测试应用配置接口'): response = requests.get("https://www.tideswing.fun/v1/api/app-config") allure.attach(str("请求数据"), '请求', allure.attachment_type.JSON) allure.attach(str(response.json()), '响应', allure.attachment_type.JSON) assert response.status_code == 200 # 数据驱动 # 以下为数据驱动的 demo,定义列表中的三个元组即为三组用例,定义了用例优先级为 CRITICAL @allure.epic("kehua") @allure.feature('DataDriverDemo') @allure.severity(allure.severity_level.CRITICAL) @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*8", 54)]) def test_eval(test_input, expected): assert eval(test_input) == expected ``` - 脚本执行 ```shell # 执行指定脚本,并将 allure 的结果输出到文件夹 结果路径 中 pytest testcase/kehua/users/test_users.py --alluredir=结果路径 # 生成测试报告,加 --clean 覆盖路径下原报告 allure generate -o 报告路径 ``` ![](images/allure_info.png) ## 用例编写规范及构建规则 - 用例编写规范 - 套件规范 ## 项目外露接口 - 用例执行 - 用例获取 - 测试报告 ## 接口自动化用例 - 模块划分