模塊建站平臺(tái)專(zhuān)業(yè)的推廣公司
目錄:導(dǎo)讀
- 前言
- 一、Python編程入門(mén)到精通
- 二、接口自動(dòng)化項(xiàng)目實(shí)戰(zhàn)
- 三、Web自動(dòng)化項(xiàng)目實(shí)戰(zhàn)
- 四、App自動(dòng)化項(xiàng)目實(shí)戰(zhàn)
- 五、一線大廠簡(jiǎn)歷
- 六、測(cè)試開(kāi)發(fā)DevOps體系
- 七、常用自動(dòng)化測(cè)試工具
- 八、JMeter性能測(cè)試
- 九、總結(jié)(尾部小驚喜)
前言
接口自動(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)景
注冊(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)行失敗。
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ù)操作。
大致思路如下:
①?gòu)膃xcel中讀取用例數(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 efinally: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()
運(yùn)行結(jié)果為:
{'code': 0,'msg': 'OK','data': {'leave_amount': 240.0,'mobile_phone': '155********',}
}
下面是我整理的2023年最全的軟件測(cè)試工程師學(xué)習(xí)知識(shí)架構(gòu)體系圖 |
一、Python編程入門(mén)到精通
二、接口自動(dòng)化項(xiàng)目實(shí)戰(zhàn)
三、Web自動(dòng)化項(xiàng)目實(shí)戰(zhàn)
四、App自動(dòng)化項(xiàng)目實(shí)戰(zhàn)
五、一線大廠簡(jiǎn)歷
六、測(cè)試開(kāi)發(fā)DevOps體系
七、常用自動(dòng)化測(cè)試工具
八、JMeter性能測(cè)試
九、總結(jié)(尾部小驚喜)
生命不息,奮斗不止,每一份汗水都是青春的燃料。勇往直前,超越自我,追尋無(wú)限可能。別放棄,堅(jiān)持到底,勝利屬于那些敢于為之拼搏的人。揮灑激情,開(kāi)創(chuàng)輝煌!
時(shí)光流轉(zhuǎn),歲月更替,唯有奮斗永恒。勇往直前,不畏艱險(xiǎn),拼搏出無(wú)限可能。披荊斬棘,化壓力為動(dòng)力,砥礪前行,成就自我輝煌。堅(jiān)守夢(mèng)想,綻放光芒,努力奮斗,收獲終將屬于你!
人生猶如舞臺(tái),奮斗是最精彩的演出。勇往直前,超越極限,用汗水澆灌夢(mèng)想的花朵。不畏失敗,堅(jiān)持不懈,相信自己的力量,創(chuàng)造屬于自己的輝煌篇章。