一、Mysql数据库封装

在common中新建一个mysql_db_util.py的文件

import pymysqlclass MysqlDBUtil:#字典输出def __init__(self, host, user, password, port=3306):self.connect = pymysql.Connect(host=host,port=port,user=user,password=password,charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)def exe_sql(self, sql):try:# 获取游标对象cursor = self.connect.cursor()# 调用游标对象的execute方法,执行sqlcursor.execute(sql)#  如果是查询if sql.split()[0].lower() == "select":# 返回所有数据return cursor.fetchall()#  否则:else:# 提交事务self.connect.commit()# 返回受影响的行数return cursor.rowcountexcept Exception as e:# 事务回滚self.connect.rollback()# 打印异常信息print(e)finally:# 关闭游标cursor.close()# 关闭连接self.connect.close()if __name__ == '__main__':db_util = MysqlDBUtil(host='localhost', user='root', password='123456')result = db_util.exe_sql('select * from community.tb_user')print(result)

输出结果如下:

发贴的测试用例添加数据库的断言校验以及连接数据库通过配置文件获取数据库的账号、密码等信息;

import pytestfrom api.portal.portalArticle.create_article_api import CreateArticleApi
from common.file_load import read_excel, load_yaml_file
from common.mysql_db_util import MysqlDBUtilclass TestCreateArticleApi:test_data = read_excel('\data\community_testdata.xlsx', '发帖数据')# test_data = [#     ['标题为空', '', '超级管理员', '内容', '1', 200, '操作成功'],#     ['正常发帖', '1111', '超级管理员', '内容', '1', 200, '操作成功'],#     ['发帖用户为空', '1111', '', '内容', '1', 200, '操作成功'],# ]@pytest.mark.parametrize('casename,title, author, content, createUser,expect_code,expect_message', test_data)def test_buy_now_params(self, casename, title, author, content, createUser, expect_code, expect_message):createArticleApi = CreateArticleApi(title, author, content, createUser)resp = createArticleApi.sendRequest()print(resp.text)status_code = resp.status_codeassert status_code == expect_codemessage = resp.json()['msg']assert message == expect_message# 通过yml获取DB的信息db_info = load_yaml_file('/config/db.yml')['community']host = db_info['host']userName = db_info['userName']password = db_info['password']port = db_info['port']mysqlDBUtil = MysqlDBUtil(host=host, user=userName, password=password,port=port)sqlResult = mysqlDBUtil.exe_sql(f"select count(1) as nub from community.tb_article where title = '{title}'")[0]['nub']assert sqlResult != 0

二、redis数据库封装

新建一个redis_util.py

import javaobj
import redisclass RedisUtil:def __init__(self,host,pwd,port=6379,decode_responses=False,db=0):# 先创建一个连接池self.pool = redis.ConnectionPool(host=host, password=pwd,port=port, decode_responses=decode_responses, db=db)self.r = redis.Redis(connection_pool=self.pool)  # 从连接池里拿到一个连接对象# 通过连接对象,对redis进行操作def get(self,key):# 判断该key对应的数据类型是什么,然后决定要用哪个数据获取方法type = self.r.type(key).decode('utf-8')if type == 'string':return self.r.get(key)elif type == 'hash':return self.r.hgetall(key)elif type == 'list':return self.r.lrange(key,0,-1)elif type == 'set':return self.r.smembers(key)elif type == 'zset':return self.r.zrange(key,0,-1)else:raise Exception(f'{key}对应的数据类型不支持')if __name__ == '__main__':redis_util = RedisUtil(host='localhost',pwd='123456')res = redis_util.get('{XXX_XXX_XXX}_59') #59是用户id# res得到的数据是项目向缓存中存储的一个java对象的序列化数据,我们无法直接从中得到想要的数据# 所以我们需要先将该数据转换成python对象,然后再处理print(res)res_obejct = javaobj.loads(res)print(res_obejct)print(type(res_obejct))print(res_obejct[0])# 获取该对象下都有哪些属性信息,看到的业务属性需要和开发沟通是什么意思print(dir(res_obejct[0]))# 获取立即购买时存储到redis的num数量print(res_obejct[0].__getattribute__('num'))print(res_obejct[0].__getattribute__('skuId'))

