Skip to content

About

接口自动化测试框架 Demo:pytest + requests + Flask 被测系统,数据驱动用例、fixtures 生命周期管理、GitHub Actions 持续集成

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

pytest 接口自动化测试框架(Demo)

基于 pytest + requests + Flask 的接口自动化测试项目:内置一个待办事项(TODO)REST API 被测系统, 框架自动拉起被测服务、执行测试并生成 HTML 报告,支持 GitHub Actions 持续集成。

项目结构

pytest-api-framework/
├── app/
│   └── todo_server.py      # 被测系统: 待办事项 REST API(登录鉴权 + CRUD)
├── api/
│   └── base_api.py         # HTTP 客户端封装(统一 base_url / token 注入 / 请求日志)
├── common/
│   ├── tools.py            # yaml 读取、日志初始化
│   └── check.py            # 通用断言封装(业务码 / HTTP 状态码 / 字段断言)
├── config/
│   └── config.yaml         # 环境配置(地址、端口、预置账号)
├── data/
│   ├── login_cases.yaml    # 登录接口数据驱动用例
│   └── todo_cases.yaml     # 待办接口数据驱动用例
├── testcase/
│   ├── conftest.py         # fixtures: 启动被测系统 / 已登录客户端 / 匿名客户端
│   ├── test_login.py       # 登录接口测试(正常 + 异常场景)
│   └── test_todo.py        # 待办接口测试(CRUD 业务流 + 参数异常 + 鉴权)
├── .github/workflows/ci.yml  # GitHub Actions: 提交代码自动跑测试并归档报告
├── pytest.ini
└── requirements.txt

运行方式

pip install -r requirements.txt

# 跑全部用例并生成 HTML 报告
python -m pytest testcase --html=report/report.html --self-contained-html

框架会自动以子进程方式启动 app/todo_server.py(默认 127.0.0.1:5099), 测试结束后自动回收进程,无需手动部署被测系统。

覆盖的测试场景

模块 场景
登录接口 正常登录、密码错误(403)、缺少参数(400)、空请求体(400)
待办接口 创建成功、空白标题(400)、CRUD 完整业务流、列表状态过滤
边界与异常 查询不存在资源(404)、删除后再次查询(404)
安全/鉴权 未携带 token 访问全部接口被拒(401)

框架设计要点

  • 数据驱动:测试数据与脚本分离,用例以 yaml 维护,parametrize 批量执行
  • fixture 管理生命周期:conftest.py 会话级启动被测系统、提供已登录/匿名客户端
  • 统一断言封装:common/check.py 让断言失败信息包含期望/实际值,便于定位
  • 请求日志:客户端自动记录请求方法与响应摘要
  • CI 集成:push 到 main 分支自动执行全部用例并归档 HTML 报告

后续可扩展方向

  • 接入 Allure 报告与失败截图
  • 增加并发/性能冒烟脚本(locust)
  • 用例失败自动重试(pytest-rerunfailures)
  • 多环境配置切换(dev / test)

About

接口自动化测试框架 Demo:pytest + requests + Flask 被测系统,数据驱动用例、fixtures 生命周期管理、GitHub Actions 持续集成

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages