Python接口自動(dòng)化淺析如何處理動(dòng)態(tài)數(shù)據(jù)
在上一篇Python接口自動(dòng)化測(cè)試系列文章:Python接口自動(dòng)化淺析logging封裝及實(shí)戰(zhàn)操作,主要介紹如何提取token、將token作為類(lèi)屬性全局調(diào)用及充值接口如何攜帶token進(jìn)行請(qǐng)求。
以下主要介紹:接口自動(dòng)化過(guò)程中,動(dòng)態(tài)數(shù)據(jù)如何生成、動(dòng)態(tài)數(shù)據(jù)與數(shù)據(jù)庫(kù)數(shù)據(jù)進(jìn)行對(duì)比并替換。
一、應(yīng)用場(chǎng)景F
注冊(cè)接口參數(shù)需要手機(jī)號(hào),手機(jī)號(hào)如何動(dòng)態(tài)生成?
生成的手機(jī)號(hào)如何與數(shù)據(jù)庫(kù)數(shù)據(jù)進(jìn)行對(duì)比?
未注冊(cè)的手機(jī)號(hào)如何替換用例數(shù)據(jù)中的手機(jī)號(hào)?
二、動(dòng)態(tài)手機(jī)號(hào)處理思路
- 編寫(xiě)函數(shù),生成隨機(jī)的手機(jī)號(hào);
- 將生成的手機(jī)號(hào)進(jìn)行數(shù)據(jù)庫(kù)查詢(xún);
- 如手機(jī)號(hào)已存在,就重新生成手機(jī)號(hào);
- 如手機(jī)號(hào)不存在,就將此手機(jī)號(hào)替換測(cè)試用例中的手機(jī)號(hào)。
三、動(dòng)態(tài)手機(jī)號(hào)處理
1、注冊(cè)接口測(cè)試用例
在data/cases.xlsx中,新建register工作簿,填充注冊(cè)接口用例,其中mobile_phone是動(dòng)態(tài)參數(shù),
如果寫(xiě)死,在自動(dòng)化過(guò)程中,會(huì)運(yùn)行失敗,所以這里用#new_phone#表示。
2、動(dòng)態(tài)生成手機(jī)號(hào)
在common目錄下,新建文件helper.py,用于編寫(xiě)輔助函數(shù),
實(shí)現(xiàn)特定的功能(類(lèi)似于HttpRunner中的debugtalk.py)。
實(shí)現(xiàn)批量生成11位手機(jī)號(hào),代碼如下:
import random def generate_mobile(): """生成隨機(jī)手機(jī)號(hào)""" phone = "1" + random.choice(["3","5","7","8","9"]) for i in range(0,9): num = random.randint(1,9) phone += str(num) return phone if __name__ == '__main__': print(generate_mobile())
運(yùn)行之后,結(jié)果為:
13889546979
上面代碼生成批量手機(jī)號(hào),比較簡(jiǎn)易,如對(duì)手機(jī)號(hào)格式要求更精確,可以自行按要求編寫(xiě)。
四、數(shù)據(jù)庫(kù)查詢(xún)并替換
1、replace()方法
描述:
replace()
方法把字符串中的 old(舊字符串) 替換成 new(新字符串)
replace語(yǔ)法:
str.replace(old, new[, max])
old
-- 將被替換的字符串。
new
-- 新字符串,用于替換old字符串。
max
-- 可選字符串, 替換不超過(guò) max 次
replace實(shí)戰(zhàn)例子:
現(xiàn)有字符串如下:
Str = 'coco愛(ài)讀書(shū)'
現(xiàn)在將Str中的coco改為vivi
Str = 'coco愛(ài)讀書(shū)' print(Str.replace('coco', 'vivi'))
輸出結(jié)果如下:
vivi愛(ài)讀書(shū)
2、編寫(xiě)注冊(cè)接口用例
接下來(lái)的注冊(cè)接口用例代碼,大多數(shù)代碼其實(shí)和登錄用例一樣,只是新增了查詢(xún)數(shù)據(jù)庫(kù)操作。
大致思路如下:
- 從excel中讀取用例數(shù)據(jù);
- 判斷用例數(shù)據(jù)中是否包含#new_phone#;
- 如包含#new_phone#,則隨機(jī)生成手機(jī)號(hào);
- 如隨機(jī)生成的手機(jī)號(hào)在數(shù)據(jù)庫(kù)中存在,則重新生成;
- 如隨機(jī)生成的手機(jī)號(hào)在數(shù)據(jù)庫(kù)中不存在,則用此手機(jī)號(hào)替換#new_phone#,進(jìn)行注冊(cè)。
import json import unittest from common.db_handler import DBHandler from common.helper import generate_mobile from common.logger_handler import logger from common.requests_handler import RequestHandler from common.excel_handler import ExcelHandler from config.setting import config from libs import ddt from middleware.yaml_handler import yaml_data @ddt.ddt class TestRegister(unittest.TestCase): # 讀取register sheet數(shù)據(jù) excel = ExcelHandler(config.data_path) data = excel.read_excel('register') def setUp(self): self.req = RequestHandler() self.db = DBHandler(host=yaml_data['mysql']['host'], port=yaml_data['mysql']['port'], user=yaml_data['mysql']['user'], password=yaml_data['mysql']['password'], database=yaml_data['mysql']['db'], charset=yaml_data['mysql']['charset']) def tearDown(self): self.req.close_session() self.db.close() @ddt.data(*data) def test_register(self,items): # 判斷#new_phone#是否在用例數(shù)據(jù)中 if "#new_phone#" in items['payload']: while True: # 使用自動(dòng)生成手機(jī)號(hào)的函數(shù) mobile = generate_mobile() # 從數(shù)據(jù)庫(kù)中查詢(xún)此手機(jī)號(hào)是否存在 query_mobile = self.db.query("select * from member where mobile_phone=%s;",args=[mobile]) # 如果不存在,就跳出循環(huán) if not query_mobile: break # 將#new_phone#替換為生成的手機(jī)號(hào) items['payload'] = items['payload'].replace('#new_phone#', mobile) logger.info('*'*30) logger.info('測(cè)試第{}條測(cè)試用例:{}'.format(items['case_id'],items['case_title'])) logger.info('測(cè)試數(shù)據(jù)是:{}'.format(items)) # 訪問(wèn)注冊(cè)接口,獲取實(shí)際結(jié)果 res = self.req.visit(items['method'],config.host+items['url'], json=json.loads(items['payload'])) # 斷言:預(yù)期結(jié)果與實(shí)際結(jié)果對(duì)比 try: self.assertEqual(res['code'],items['expected_result']) logger.info(res) result = 'PASS' except AssertionError as e: logger.error("測(cè)試用例執(zhí)行失敗{}".format(e)) result = 'fail' raise e finally: TestRegister.excel.write_excel(config.data_path,'register',items['case_id']+1,8,res['code']) TestRegister.excel.write_excel(config.data_path,'register',items['case_id'] + 1,9, result) if __name__ == '__main__': unittest.main()
那么,大家在接口自動(dòng)化過(guò)程中,是如何處理動(dòng)態(tài)數(shù)據(jù)的?
以上就是Python接口自動(dòng)化淺析如何處理動(dòng)態(tài)數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Python接口自動(dòng)化動(dòng)態(tài)數(shù)據(jù)處理的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。