一,介紹
上周用jmeter做性能測試時,接口B傳入的參數需要依賴接口A生成的借貸申請ID,接口A運行完需要把生成的借貸申請ID導出來到一個文件,作為參數傳給接口B,剛開始的時候,手動去數據庫倒,
倒了幾次感覺有些麻煩,就寫了一段python讀取數據庫並將讀到數據寫入到本地文件

二,python讀取數據庫代碼如下:
# -*- coding:utf-8 -*-
import pymysql
def get_loan_number(file):
connect = pymysql.Connect(
host="0.0.0.0",
port=3306,
user="test",
passwd="12345678",
db="test",
charset='utf8'
)
print("寫入中,請等待……")
cursor = connect.cursor()
sql = "select id from application where status='SUBMITTING' and contract like 'Performance-%' and " \
"loan_org_party='166490194444444444'"
cursor.execute(sql)
number = cursor.fetchall()
fp = open(file, "w")
loan_count = 0
for loanNumber in number:
loan_count += 1
fp.write(loanNumber[0] + "\n")
fp.close()
cursor.close()
connect.close()
print("寫入完成,共寫入%d條數據……" % loan_count)
if __name__ == "__main__":
file = r"C:\Users\test\Desktop\loanNUmber.txt"
get_loan_number(file)
運行結果如下:

三、寫到本地數據如下:

四、jmeter 用CSV Data Set Config讀取本地參數文件

