自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Tep集成 HttpRunner 與 Flask 實現(xiàn)開箱即用

開發(fā) 前端
剛開始只是我用來調(diào)試代碼的,等到把Mock寫完以后,想到可能大家也需要調(diào)試代碼,就把它做到tep里面了,并且附帶了測試用例的示例代碼,安裝完以后就能一鍵運(yùn)行,開箱即用,美滋滋。歸根結(jié)底,都是為了把tep做大做強(qiáng)。

 

大家好,我是剛哥。

趁著元旦假期最后一天,有著大把時間,奔著把tep做大做強(qiáng)的目標(biāo),好好學(xué)習(xí)了一波。在開始正文之前,先回答可能會問到的兩個問題。第一個問題是為什么要集成HttpRunner?因為我最近在思考如何給tep做分層設(shè)計,參考了我司現(xiàn)有的接口自動化平臺,它的設(shè)計是每個用例有很多測試步驟,可以針對用例設(shè)置預(yù)設(shè)變量,然后在測試步驟中引用。正當(dāng)我準(zhǔn)備自己開發(fā)類似功能時,想到了HttpRunner,我記得HttpRunner第3版是建議直接編寫pytest代碼而非以前的ymal或json文件了。大有所獲,HttpRunner正是以這種方式編寫的代碼,而且和pytest有很好的結(jié)合,很符合tep要集成的第三方包的希望。第二個問題是為什么要集成Flask?剛開始只是我用來調(diào)試代碼的,等到把Mock寫完以后,想到可能大家也需要調(diào)試代碼,就把它做到tep里面了,并且附帶了測試用例的示例代碼,安裝完以后就能一鍵運(yùn)行,開箱即用,美滋滋。歸根結(jié)底,都是為了把tep做大做強(qiáng)。

tep0.9.3正式發(fā)布

要體驗HttpRunner和Flask,需要先安裝或升級到tep0.9.3。

安裝:

  1. pip install tep 

升級:

  1. pip install -U tep 

或者指定版本:

  1. pip install tep==0.9.3 

安裝tep時會順帶安裝HttpRunner和Flask,安裝完以后就可以執(zhí)行命令初始化項目:

  1. tep startproject demo093 

輸出:

  1. D:\PycharmProjects>tep startproject demo093 
  2. 2022-01-03 16:07:31.929 | INFO     | tep.scaffold:create_scaffold:53 - Create new project: demo093 
  3. Project root dir: D:\PycharmProjects\demo093 
  4.  
  5. Created folder: demo093 
  6. Created folder: demo093\fixtures 
  7. Created folder: demo093\tests 
  8. Created folder: demo093\files 
  9. Created folder: demo093\reports 
  10. Created folder: demo093\utils 
  11. Created file: demo093\.gitignore 
  12. Created file: demo093\conf.yaml 
  13. Created file: demo093\conftest.py 
  14. Created file: demo093\pytest.ini 
  15. Created file: demo093\fixtures\__init__.py 
  16. Created file: demo093\fixtures\fixture_admin.py 
  17. Created file: demo093\fixtures\fixture_env_vars.py 
  18. Created file: demo093\fixtures\fixture_login.py 
  19. Created file: demo093\fixtures\fixture_your_name.py 
  20. Created file: demo093\tests\__init__.py 
  21. Created file: demo093\tests\test_login.py 
  22. Created file: demo093\tests\test_post.py 
  23. Created file: demo093\tests\test_mysql.py 
  24. Created file: demo093\tests\test_request.py 
  25. Created file: demo093\tests\test_login_pay.py 
  26. Created file: demo093\tests\test_login_pay_httprunner.py 
  27. Created file: demo093\utils\__init__.py 
  28. Created file: demo093\utils\flask_mock_api.py 
  • 修改了fixture_env_vars.py里面的domain為http://127.0.0.1:5000,這是Flask啟動后的默認(rèn)地址。
  • 修改了fixture_login.py里面的登錄url和username,跟Flask的Mock對應(yīng)。
  • 新增了utils\flask_mock_api.py,直接啟動Mock服務(wù)。
  • 新增了tests\test_login_pay.py,用例數(shù)據(jù)一體開發(fā)模式,登錄到下單流程的示例代碼,可以一鍵運(yùn)行成功。
  • 新增了tests\test_login_pay_httprunner.py,HttpRunner開發(fā)模式,登錄到下單流程的示例代碼,可以一鍵運(yùn)行成功。

