pytest+yaml框架-6.hooks鉤子功能實(shí)現(xiàn)-創(chuàng)新互聯(lián)

前言

在發(fā)送請求的時(shí)候,我們希望在發(fā)送請求參數(shù)前,帶上簽名的值,或者返回的內(nèi)容需要二次處理,解密后返回。
此功能我們可以用 hooks 鉤子來實(shí)現(xiàn)
pip 安裝插件

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供峨山縣網(wǎng)站建設(shè)、峨山縣做網(wǎng)站、峨山縣網(wǎng)站設(shè)計(jì)、峨山縣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、峨山縣企業(yè)網(wǎng)站模板建站服務(wù),十年峨山縣做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
pip install pytest-yaml-yoyo

hooks 功能在v1.0.4版本上實(shí)現(xiàn)

response 鉤子功能

requests 庫只支持一個(gè) response 的鉤子,即在響應(yīng)返回時(shí)可以捎帶執(zhí)行我們自定義的某些方法。
可以用于打印一些信息,做一些響應(yīng)檢查或想響應(yīng)對象中添加額外的信息

# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
import requests
url = 'https://httpbin.org/get'


def response_status(resopnse, *args, **kwargs):
    print('url', resopnse.url)
    resopnse.status = 'PASS' if resopnse.status_code< 400 else 'FAIL'


res = requests.get(url, hooks={'response': response_status})
print(res.status)

以上是基于requests 庫的鉤子功能實(shí)現(xiàn)的基本方式

yaml 用例中添加response 鉤子

在yaml 文件中添加response 鉤子功能,跟上面代碼方式差不多, 有2種方式

  • 1.寫到config 全局配置,每個(gè)請求都會(huì)帶上hooks
  • 2.寫到單個(gè)請求的request 下,僅單個(gè)請求會(huì)帶上hooks功能

先看單個(gè)請求的response 鉤子
test_hook1.yml

# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
config:
  name: post示例
teststeps:
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: test
      password: "123456"
    hooks:
      response: ['hook_response']
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$.code, 0]
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: test
      password: "123456"
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$.code, 0]

在conftest.py 中注冊鉤子函數(shù)

# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/

def hook_response(response, *args, **kwargs):
    # print(response.text) 原始數(shù)據(jù)
    print("執(zhí)行response hook 函數(shù)內(nèi)容....")
    class NewResponse:
        text = '{"code": 0, "data": {"token": "yo yo"}}'  # response.text解密
        history = response.history
        headers = response.headers
        cookies = response.cookies
        status_code = response.status_code
        raw = response.raw
        is_redirect = response.is_redirect
        content = b'{"code": 0, "data": {"token": "yo yo"}}'  # response.text解密
        elapsed = response.elapsed

        @staticmethod
        def json():
            # 拿到原始的response.json() 后解碼
            return {"code": 0, "data": {"token": "yo yo"}}

    return NewResponse

my_builtins.hook_response = hook_response

由于上面用例只在第一個(gè)請求中使用了hooks功能,所以第二個(gè)請求的斷言- eq: [$.code, 0]會(huì)失敗

鉤子方法調(diào)用語法

  • 1.層級是在request 下
  • 2.hooks 關(guān)鍵字對應(yīng)的是一個(gè)字典 {“response”: []}
  • 3.response 的值可以是單個(gè)函數(shù)名稱,也可以是多個(gè)func1, func2,或者是一個(gè)list類型[func1, func2]
  • 4.response 的值必須是一個(gè)可以調(diào)用的函數(shù),此函數(shù)需在conftest 中注冊綁定到my_builtins模塊
  • 5.調(diào)用的函數(shù)第一個(gè)參數(shù)是response, 可以重寫response內(nèi)容(如需要對返回結(jié)果解密),也可以不用重寫
request:
    method: POST
    url: http://httpbin.org/post
    hooks:
      response: ['hook_response']
config 全局使用

在config 中配置全局hooks功能,格式如下

config:
  name: post示例
  hooks:
    response: ['hook_response']

test_hook2.yml完整示例

config:
  name: post示例
  hooks:
    response: ['hook_response']
teststeps:
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: test
      password: "123456"
    hooks:
      response: ['hook_response']
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$.code, 0]
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: test
      password: "123456"
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$.code, 0]

全局配置hooks, 那么該用例下所有的請求都會(huì)帶上hooks

請求預(yù)處理鉤子

如果需要對請求參數(shù)預(yù)處理,我們還新增了一個(gè)request 請求鉤子,可以獲取到發(fā)送請求時(shí)的request參數(shù)

在conftest.py

# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/


def func1(req):
    print(f'請求預(yù)處理:{req}')
    
    
def func2():
    print(f'請求預(yù)處理-----------')
    
    
my_builtins.func1 = func1
my_builtins.func2 = func2

在 yaml 文件中使用示例

config:
  name: post示例
teststeps:
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: test
      password: "123456"
    hooks:
      request: ['func1', 'func2']
      response: ['hook_response']
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$.code, 0]
-
  name: post
  request:
    method: POST
    url: http://httpbin.org/post
    json:
      username: test
      password: "123456"
    hooks:
      response: ['hook_response']
  extract:
      url:  body.url
  validate:
    - eq: [status_code, 200]
    - eq: [headers.Server, gunicorn/19.9.0]
    - eq: [$.code, 0]

在config 中設(shè)置全局hooks示例

config:
  name: post示例
  hooks:
    request: ['func1', 'func2']
    response: ['hook_response']

由于request 變量是pytest的一個(gè)內(nèi)置fixture,此變量保留著,獲取請求參數(shù)的函數(shù)使用req代替。
利用request hook功能可以實(shí)現(xiàn)請求參數(shù)的預(yù)處理,比如請求body簽名和加密處理等需求。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

新聞標(biāo)題:pytest+yaml框架-6.hooks鉤子功能實(shí)現(xiàn)-創(chuàng)新互聯(lián)
文章來源:http://muchs.cn/article16/dshsdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、全網(wǎng)營銷推廣網(wǎng)站排名、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司