Python 接口测试之数据库封装
2023-06-13 09:16:50 时间
引言
做接口测试的时候,避免不了操作数据库。因为数据校验需要,测试数据初始化需要、一些参数化场景需要等。
数据库操作框架设计
这里主要操作mysql数据库,整体思路:
封装实现
具体代码实现:
import pymysql
import json
class OperateMysql(object):
def __init__(self):
# 数据库初始化连接
self.connect_interface_testing = pymysql.connect(
"localhost",
"root",
"123456",
"test",
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
# 创建游标操作数据库
self.cursor_interface_testing = self.connect_interface_testing.cursor()
def select_first_data(self, sql):
"""
查询第一条数据
"""
try:
# 执行 sql 语句
self.cursor_interface_testing.execute(sql)
except Exception as e:
print("执行sql异常:%s"%e)
else:
# 获取查询到的第一条数据
first_data = self.cursor_interface_testing.fetchone()
# print(first_data)
# 将返回结果转换成 str 数据格式,禁用acsii编码
first_data = json.dumps(first_data,ensure_ascii=False)
# self.connect_interface_testing.close()
return first_data
def select_all_data(self,sql):
"""
查询结果集
"""
try:
self.cursor_interface_testing.execute(sql)
except Exception as e:
print("执行sql异常:%s"%e)
else:
first_data = self.cursor_interface_testing.fetchall()
first_data = json.dumps(first_data,ensure_ascii=False)
# self.connect_interface_testing.close()
return first_data
def del_data(self,sql):
"""
删除数据
"""
res = {}
try:
# 执行SQL语句
result = self.cursor_interface_testing.execute(sql)
# print(result)
if result != 0:
# 提交修改
self.connect_interface_testing.commit()
res = {'删除成功'}
else:
res = {'没有要删除的数据'}
except:
# 发生错误时回滚
self.connect_interface_testing.rollback()
res = {'删除失败'}
return res
def update_data(self,sql):
"""
修改数据
"""
try:
self.cursor_interface_testing.execute(sql)
self.connect_interface_testing.commit()
res = {'更新成功'}
except Exception as e:
self.connect_interface_testing.rollback()
res = {'更新删除'}
return res
def insert_data(self,sql,data):
"""
新增数据
"""
try:
self.cursor_interface_testing.execute(sql,data)
self.connect_interface_testing.commit()
res = {data,'新增成功'}
except Exception as e:
res = {'新增失败',e}
return res
def conn_close(self):
# 关闭数据库
self.cursor_interface_testing.close()
if __name__ == "__main__":
# ()类的实例化
om = OperateMysql()
# 新增
data = [{'id': 1, 'name': '测试', 'age': 15}, {'id': 2, 'name': '老王', 'age': 10}, {'id': 3, 'name': '李四', 'age': 20}]
for i in data:
i_data = (i['id'],i['name'],i['age'])
insert_res = om.insert_data(
"""
INSERT INTO test_student (id,name,age) VALUES (%s,%s,%s)
""",i_data
)
print(insert_res)
# 查询
one_data = om.select_first_data(
"""
SELECT * FROM test_student;
"""
)
all_data = om.select_all_data(
"""
SELECT * FROM test_student;
"""
)
print(one_data)
# all_data字符串类型的list转list
print("查询总数据:%s",len(json.loads(all_data)),"分别是:%s",all_data)
# 修改
update_data = om.update_data(
"""
UPDATE test_student SET name = '王五' WHERE id = 1;
"""
)
print(update_data)
# 删除
del_data = om.del_data(
"""
DELETE FROM test_student WHERE id in (1,2,3);
"""
)
print(del_data)
# 关闭游标
om.conn_close()
运行结果:
为了方便演示,先注释删除数据的sql,再执行程序:
总结
以上就是python操作mysql数据库的常用方法,如果对python测试开发相关技术感兴趣的伙伴,欢迎加入测试开发学习交流QQ群:696400122,不积跬步,无以至千里。
相关文章
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- python爬虫—–Python访问http的几种方式「建议收藏」
- python表情代码_Python实现表情包的代码实例[通俗易懂]
- 【说站】python将数字转化为汉字
- 用python连接数据库、索引、优化、事务
- python pkl文件_Python字符串格式化输出的方式包括
- python type error是什么意思_Python 报错 TypeError:’DoesNotExist’对象不可调用
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(一)
- python-Python与PostgreSQL数据库-使用Python执行PostgreSQL查询(一)
- Python bytes类型及用法
- Python简易操作MySQL数据库指南(python操作mysql数据库)
- Python实现快速连接Redis数据库(python连接redis)
- Python轻松连接SQL Server数据库(python连接sqlserver)
- 利用Neo4j和Python进行无缝图形数据库支持(neo4j python)
- Python实现MySQL数据库的读取(python读取mysql)
- Python中使用MySQL数据库的操作技巧(mysql_python)