当前位置: 首页 > 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/'])

相关文章:

  • 明心见性与真如三昧
  • 为什么RPN经过的候选框处理后,要使用rcnn来进行候选框的分类和回归操作?
  • Android audio_policy_configuration.xml加载流程
  • 基于SIMMECHANICS的单自由度磁悬浮隔振器PID控制系统simulink建模与仿真
  • VLAN间通讯技术
  • 关于QTableWidget控件中不显示Item的问题解决方法
  • MCP是什么?
  • windows搭建wireshark抓包snmp协议环境步骤
  • 3.THREE.FogExp2 全面详解 + 实例演示 + 深度解析
  • 【Redis】SpringDataRedis
  • 【单片机 C语言】单片机学习过程中常见C库函数(学习笔记)
  • 悬空引用和之道、之禅-《分析模式》漫谈57
  • 异或区间的划分
  • CmStick CmStick ME|精工品质与工业级安全的加密狗之选[特殊字符][特殊字符]
  • CTF web入门之SQL注入使用工具sqlmap
  • 基于Spring Boot+微信小程序的智慧农蔬微团购平台-项目分享
  • 国产仪器进化论:“鲁般号”基于无人机的天线测试系统
  • idea使用docker插件一键部署项目
  • 2025年一站式AI创作平台主要功能介绍及使用教程
  • Idea中实用设置和插件
  • 陕西一批干部任职公示:西安市未央、雁塔、阎良区委书记拟调整
  • 用8年还原曹操墓鉴定过程,探寻曹操墓新书创作分享会举行
  • 中国旅游日主题月期间,东航将准备超51.9万套特惠机票
  • 李家超将率团访问浙江
  • 人民网评:“中国传递爱而不是关税”
  • 广西:启动旱灾防御三级应急响应