Python+requests+pytest+allure封装接口自动化6-mysql、redis数据库封装相关推荐

  1. 2022超级好用的接口自动化测试框架:基于python+requests+pytest+allure实现

    众所周知,目前市面上大部分的企业实施接口自动化最常用的有两种方式: 1.基于工具类的接口自动化,如: Postman+Newman+Jenkins+Git/svn Jmeter+Ant+Jenkins ...

  2. 接口自动化测试框架:python+requests+pytest+allure实现

    接口自动化测试框架 一.接口自动化测试框架需要解决的问题 二.接口自动化测试框架目录结构 三.日志监控文件的信息 四.搭建具有企业Logo的定制化报告.    今年是以往10年中最坏的一年,是未来10 ...

  3. 接口自动化测试框架搭建:基于python+requests+pytest+allure实现

    目录 一.接口自动化测试框架需要具备什么功能? 二.接口自动化测试框架目录结构 三.日志监控文件的信息 四.搭建具有企业Logo的定制化报告. 众所周知,目前市面上大部分的企业实施接口自动化最常用的有 ...

  4. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

  5. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

    1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...

  6. python+requests+pytest+allure+yaml+DDT+logs 接口自动化框架使用手册

    一.单条测试用例 无ddt数据驱动的场景 1 config.yaml 中书写基础路径 2 在redloads模块中,新建一个demo_fun.py文件, 其中demo要用的方法写在其中 如:读取con ...

  7. Pytest+Allure+Jenkins接口自动化项目实战(一)

    经过一周多时间,基于python+pytest+excel+allure框架的接口自动化测试初版已基本实现,包括基本配置读取.用例读取.用例执行.sql读取执行.前置数据准备.后置数据清理以及测试报告 ...

  8. Python+Requests+Pytest+YAML+Allure实现接口自动化

    作者:wintest 链接:https://www.cnblogs.com/wintest/p/13423231.html 本项目实现接口自动化的技术选型:Python+Requests+Pytest ...

  9. bcb6通过https接口post数据_Python+Requests+Pytest+YAML+Allure实现接口自动化

    点击上方"编程派",选择设为"设为星标" 优质文章,第一时间送达! 本项目实现接口自动化的技术选型:Python+Requests+Pytest+YAML+Al ...

最新文章

  1. 使用NetFlow分析网络异常流量
  2. 函数 —— fork()将运行着的程序分成2个(几乎)完全一样的进程
  3. 笔记-项目干系人管理-规划干系人管理
  4. linux send 失败_Epoll学习服务器的实现-Linux内核原始Epoll结构
  5. 文件、文件夹操作(I)
  6. golang 面向接口编程
  7. 2198元买真全面屏手机!网友:笑而不语...
  8. 余承东:华为P40或是鸿蒙系统首款手机,新机明年3月发布
  9. Oracle序列号详解
  10. 在多label 的代码里面添加augmentation功能遇到的问题
  11. python除法运算定律_除法竖式算法的原理是什么?
  12. uctf-杂项题目分析
  13. 如何在JPG或BMP图片上显示输入的订单数据内容,并在报表打印时显示出来,后台数据库是SQL SERVER 2000 ,先谢了.高分!...
  14. r9270公版bios_R9280,R9270,HD7000,VBE7007.系显卡全套修改超频刷BIOS工具
  15. CronTrigger使用
  16. FAT文件系统解析(一) 引导扇区、FAT表及根目录区分析
  17. [数学知识][几何]求三角形面积的几种方法
  18. 萌娃投票程序php+mysql,PHP+MySql+jQuery实现的顶和踩投票功能
  19. [译]使用MVI打造响应式APP(三):状态折叠器
  20. 如何查看手机登录IP地址

热门文章

  1. Python蒙特卡洛算法
  2. 为组件添加Expires头,最大化利用浏览器缓存
  3. 足球视频AI(二)——球员与球的目标检测
  4. 如何用LLMs来赚钱?基于ChatGPT的商业模式指南
  5. 卡巴のZwSetSystemInformation的心寒
  6. 对账功能 (XXL-Job)
  7. java工具Joda-Time 日期的处理
  8. go-grpc环境配置(win10)
  9. windows11右键文件夹一直转圈,然后闪屏解决方法,亲测有效!!!
  10. 支持向量机之SVR 用法与参数详解 python