当前位置: 首页 > news >正文

pytest-项目结构

项目结构

api_test_project/
├── config/
│   └── config.py  # 配置文件,存储接口的基本信息,如 URL、请求头、认证信息等
├── data/
│   └── test_data.json  # 测试数据文件,存储接口的请求参数、预期结果等
├── tests/
│   ├── __init__.py
│   ├── test_single_api.py  # 单接口测试用例文件
├── utils/
│   ├── __init__.py
│   └── api_client.py  # 封装接口请求的工具类
├── report/
│   └── report.html  # 测试报告文件
├── requirements.txt  # 项目依赖文件
└── run_tests.py  # 运行测试用例的脚本

1. config目录——存放项目的配置文件

# config.py
# 接口的基本 URL
BASE_URL = 'https://api.example.com'
# 公共请求头
HEADERS = {'Content-Type': 'application/json','Authorization': 'Bearer your_token'
}

2. data目录——存放测试数据

// test_data.json
{"get_user": {"request": {"method": "GET","endpoint": "/users/1","params": {}},"expected": {"status_code": 200,"data": {"id": 1,"name": "John Doe"}}}
}

3. test 目录——存放测试用例文件

# tests/test_single_api.py
import pytest
import requests
from config.config import BASE_URL, HEADERS
import json# 读取测试数据
with open('../data/test_data.json', 'r', encoding='utf-8') as f:TEST_DATA = json.load(f)@pytest.mark.parametrize("test_case", TEST_DATA.values())
def test_single_api(test_case):request_info = test_case['request']expected = test_case['expected']method = request_info['method']endpoint = request_info['endpoint']params = request_info.get('params', {})url = BASE_URL + endpointif method == 'GET':response = requests.get(url, headers=HEADERS, params=params)elif method == 'POST':response = requests.post(url, headers=HEADERS, json=params)# 可以根据需要添加更多的请求方法# 断言响应状态码assert response.status_code == expected['status_code']# 断言响应数据if 'data' in expected:assert response.json() == expected['data']

4. utils目录——存放工具类

# utils/api_client.py
import requests
from config.config import BASE_URL, HEADERSclass APIClient:def __init__(self):self.base_url = BASE_URLself.headers = HEADERSdef send_request(self, method, endpoint, params=None):url = self.base_url + endpointif method == 'GET':response = requests.get(url, headers=self.headers, params=params)elif method == 'POST':response = requests.post(url, headers=self.headers, json=params)# 可以根据需要添加更多的请求方法return response

5. report目录——存放测试报告文件

运行测试用例后会生成 report.html 文件。

6. requirements.txt文件

该文件列出了项目的依赖库,例如:

pytest

requests

pytest-html

7. run_tests.py文件

该文件用于运行测试用例并生成测试报告:

# run_tests.py
import pytestif __name__ == "__main__":pytest.main(['-s', '-v', '--html=report/report.html', 'tests/'])

http://www.xdnf.cn/news/57367.html

相关文章:

  • 管道位移自动化监测方案
  • neo4j-community-3.5.5-unix.tar.gz安装
  • leetcode 647. Palindromic Substrings
  • 从规则到大模型:知识图谱信息抽取实体NER与关系RE任务近10年演进发展详解
  • DSRAM介绍
  • 美创科技20周年庆典顺利举行
  • npm -v npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。来看看永久修改执行策略!
  • Pytorch的极简transformer用于时间序列预测
  • 负载均衡与实时调度—LSF
  • 精益数据分析(10/126):深度剖析数据指标,驱动创业决策
  • 硬件测试项之电源纹波的测量和纹波的要求、纹波的抑制
  • Uniapp:pages.json页面路由
  • 【Linux】进程替换与自定义 Shell:原理与实战
  • Uniapp:创建项目
  • vue3 主题模式 结合 element-plus的主题
  • spark与hadoop的区别
  • 新能源汽车充电桩运营模式的发展与优化路径探析
  • Docker Compose 和 Kubernetes(k8s)区别
  • 为什么RPN经过的候选框处理后,要使用rcnn来进行候选框的分类和回归操作?
  • Windows1909,21H2哪个版本更稳定
  • RHCSA Linux系统 用户和组的管理
  • 【GPLT】2025年第十届团队程序设计天梯赛赛后题解
  • 鸿蒙NEXT开发LRUCache缓存工具类(单例模式)(ArkTs)
  • 【仿Mudou库one thread per loop式并发服务器实现】HTTP协议模块实现
  • 系统分析师知识点:访问控制模型OBAC、RBAC、TBAC与ABAC的对比与应用
  • Unreal 如何实现一个Vehicle汽车沿着一条指定Spline路径自动驾驶
  • SpringBoot和微服务学习记录Day3
  • PCB 射频天线设计和版图创建技巧
  • Redis 的单线程模型对微服务意味着什么?需要注意哪些潜在瓶颈?
  • 系统架构设计(二):基于架构的软件设计方法ABSD