在上一篇Python接口自動化測試系列文章:Python接口自動化淺析logging封裝及實戰(zhàn)操作,主要介紹如何提取token、將token作為類屬性全局調(diào)用及充值接口如何攜帶token進行請求。
以下主要介紹:接口自動化過程中,動態(tài)數(shù)據(jù)如何生成、動態(tài)數(shù)據(jù)與數(shù)據(jù)庫數(shù)據(jù)進行對比并替換。
一、應(yīng)用場景F
注冊接口參數(shù)需要手機號,手機號如何動態(tài)生成?
生成的手機號如何與數(shù)據(jù)庫數(shù)據(jù)進行對比?
未注冊的手機號如何替換用例數(shù)據(jù)中的手機號?
二、動態(tài)手機號處理思路
- 編寫函數(shù),生成隨機的手機號;
- 將生成的手機號進行數(shù)據(jù)庫查詢;
- 如手機號已存在,就重新生成手機號;
- 如手機號不存在,就將此手機號替換測試用例中的手機號。
三、動態(tài)手機號處理
1、注冊接口測試用例
在data/cases.xlsx中,新建register工作簿,填充注冊接口用例,其中mobile_phone是動態(tài)參數(shù),
如果寫死,在自動化過程中,會運行失敗,所以這里用#new_phone#表示。

2、動態(tài)生成手機號
在common目錄下,新建文件helper.py,用于編寫輔助函數(shù),
實現(xiàn)特定的功能(類似于HttpRunner中的debugtalk.py)。
實現(xiàn)批量生成11位手機號,代碼如下:
import random
def generate_mobile():
"""生成隨機手機號"""
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())
運行之后,結(jié)果為:
13889546979
上面代碼生成批量手機號,比較簡易,如對手機號格式要求更精確,可以自行按要求編寫。
四、數(shù)據(jù)庫查詢并替換
1、replace()方法
描述:
replace()
方法把字符串中的 old(舊字符串) 替換成 new(新字符串)
replace語法:
str.replace(old, new[, max])
old
-- 將被替換的字符串。
new
-- 新字符串,用于替換old字符串。
max
-- 可選字符串, 替換不超過 max 次
replace實戰(zhàn)例子:
現(xiàn)有字符串如下:
現(xiàn)在將Str中的coco改為vivi
Str = 'coco愛讀書'
print(Str.replace('coco', 'vivi'))
輸出結(jié)果如下:
vivi愛讀書
2、編寫注冊接口用例
接下來的注冊接口用例代碼,大多數(shù)代碼其實和登錄用例一樣,只是新增了查詢數(shù)據(jù)庫操作。
大致思路如下:
- 從excel中讀取用例數(shù)據(jù);
- 判斷用例數(shù)據(jù)中是否包含#new_phone#;
- 如包含#new_phone#,則隨機生成手機號;
- 如隨機生成的手機號在數(shù)據(jù)庫中存在,則重新生成;
- 如隨機生成的手機號在數(shù)據(jù)庫中不存在,則用此手機號替換#new_phone#,進行注冊。
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:
# 使用自動生成手機號的函數(shù)
mobile = generate_mobile()
# 從數(shù)據(jù)庫中查詢此手機號是否存在
query_mobile = self.db.query("select * from member where mobile_phone=%s;",args=[mobile])
# 如果不存在,就跳出循環(huán)
if not query_mobile:
break
# 將#new_phone#替換為生成的手機號
items['payload'] = items['payload'].replace('#new_phone#', mobile)
logger.info('*'*30)
logger.info('測試第{}條測試用例:{}'.format(items['case_id'],items['case_title']))
logger.info('測試數(shù)據(jù)是:{}'.format(items))
# 訪問注冊接口,獲取實際結(jié)果
res = self.req.visit(items['method'],config.host+items['url'],
json=json.loads(items['payload']))
# 斷言:預(yù)期結(jié)果與實際結(jié)果對比
try:
self.assertEqual(res['code'],items['expected_result'])
logger.info(res)
result = 'PASS'
except AssertionError as e:
logger.error("測試用例執(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()
那么,大家在接口自動化過程中,是如何處理動態(tài)數(shù)據(jù)的?
以上就是Python接口自動化淺析如何處理動態(tài)數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Python接口自動化動態(tài)數(shù)據(jù)處理的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- Vue.js實現(xiàn)簡單動態(tài)數(shù)據(jù)處理
- 淺談如何使用python抓取網(wǎng)頁中的動態(tài)數(shù)據(jù)實現(xiàn)
- Python接口自動化測試框架運行原理及流程
- python接口自動化測試之接口數(shù)據(jù)依賴的實現(xiàn)方法
- python接口自動化(十六)--參數(shù)關(guān)聯(lián)接口后傳(詳解)