一、企業(yè)級應(yīng)用案例實踐
案例介紹
案例網(wǎng)址:https://mubu.com/
教學(xué)文檔鏈接: https://share.mubu.com/doc/2FBfG57sPS3 密碼: p35v
-
初始化項目:
-
httprunner startproject insaneloafer(venv) D:\Programs\DevOps\pythonProject>httprunner startproject insaneloafer 2021-05-30 15:26:45.667 | INFO | httprunner.scaffold:create_scaffold:43 - Create new project: insaneloafer Project Root Dir: D:\Programs\DevOps\pythonProject\insaneloafer created folder: insaneloafer created folder: insaneloafer\har created folder: insaneloafer\testcases created folder: insaneloafer\reports created file: insaneloafer\testcases\demo_testcase_request.yml created file: insaneloafer\testcases\demo_testcase_ref.yml created file: insaneloafer\debugtalk.py created file: insaneloafer\.env created file: insaneloafer\.gitignore $ tree insaneloafer -a 2021-05-30 15:26:45.682 | WARNING | httprunner.scaffold:show_tree:29 - tree command not exists, ignore. Sentry is attempting to send 0 pending error messages Waiting up to 2 seconds Press Ctrl-Break to quit - 上傳項目到GitHub
-
git status查看是否為GitHub的倉庫 -
git init將當(dāng)前項目創(chuàng)建為GitHub的倉庫 -
git add .將當(dāng)前項目添加到GitHub倉庫 -
git commit -m "initialize project"提交
-
-
進(jìn)行抓包
點擊登錄
登錄后點擊創(chuàng)建文檔
文檔內(nèi)容輸入Demo
-
在Charles中過濾靜態(tài)api,過濾器里輸入:
mubu.com/assert,然后將assert刪除
image.png -
選中所有接口,右鍵點擊
Export Session
image.png -
導(dǎo)出格式選中
.har,將其導(dǎo)出到項目的har目錄下
image.png
image.png -
在
gitignore文件中添加.idea一行,并上傳錄制文檔到GitHub- image.png
-
git add har/Insane.har進(jìn)行添加` -
git commit -m "錄制生成HAR文件"進(jìn)行提交
image.png
-
生成用例:
har2case har/Insane.har,默認(rèn)生成pytest格式。-2y可以生成yaml格式,-2j可以生成json格式
image.png -
將生成好的用例以及
__init__.py文件移動到testcase目錄下
image.png -
運行腳本,查看結(jié)果,發(fā)現(xiàn)創(chuàng)建的為無標(biāo)題的文檔,結(jié)果與預(yù)期不符
image.png

- 排查問題是由于創(chuàng)建的文檔id與其下所編輯的文檔id未做關(guān)聯(lián)
進(jìn)行參數(shù)關(guān)聯(lián)
-
參數(shù)提取方式:
-
body.xxx,從響應(yīng)體中提取關(guān)聯(lián)參數(shù),后面跟jmespath路徑 -
headers.xxx,從headers中提取關(guān)聯(lián)參數(shù) -
cookies.xxx,從cookies中提取關(guān)聯(lián)參數(shù)
-
-
jmespath定位:https://jmespath.org/
- 定位列表用
[number] - 定位字典用
. -
示例image.png
image.png -
如果所定位的字段有特殊符號會提示不符合jmespath格式,此時將有特殊符號的字段使用引號引起來便可image.png
- 定位列表用
-
在測試用例中實現(xiàn)
- 調(diào)用
.extract() - 再調(diào)用
.with_jmespath("body.data.id", "docID")
image.png - 將所有的docID進(jìn)行替換,替換格式
${docID}
image.png
- 調(diào)用
-
再次運行用例,發(fā)現(xiàn)用例通過
image.png -
設(shè)置userId關(guān)聯(lián)
-
userID首次出現(xiàn)位置為
image.png -
設(shè)置關(guān)聯(lián)
image.png
image.png
-
設(shè)置全局變量
- 在
config后面加入.variables(**{"key":"value"}) -
然后將所有的數(shù)據(jù)替換為變量
image.png
設(shè)置base_url
- 在config后加上
.base_url("url")
image.png -
將其他接口的base_url全部去掉image.png
設(shè)置host全局變量
- 在config的
.variables()中加入host全局變量,并進(jìn)行全局替換
image.png
設(shè)置電話號碼、密碼為全局變量
- 在config的
.variables()中加入phone和password全局變量,并進(jìn)行全局替換
image.png
設(shè)置單接口的局部變量
- 在
Step(RunRequest(xxx))后加上.with_variables(**{"key": "value"}),然后對變量進(jìn)行替換
image.png
增加斷言
-
同樣適用jmespath獲取內(nèi)容進(jìn)行斷言,在結(jié)果后面添加斷言語句
.assert_xxx()即可
image.png -
更多的校驗方式:
.assert_length_greater_than("body.data.folders", 5)
實現(xiàn)teardown
- 在step后加入
.teardown_hook("${sleep(2)}"),其中括號里為自定義或框架自帶的函數(shù)。這里實在接口運行完后sleep兩秒
image.png - 加載文檔列表后,獲取 folders 數(shù)量并賦值給 folders_num
-
在
debugtalk.py文件中定義函數(shù)get_folders_num,其中response為內(nèi)置變量,返回接口響應(yīng)結(jié)果
image.png -
在獲取文檔列表接口后加入
.teardown_hook("${get_folders_num($response)}", "folder_num")
image.png -
運行結(jié)果
image.png -
修改函數(shù)
image.png -
修改后的運行結(jié)果
image.png
-
再次修改函數(shù),返回folders數(shù)量
image.png -
再次運行,可以發(fā)現(xiàn)運行結(jié)果存儲到變量
folders_num中
image.png -
此時便可在step中進(jìn)行調(diào)用
image.png
- 替換
user-agent為HTTPRunner的版本,表示接口使用HTTPRunner運行的
image.png
實現(xiàn)簽名算法
- 在
debugtalk.py文件中定義實現(xiàn)md5加密的獲取token函數(shù)
image.png -
在測試用例對應(yīng)位置加入函數(shù)
image.png
設(shè)置文檔的全局標(biāo)題
- 在
debugtalk.py文件中加入以下函數(shù)
def get_random_title():
return f"demo-{random.randint(1, 99999)}"
-
在測試用例對應(yīng)位置加入函數(shù)
image.png
測試用例分層運行
-
在testcase目錄下創(chuàng)建
login_test.py文件,將于login相關(guān)的代碼拷貝進(jìn)去
image.png -
在
insane_test.py中運行l(wèi)ogin測試用例,運行的格式參考:- 運行
hmake testcases/demo_testcase_ref.yml,生成demo文件
image.png - 拷貝demo文件的導(dǎo)入命令至
insane_test.py中
image.png -
然后在teststeps列表中加入登錄的用例:
image.png
- 運行
導(dǎo)出測試用例的參數(shù)給其余用例使用
-
在測試用例的config中加入
.export("xxx")
image.png -
在另一個用例中進(jìn)行導(dǎo)入
image.png
測試用例相互獨立運行
-
hrun testcases/login_test.py testcases/insane_test.py
image.png
參數(shù)化數(shù)據(jù)驅(qū)動
- 直接制定參數(shù)列表
- 在yaml格式的測試用例中加入
parameters字典即可
image.png - 使用
hmake testcases/demo_testcase_request.yml生成對應(yīng)的python文件,發(fā)現(xiàn)此文件多出引用數(shù)據(jù)驅(qū)動的代碼
image.png -
運行用例,發(fā)現(xiàn)運行了四次
image.png
- 引用函數(shù)生成參數(shù)列表
-
debugtalk.py中創(chuàng)建批量生成文檔名函數(shù)
image.png -
在測試用例中調(diào)用
image.png -
再次運行,發(fā)現(xiàn)運行了指定次數(shù)
image.png
- 引用CSV 文件
-
創(chuàng)建csv文件,第一行為變量名
image.png - 在用例中進(jìn)行引用,多個變量用
-隔開
image.png
二、查看報告
- 默認(rèn) pytest-html 報告形式
hrun har/login.yml --html=login.html - allure 報告
- 安裝 allure 插件
pip install "httprunner[allure]"- 或者單獨安裝:
pip install allure-pytest
- 運行
hrun testcases --alluredir=allurereports/-
allure serve allurereports
image.png
- 安裝 allure 插件
三、性能測試
-
安裝插件
pip install "httprunner[locust]"或者
pip install locust
-
運行
locusts -f testcases/mubu_login_[test.py]
-
拓展
-
https://github.com/myzhan/boomer
image.png
image.png
-
https://github.com/myzhan/boomer























































