httprunner變量、參數關聯與參數化


httprunner是基於requests的,所以處理思路與python中處理一樣

 

目錄

1、變量

2、httprunner參數關聯

3、httprunner參數化

 

1、變量

比如說有一種場景,接口a需要傳一個參數m值才能返回接口,接口b需要傳參數m的值以及a的返回值才能獲取結果,這時候可以把參數的值作為一個全局變量,然后傳值

這里用到關鍵字variables

- config:
    name: my_test
    base_url: http://localhost:8090
    variables:
             m: test
- test:
    name: a
    request:
        url: /a
        method: post
        headers:
            Content-Type: application/json
        json:
            m: $m
    extract:
        - q: content.q         
    validate:
        - eq: [status_code, 200]
- test:
    name : b
    request :
        url : /b
        method : POST
        headers:
            Content-Type: application/json
            token: $token
        json:
            m: $m
            q: $q
    validate :
        - eq : [status_code,200]- config:
    name: my_test
    base_url: http://localhost:8090
    variables:
             m: test
- test:
    name: a
    request:
        url: /a
        method: post
        headers:
            Content-Type: application/json
        json:
            m: $m
    extract:
        - q: content.q         
    validate:
        - eq: [status_code, 200]
- test:
    name : b
    request :
        url : /b
        method : POST
        headers:
            Content-Type: application/json
            token: $token
        json:
            m: $m
            q: $q
    validate :
        - eq : [status_code,200]

 

2、httprunner參數關聯

2個接口直接有關聯,比如新增的接口需要登錄信息才能請求成功,這時候就需要把登錄信息傳給登錄接口,可以是cookies或者token

這里用到關鍵字extract關鍵字完成提取,這里以某平台的登錄token關聯為例:

mytest.yaml

- config:
    name: my_test
    base_url: http://localhost:8090
- test:
    name: test_demo case1
    request:
        url: /admin/login
        method: post
        headers:
            Content-Type: application/json
        json:
            username: admin
            password: 123456
    extract:
        - token: content.token         # 提取token
    validate:
        - eq: [status_code, 200]

- test:
    name : add
    request :
        url : /admin/add
        method : POST
        headers:
            Content-Type: application/json
token: $token
        json:
            m: $m
validate : - eq : [status_code,200]

  

 

3、httprunner參數化

在case中需要添加多種場景的參數取值組合,來檢驗case,以某平台登錄為例子,以下記錄以csv來傳參,用到關鍵字parameters,傳csv 使用parameterize(可簡寫為P)

myuser.csv

username,password
name1,123456
name2,1234567

mytest.yaml

- config:
    name: my_test
    base_url: http://localhost:8090
    parameters:
      - username-passwd:${P(myuser.csv)}   #1、參數名稱與文件第一行成對應關系 2、從第2行開始取值
- test:
    name: test_demo case1
    request:
        url: /admin/login
        method: post
        headers:
            Content-Type: application/json
        json:
            username: $user
            password: $password
    validate:
        - eq: [status_code, 200]

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM