午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理

瀏覽:37日期:2022-07-26 09:58:30

引言

做接口測(cè)試的時(shí)候,避免不了操作數(shù)據(jù)庫(kù)。因?yàn)閿?shù)據(jù)校驗(yàn)需要,測(cè)試數(shù)據(jù)初始化需要、一些參數(shù)化場(chǎng)景需要等。

數(shù)據(jù)庫(kù)操作框架設(shè)計(jì)

這里主要操作mysql數(shù)據(jù)庫(kù),整體思路:

Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理

封裝實(shí)現(xiàn)

具體代碼實(shí)現(xiàn):

import pymysqlimport json class OperateMysql(object): def __init__(self): # 數(shù)據(jù)庫(kù)初始化連接 self.connect_interface_testing = pymysql.connect( 'localhost', 'root', '123456', 'test', charset=’utf8mb4’, cursorclass=pymysql.cursors.DictCursor ) # 創(chuàng)建游標(biāo)操作數(shù)據(jù)庫(kù) self.cursor_interface_testing = self.connect_interface_testing.cursor() def select_first_data(self, sql): ''' 查詢第一條數(shù)據(jù) ''' try: # 執(zhí)行 sql 語(yǔ)句 self.cursor_interface_testing.execute(sql) except Exception as e: print('執(zhí)行sql異常:%s'%e) else: # 獲取查詢到的第一條數(shù)據(jù) first_data = self.cursor_interface_testing.fetchone() # print(first_data) # 將返回結(jié)果轉(zhuǎn)換成 str 數(shù)據(jù)格式,禁用acsii編碼 first_data = json.dumps(first_data,ensure_ascii=False) # self.connect_interface_testing.close() return first_data def select_all_data(self,sql): ''' 查詢結(jié)果集 ''' try: self.cursor_interface_testing.execute(sql) except Exception as e: print('執(zhí)行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): ''' 刪除數(shù)據(jù) ''' res = {} try: # 執(zhí)行SQL語(yǔ)句 result = self.cursor_interface_testing.execute(sql) # print(result) if result != 0:# 提交修改self.connect_interface_testing.commit()res = {’刪除成功’} else:res = {’沒有要?jiǎng)h除的數(shù)據(jù)’} except: # 發(fā)生錯(cuò)誤時(shí)回滾 self.connect_interface_testing.rollback() res = {’刪除失敗’} return res def update_data(self,sql): ''' 修改數(shù)據(jù) ''' 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): ''' 新增數(shù)據(jù) ''' 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): # 關(guān)閉數(shù)據(jù)庫(kù) self.cursor_interface_testing.close() if __name__ == '__main__': # ()類的實(shí)例化 om = OperateMysql() # 新增 data = [{’id’: 1, ’name’: ’測(cè)試’, ’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轉(zhuǎn)list print('查詢總數(shù)據(jù):%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) # 關(guān)閉游標(biāo) om.conn_close()

運(yùn)行結(jié)果:

Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理

為了方便演示,先注釋刪除數(shù)據(jù)的sql,再執(zhí)行程序:

Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 怡红院在线观看 | 99tv| 日韩精品影视 | 色动态 | 强制高潮抽搐sm调教高h | 亚洲精品黄 | 久久久久久久久久久久国产 | 久久久久亚洲精品国产 | 欧洲久久久 | 成人观看免费视频 | 麻豆一二区 | 国产第1页 | 欧美日韩在线免费视频 | 久久精品5 | 天堂网中文在线观看 | 日韩欧美高清dvd碟片 | 色综合视频在线 | 中文字字幕在线中文 | 在线观看av免费 | 暖暖爱爱视频 | 男人av网 | 四虎在线观看视频 | 久久精品99久久久久久 | 精品久久久久久久久久久久久久久 | 婷婷色亚洲| 在线观看wwww | 99视频免费在线观看 | 国产黄色片在线观看 | 成人黄网免费观看视频 | 天天操天天操天天干 | 精品国产一区二区三区在线观看 | 亚洲天堂影院 | 国产精品www | 国产免费一区二区三区 | 国产日韩在线观看一区 | 精品1区2区 | 男人午夜天堂 | 成年人在线观看av | 4虎av| 国产成人精品白浆久久69 | www.夜夜夜 |