批量造數(shù)據(jù)
- 連接Mysql的信息
1 import pymysql 2 # 數(shù)據(jù)庫(kù)連接信息 3 # 多個(gè)庫(kù)要有多個(gè)conn 4 conn = pymysql.connect( 5 host="主機(jī)", 6 user="用戶名", 7 password="密碼", 8 database="庫(kù)名" 9 ) 10 conn1 = pymysql.connect( 11 host="主機(jī)", 12 user="用戶名", 13 password="密碼", 14 database="庫(kù)名" 15 ) 16 17 # 創(chuàng)建游標(biāo)對(duì)象 18 cursor = conn.cursor() 19 cursor1 = conn1.cursor() 20 21 # 執(zhí)行對(duì)應(yīng)的SQL 22 cursor.execute 23 # 獲取執(zhí)行結(jié)果 24 Result=cursor.fetchall()
場(chǎng)景一:基于已有的csv文件,分批次讀取csv文件中的字段值作為變量填充到執(zhí)行的SQL語(yǔ)句
- 分批讀取csv文件中的值
1 csv_file_path = 'csv文件目錄' 2 with open(csv_file_path, 'r',encoding='utf-8') as file: 3 reader = csv.reader(file) 4 next(reader) # Skip the header row 5 6 batch_size = 100 # 每批處理的數(shù)量 7 total_items = 3100 # 總共需要處理的數(shù)量 8 9 for i in range(0, total_items, batch_size): 10 # 在每次循環(huán)中處理 batch_size 個(gè)項(xiàng)目 11 # 可以在循環(huán)體內(nèi)部使用 i 作為起始索引 12 13 for j in range(i, min(i + batch_size, total_items)): 14 row = next(reader) 15 # 打印這一行的數(shù)據(jù) 16 print(row)
? 場(chǎng)景二:隨機(jī)生成特殊字段的值,作為變量填充到Insert語(yǔ)句中
- 隨機(jī)生成統(tǒng)代
1 import random 2 import string 3 def generate_credit_code(): 4 # 生成第1位登記管理部門(mén)代碼 5 管理部門(mén)代碼 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D'] 6 register_department = random.choice(管理部門(mén)代碼) 7 # print('管理部門(mén)代碼為',register_department) 8 9 # 生成2-9位組織機(jī)構(gòu)代碼 10 organizations_code = [] 11 for _ in range(8): 12 org_code = '' 13 for _ in range(8): 14 org_code += random.choice(string.ascii_uppercase + string.digits) 15 organizations_code.append(org_code) 16 organizations_code=random.choice(organizations_code) 17 # print('組織機(jī)構(gòu)代碼為',organizations_code) 18 19 20 # 生成10-17位統(tǒng)一社會(huì)信用代碼 21 unification_credit_code = '' 22 for _ in range(8): 23 unification_credit_code += random.choice(string.ascii_uppercase + string.digits) 24 # print('統(tǒng)一社會(huì)信用代碼為',unification_credit_code) 25 26 # 組合統(tǒng)一社會(huì)信用代碼 27 credit_code = f"{register_department}{''.join(organizations_code)}{unification_credit_code}" 28 return credit_code
- ?隨機(jī)生成注冊(cè)號(hào)
1 mport random 2 3 #這個(gè)注冊(cè)號(hào)是由15個(gè)隨機(jī)數(shù)字組成的,使用random.choice方法從0-9中隨機(jī)選擇數(shù)字。這個(gè)方法會(huì)被調(diào)用15次,每次都會(huì)生成一個(gè)隨機(jī)數(shù)字,然后通過(guò)字符串的join方法將這15個(gè)數(shù)字拼接在一起。 4 def generate_reg_code(): 5 # 15位注冊(cè)號(hào),以0開(kāi)頭 6 reg_code = ''.join(random.choice('0123456789') for i in range(15)) 7 return reg_code
?
結(jié)合python+pytest+fixture 實(shí)現(xiàn)定時(shí)任務(wù)接口調(diào)用
目錄結(jié)構(gòu)
(有些亂。。。
?
-- config.ini? ? ?存放的是系統(tǒng)固定的url之類(lèi)的
?
-- conftest.py? 一般用于放登錄接口,用戶返回token,利用fixture被其他接口使用
1 import pytest 2 import requests 3 import pymysql 4 from config import readconfig 5 readcon = readconfig.Read() 6 7 8 @pytest.fixture(scope="session") 9 # 這個(gè)方法是pytest封裝公共方法的一個(gè)文件,文件名必須是(conftest.py) 10 # 作用: 其他地方在使用這個(gè)方法時(shí)就不用from XX import cc 然后也不用實(shí)例化了 11 12 13 def test_login(): 14 msg = { 15 "username": '用戶名', 16 "password": '加密后的密碼' 17 } 18 19 url =readcon.get_URL("baseurl") 20 cc = requests.post(url+"api/uxxxxxxr/login", params=msg) 21 getjson = cc.json() 22 23 # 獲取token 24 tok = getjson['data']['token'] 25 userid = getjson['data']['userId'] 26 return tok, userid
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-709748.html
定時(shí)任務(wù)
import pytest import requests from config import readconfig read = readconfig.Read() class TestCase1: global url, tim # 全局變量,便于其他地方調(diào)用 url = read.get_URL("baseurl") tim = read.get_URL("timeout") def test_case1(self, test_login): head = {'Content-Type': 'application/json', 'Authorization': test_login[0]} # test_login[0]為token NewtestCreditCodeList = [] SelectNewtestGs = "select 字段1,字段2,字段3 from 數(shù)據(jù)表 order by id desc limit 100" cursor.execute(SelectNewtestGs) SelectNewtestResult = cursor.fetchall() for tuple in SelectNewtestResult: NewtestCreditCodeList.append(tuple[2]) NewtestGsCreditCodeListResult = ', '.join('"' + i + '"' for i in NewtestCreditCodeList) print('結(jié)果為', NewtestGsCreditCodeListResult) r = requests.get(url + 'api/exxxxxh/txxx/xxxxx?入?yún)?'+NewtestGsEidListResult, headers=head) print(r.json())
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-709748.html
到了這里,關(guān)于Python腳本批量造數(shù)據(jù)、跑定時(shí)任務(wù)協(xié)助測(cè)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!