requests+pytest+allure接口测试框架搭建
文章目录
- requests+pytest+allure接口测试框架搭建
- 一、创建项目![在这里插入图片描述](https://img-blog.csdnimg.cn/969aa95077fc447c9b9d1bc4c8b58017.png)
- 1.创建配置文件
- 2.common文件夹下
- 3.case文件夹下
- 二、pytest输出HTML报告
- 三、将allure报告输出到report目录中
requests+pytest+allure接口测试框架搭建
一、创建项目
- case:接口测试用例
- common:公共方法
- config:配置相关
- report:测试报告
1.创建配置文件
将测试环境、url及登录账户等信息维护在此
2.common文件夹下
维护一些公用方法,例如读取配置文件、获取环境cookie等
读取配置如下
import os
import configparserclass readConfig():def __init__(self, locator):self.locator = locator# 获取文件当前位置(此处避免出错,获取文件绝对位置)cur_path = os.path.dirname(os.path.abspath(__file__))# 拼接配置文件路径config_path = os.path.join(cur_path, "..\\config\\", self.locator)# 引用configparser类self.conf = configparser.ConfigParser()# 读取配置文件self.conf.read(config_path, encoding='utf-8')# 读取测试环境def read_test_evn(self, emo):test_evn = self.conf.get('TEST_EVN', emo)return test_evn# 读取测试账户def read_test_usr(self, msg):test_usr = self.conf.get('TEST_USER', msg)return test_usrdef read_test_module(self):test_module = self.conf.get("TEST_MODULE", "registration")return test_module
获取cookie
import requests
from readConfig import readConfigdef get_cookie():# 调用读取文件类rc = readConfig('conf.ini')# 读取环境ip及端口ip_port = rc.read_test_evn('ip_port')# 读取登录pathlogin_url = rc.read_test_evn('login_url')# 读取登录账户信息userid = rc.read_test_usr('usr')pwd = rc.read_test_usr('pwd')r = requests.post(ip_port + login_url, data={"username": userid, "password": pwd}, verify=False, allow_redirects=False)cookies = r.cookies.get_dict()# 将获取的cookie重新拼装cookies = "SESSION=" + cookies['SESSION']return cookies
3.case文件夹下
存放我们需要测试的用例,举例测试一个查询接口:
import os
import sys
import time
import allure
import pytest
import requests
from pytest import assumesys.path.append((os.path.abspath(os.path.join(os.path.dirname(__file__), '../common'))))
from readConfig import readConfig
from get_cookie import get_cookie# 读取配置文件
rc = readConfig('conf.ini')
ip_port = rc.read_test_evn('ip_port')
select_url = rc.read_test_evn('select')
module_id = rc.read_test_module()
# 参数化
userdata1 = ["测试", "多多"]
# 获取cookie
cookies = get_cookie()@pytest.mark.parametrize("username", userdata1)
def test_01(username):"""@function:测试查询接口@param username:搜素姓名"""h = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8","Cookie": cookies}print(h)r = requests.post(ip_port+select_url, data={"patientname": username, "_modulePartId_": module_id}, headers=h, verify=False).json()code = r["code"]print(r)with assume: assert (code == 200)if __name__ == "__main__":pytest.main(["-s", "-v", "test_select.py"])
二、pytest输出HTML报告
命令:pytest ./case/test_select.py --html=report/report.html
指定运行test_select.py中的所有用例,report.html生成在report路径下,拖拽到浏览器中即可打开。
PS:如果出现下面的报错,安装pytest-html(pip install pytest-html)
三、将allure报告输出到report目录中
pytest是可以直接扫描符合条件的testcase的,因此不用像unittest框架那样在根目录下建一个main.py来运行,直接使用命令行工具即可运行。
1、这里使用了allure报告,需要先安装allure工具:
pip install allure-pytest==2.8.6 --index-url https://pypi.douban.com/simple
安装完成之后,打开一个你之前写的pytest脚本,看能不正常使用,如果报错:AttributeError: module ‘allure’ has no attribute ‘severity_level’,这个是之前 pytest-allure-adaptor 这个插件与 allure-pytest 不能共存,卸载掉 pytest-allure-adaptor。
2、然后去github上下载allure插件
https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/
下载完成后解压到本地,将bin的路径添加到环境变量中。
3、随后在项目根目录下打开cmd窗口启用
输入:
pytest --alluredir ./report/allure_raw
4、生成html报表并在浏览器中打开
执行完成后,在当前目录下,report目录会生成一个allure_raw的原始文件,这个只是测试报告的原始文件,不能打开成html的报搞,再命令行启动一下即可:
allure serve report/allure_raw
requests+pytest+allure接口测试框架搭建相关推荐
- pytest+allure测试框架搭建
https://blog.csdn.net/wust_lh/article/details/86685912 https://www.jianshu.com/p/9673b2aeb0d3 定制化展示数 ...
- 接口自动化测试框架搭建:基于python+requests+pytest+allure实现
目录 一.接口自动化测试框架需要具备什么功能? 二.接口自动化测试框架目录结构 三.日志监控文件的信息 四.搭建具有企业Logo的定制化报告. 众所周知,目前市面上大部分的企业实施接口自动化最常用的有 ...
- 接口自动化测试框架:python+requests+pytest+allure实现
接口自动化测试框架 一.接口自动化测试框架需要解决的问题 二.接口自动化测试框架目录结构 三.日志监控文件的信息 四.搭建具有企业Logo的定制化报告. 今年是以往10年中最坏的一年,是未来10 ...
- 2022超级好用的接口自动化测试框架:基于python+requests+pytest+allure实现
众所周知,目前市面上大部分的企业实施接口自动化最常用的有两种方式: 1.基于工具类的接口自动化,如: Postman+Newman+Jenkins+Git/svn Jmeter+Ant+Jenkins ...
- python+requests+pytest 接口自动化框架(一)
目录 一.Pytest详解以及常用的插件安装 二.Pytest默认的测试用例的规则 三.Pytest用例运行方式以及参数 1.命令行模式运行 命令:pytes 2.主函数模式运行 3.基于pytest ...
- python+requests+pytest 接口自动化框架(四)
目录 一.requests库简介 二.requests库常用方法 三.request()返回的response对象 四.实战案例 post(url, data=None, json=None, **k ...
- pytest接口自动化框架搭建
目录: 一.设计思路-整体框架: 二.具体框架搭建 1.公共方法-common 1.1.yaml_util.py 1.2.excel_util.py 1.4.text_ util.py 1.5.exc ...
- pytest接口自动化测试框架搭建
文章目录 一. 背景 二. 基础环境 三. 项目结构 四.框架解析 4.1 接口数据文件处理 4.2 封装测试工具类 4.3 测试用例代码编写 4.4 测试用例运行生成报告 一. 背景 Pytest目 ...
- java版本-API接口测试框架搭建
基本概念:testng ,http, json, mysql, jenkins, spring 陆陆续续搭建起来的接口测试框架,使用起来并不是特别的理想,所以走上了一条迭代优化的不归路. 所谓的框架是 ...
最新文章
- 学习Python,这22个包怎能不掌握?
- App Store生存法则:iOS开发者经验分享
- Delphi应用程序的调试(四)The Debug Inspector
- Delphi中类型转换函数
- 线程池状态和使用注意点
- java中的类型擦除type erasure
- [react] react中的setState执行机制是什么呢?
- mysql内置含糊_mySQL入门04 内置函数
- c# 用正则表达式获取开始和结束字符串中间的值
- springboot怎么返回404_springboot异常处理之404
- Sublime好看的字体
- 网易面试总结——面试案例5~面试案例8
- 理解Mach-O文件格式(1)
- IT男人:四十岁是一枝花吗?
- windows10计算机放桌面,将win10计算器放在桌面上的操作方法
- tomcat问题——判断tomcat是否安装成功
- 浅谈Android性能优化方案
- PMI-ACP练习题(15)
- java getiotype_坑爹微信之读取PKCS12流时出现的java.io.IOException: DerInputStream.getLength...
- Week 5: Management of the Sporadic Nature of Data Flows 4 - UE-Triggered Service Request