python + Pytest + requests 的接口自动化步骤
创始人
2025-01-07 22:04:14
0

python+pytest+request+allure+yaml接口自动化测试项目实战

开发环境准备

1. jdk 下载

Java官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
安装: https://blog.csdn.net/VA_AV/article/details/138508891

2. Pytest

# 安装 pip install pytest  #更新 pytest  pip install --upgrade pytest  # 确认 Pytest 是否已成功安装,并显示 Pytest 的版本信息 pytest --version 

python + Pytest + requests 的接口自动化

1. 设计框架结构

project_root/ ├── conf/                # 配置文件 │   ├── config.yaml      # 全局配置 │   └── env_config.yaml  # 环境配置 ├── data/                # 测试数据 │   ├── test_data.json   # 测试数据文件 │   └── ... ├── lib/                 # 自定义库 │   ├── api_client.py    # API客户端 │   └── utils.py         # 工具函数 ├── tests/               # 测试用例 │   ├── test_case1.py │   ├── test_case2.py │   └── ... ├── reports/             # 测试报告 │   ├── allure_report/   # Allure报告 │   └── pytest_report/   # Pytest报告 ├── requirements.txt     # 依赖包 └── README.md            # 项目说明 

2. 编写配置文件

# conf/config.yaml base_url: http://api.example.com timeout: 10  # conf/env_config.yaml dev:   base_url: http://dev-api.example.com   timeout: 5  prod:   base_url: http://prod-api.example.com   timeout: 10  

置文件用于管理全局配置和环境配置。例如:

3. 封装API客户端

封装一个API客户端,用于发送HTTP请求并处理响应。例如,使用Python和Requests库:

# lib/api_client.py import requests from conf.config import config  class APIClient:     def __init__(self):         self.base_url = config['base_url']         self.timeout = config['timeout']      def get(self, endpoint, params=None, headers=None):         url = f"{self.base_url}{endpoint}"         response = requests.get(url, params=params, headers=headers, timeout=self.timeout)         return response      def post(self, endpoint, json=None, headers=None):         url = f"{self.base_url}{endpoint}"         response = requests.post(url, json=json, headers=headers, timeout=self.timeout)         return response      # 其他HTTP方法类似  

4. 编写测试用例

编写具体的测试用例,使用测试框架来组织和运行这些用例。例如,使用Pytest:

# tests/test_case1.py from lib.api_client import APIClient  def test_get_user():     client = APIClient()     response = client.get('/users/1')     assert response.status_code == 200     assert response.json()['id'] == 1  def test_create_user():     client = APIClient()     data = {'name': 'John Doe', 'email': 'john.doe@example.com'}     response = client.post('/users', json=data)     assert response.status_code == 201     assert response.json()['name'] == 'John Doe'  

5. 集成报告工具

集成报告工具,生成详细的测试报告。例如,使用Allure:

# 安装Allure插件 pip install allure-pytest  # 运行测试并生成报告 # 要使 Allure 侦听器能够在测试执行期间收集结果,只需在命令行添加存储结果的文件夹的路径参数即可 pytest --alluredir=reports/allure_report #要在测试完成后查看实际报告,需要使用 Allure 命令行实用程序从结果生成报告: allure serve reports/allure_report  

allure的安装和使用(windows环境)

https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/

  1. 选择一个版本(windows下载.zip包就可以)
    下载完直接解压就好了(记住路径)
    打开包,打到bin目录,找到allure.bat双击运行

  2. 配置allure系统环境变量
    【计算机–属性–高级系统设置–环境变量–系统变量–path–编辑】
    环境变量添加刚才解压时allure的地址 放bin文件的路径:E:\tools\allure-2.30.0\bin

  3. 检验环境变量配置成功:打开终端命令行,输入:allure

6. 集成持续集成工具

将测试框架集成到持续集成工具中,实现自动化测试。
持续集成工具:Jenkins、GitLab CI等。

7. 文档和维护

编写详细的文档,包括项目说明、使用指南和维护手册。

相关内容

热门资讯

透视数据!智星德州有脚本吗,w... 《透视数据!智星德州有脚本吗,wepoker游戏下载(详细辅助解密教程)》 智星德州有脚本吗软件透明...
wpk辅助工具下载!拱趴大菠萝... wpk辅助工具下载!拱趴大菠萝开挂方法,wepoker底牌透视脚本下载(详细辅助解密教程);wpk辅...
辅助透视!wepoker轻量版... 辅助透视!wepoker轻量版透视,小程序边锋辅助(原来真的是有挂)1、小程序边锋辅助系统规律教程、...
透视了解“aapoker透视软... 透视了解“aapoker透视软件”真是真的是有挂(辅助挂)详细辅助详细教程透视了解“aapoker透...
透视ai代打!hhpoker万... 透视ai代打!hhpoker万能辅助器,聚星ai辅助工具激活码(详细辅助科技教程)1、不需要AI权限...
wepoker透视脚本免费下载... wepoker透视脚本免费下载!wepoker透视最简单三个步骤,wpk透视脚本链接(详细辅助202...
透视神器“wepoker免费透... 透视神器“wepoker免费透视脚本”果然存在有挂(辅助挂)详细辅助攻略教程1)辅助挂:进一步探索辅...
透视美元局!wepoker怎么... 透视美元局!wepoker怎么挂底牌,胡乐安庆辅助工具(真是是真的有挂)胡乐安庆辅助工具软件透明挂微...
透视辅助!wepoker透视,... 透视辅助!wepoker透视,德州之星扫描器(详细辅助教你攻略)关于德州之星扫描器机制的,其中提到了...
wepoker透视脚本安卓!p... wepoker透视脚本安卓!pokermaster脚本,wepoker底牌透视脚本http(详细辅助...