新版README.md

之前tep的README是全英文的,這次我也決定不裝了,改成中文 ,豐富了內(nèi)容,大家可以對tep有個全新和全面的了解啦。以下是全文:

tep

tep是Try Easy Pytest的首字母縮寫,是一款基于pytest測試框架的測試工具,集成了各種實用的第三方包和優(yōu)秀的自動化測試設(shè)計思想,幫你快速實現(xiàn)自動化項目落地。

安裝

支持Python3.6以上,推薦Python3.8以上。

標(biāo)準(zhǔn)安裝:

  1. $ pip install tep 

國內(nèi)鏡像:

  1. $ pip --default-timeout=600 install -i https://pypi.tuna.tsinghua.edu.cn/simple tep 

檢查安裝成功:

  1. $ tep -V  # 或者 tep --version 
  2. 0.2.3 

快速創(chuàng)建項目

tep提供了腳手架,預(yù)置了項目結(jié)構(gòu)和代碼,打開cmd,使用startproject命令快速創(chuàng)建項目:

  1. tep startproject project_name 

并且提供了-venv參數(shù),在項目初始化時,可以同時創(chuàng)建一個虛擬環(huán)境(推薦):

  1. tep startproject project_name -venv 

輸出測試報告

tep提供了--tep-reports參數(shù)來生成allure測試報告:

  1. pytest  --tep-reports 

報告文件存放在根目錄的reports/中。

Mock服務(wù)

tep自帶了一個Flask應(yīng)用(utils/flask_mock_api.py),提供了登錄到下單流程的5個接口,啟動后即可一鍵運(yùn)行示例中的測試用例。

三種開發(fā)模式

tep兼容三種開發(fā)模式:用例數(shù)據(jù)一體(適合新手)、用例數(shù)據(jù)分離(適合老手)、HttpRunner(新老皆宜)。

①用例數(shù)據(jù)一體,示例代碼如下所示:

  1. def test(env_vars, login): 
  2.     # 搜索商品 
  3.     response = request( 
  4.         "get"
  5.         url=env_vars.domain + "/searchSku"
  6.         headers={"token": login.token}, 
  7.         params={"skuName""電子書"
  8.     ) 
  9.     sku_id = jmespath.search("skuId", response.json()) 
  10.     sku_price = jmespath.search("price", response.json()) 
  11.     assert response.status_code < 400 
  12.  
  13.     # 添加購物車 
  14.     sku_num = 3 
  15.     response = request( 
  16.         "post"
  17.         url=env_vars.domain + "/addCart"
  18.         headers={"token": login.token}, 
  19.         json={"skuId": sku_id, "skuNum": sku_num} 
  20.     ) 
  21.     total_price = jmespath.search("totalPrice", response.json()) 
  22.     assert response.status_code < 400 
  23.  
  24.     # 下單 
  25.     response = request( 
  26.         "post"
  27.         url=env_vars.domain + "/order"
  28.         headers={"token": login.token}, 
  29.         json={"skuId": sku_id, "price": sku_price, "skuNum": sku_num, "totalPrice": total_price} 
  30.     ) 
  31.     order_id = jmespath.search("orderId", response.json()) 
  32.     assert response.status_code < 400 
  33.  
  34.     # 支付 
  35.     response = request( 
  36.         "post"
  37.         url=env_vars.domain + "/pay"
  38.         headers={"token": login.token}, 
  39.         json={"orderId": order_id, "payAmount""6.9"
  40.     ) 
  41.     assert response.status_code < 400 
  42.     assert response.json()["success"] == "true" 

更多內(nèi)容請參考《如何使用teprunner測試平臺編寫從登錄到下單的大流程接口自動化用例》

②用例數(shù)據(jù)分離

開發(fā)中,敬請期待...

③HttpRunner,示例代碼如下所示:

  1. from httprunner import HttpRunner, Config, Step, RunRequest 
  2.  
  3.  
  4. class TestLoginPay(HttpRunner): 
  5.     config = ( 
  6.         Config("登錄到下單流程"
  7.             .variables( 
  8.             **{ 
  9.                 "skuNum""3" 
  10.             } 
  11.         ) 
  12.             .base_url("http://127.0.0.1:5000"
  13.     ) 
  14.  
  15.     teststeps = [ 
  16.         Step( 
  17.             RunRequest("登錄"
  18.                 .post("/login"
  19.                 .with_headers(**{"Content-Type""application/json"}) 
  20.                 .with_json({"username""dongfanger""password""123456"}) 
  21.                 .extract() 
  22.                 .with_jmespath("body.token""token"
  23.                 .validate() 
  24.                 .assert_equal("status_code", 200) 
  25.         ), 
  26.         Step( 
  27.             RunRequest("搜索商品"
  28.                 .get("searchSku?skuName=電子書"
  29.                 .with_headers(**{"token""$token"}) 
  30.                 .extract() 
  31.                 .with_jmespath("body.skuId""skuId"
  32.                 .with_jmespath("body.price""skuPrice"
  33.                 .validate() 
  34.                 .assert_equal("status_code", 200) 
  35.         ), 
  36.         Step( 
  37.             RunRequest("添加購物車"
  38.                 .post("/addCart"
  39.                 .with_headers(**{"Content-Type""application/json"
  40.                                  "token""$token"}) 
  41.                 .with_json({"skuId""$skuId""skuNum""$skuNum"}) 
  42.                 .extract() 
  43.                 .with_jmespath("body.totalPrice""totalPrice"
  44.                 .validate() 
  45.                 .assert_equal("status_code", 200) 
  46.         ), 
  47.         Step( 
  48.             RunRequest("下單"
  49.                 .post("/order"
  50.                 .with_headers(**{"Content-Type""application/json"
  51.                                  "token""$token"}) 
  52.                 .with_json({"skuId""$skuId""price""$skuPrice""skuNum""$skuNum""totalPrice""$totalPrice"}) 
  53.                 .extract() 
  54.                 .with_jmespath("body.orderId""orderId"
  55.                 .validate() 
  56.                 .assert_equal("status_code", 200) 
  57.         ), 
  58.         Step( 
  59.             RunRequest("支付"
  60.                 .post("/pay"
  61.                 .with_headers(**{"Content-Type""application/json"
  62.                                  "token""$token"}) 
  63.                 .with_json({"orderId""$orderId""payAmount""6.9"}) 
  64.                 .validate() 
  65.                 .assert_equal("status_code", 200) 
  66.                 .assert_equal("body.success""true"
  67.         ), 
  68.     ] 

用戶手冊

https://dongfanger.gitee.io/blog/chapters/tep.html

 

責(zé)任編輯:武曉燕 來源: 測試開發(fā)剛哥Python
相關(guān)推薦

2021-08-31 15:53:48

Nuxt 開箱服務(wù)

2023-11-04 12:43:44

前端圖片參數(shù)

2022-08-08 08:29:55

圖片壓縮前端互聯(lián)網(wǎng)

2023-03-08 21:25:58

開源工具庫開箱

2023-01-15 20:28:32

前端圖片壓縮

2021-09-01 17:43:32

StreamNativ開源

2017-01-09 13:21:24

達(dá)沃超融合

2013-11-01 09:37:19

Android系統(tǒng)架構(gòu)工具

2021-09-26 05:41:47

基礎(chǔ)設(shè)施連接無線技術(shù)網(wǎng)絡(luò)

2023-01-29 07:49:57

2021-09-28 09:30:18

uni-appVue 3.0uniCloud

2022-08-02 09:01:55

后臺管理模版

2022-01-05 09:58:59

鴻蒙HarmonyOS應(yīng)用

2022-05-19 15:10:03

英特爾TensorFlow

2015-06-30 09:49:19

管理平臺開源KVM

2019-11-25 00:00:00

開源技術(shù) 數(shù)據(jù)

2022-01-11 09:32:20

鴻蒙HarmonyOS應(yīng)用

2024-11-25 06:20:00

Netty封裝框架

2023-10-31 08:03:33

開源電子簽名組件
點贊
收藏

51CTO技術(shù)棧公眾號