HttpRunner框架學習--參數化,結果驗證

1.參數化

自動化測試中,肯定涉及到用例測試步驟都基本一致,只是輸入參數不一樣的用例。這種情況就可以使用參數化方法來解決,避免重複編碼。

HttpRunner中參數化使用的是parameters,要區分它跟variables的區別,variables是一個變量,爲了不重複定義,可定義一次,下邊使用$變量名 的方式使用。parameters是用例所需的輸入參數,可以分爲多組不同數據。

參數爲username,uid,password,下邊是兩組參數。

    parameters:               
      - username-uid-password:  #兩組參數
          - [test1,1,123]
          - [test2,2,125]
- config:
    name: testcase description
    request:
      base_url: http://127.0.0.1:5000
    parameters:               
      - username-uid-password:  #兩組參數
          - [test1,1,123]
          - [test2,2,125]
#    variables:
#        username: user1
#        password: 123456

- test:
    name: /api/get-token
    request:
      headers:
        Content-Type: application/json
        User-Agent: python-requests/2.18.4
        app_version: 2.8.6
        device_sn: FwgRiO7CNA50DSU
        os_platform: ios
      json:
        sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
      method: POST
      url: /api/get-token
    validate:
      - eq: [status_code, 200]
      - eq: [headers.Content-Type, application/json]
      - eq: [content.success, true]
    #        - eq: [content.token, baNLX1zhFYP11Seb]
    extract:
      - token: content.token

- test:
    name: /api/users/$uid
    request:
      headers:
        Content-Type: application/json
        User-Agent: python-requests/2.18.4
        device_sn: FwgRiO7CNA50DSU
        token: $token
      json:
        name: $username
        password: $password
      method: POST
      url: /api/users/$uid
    validate:
      - eq: [status_code, 201]
      - eq: [headers.Content-Type, application/json]
      - eq: [content.success, true]
      - eq: [content.msg, user created successfully.]

如下形式的參數共有4*4=16種組合,所以會進行用例會執行16次

parameters:
        - user: ["test1", "test2", "test3", "test4"]
        - psw: ["123456", "123456", "123456", "123456"]

如果參數比較多,還可以使用csv格式進行保存,然後在yml或json文件中引用csv文件進行運行,通過內置的 parameterize(可簡寫爲 P)函數引用 CSV 文件

uid.csv

uid
1001
1002
1999
2000
- config:
    name: testcase description
    request:
      base_url: http://127.0.0.1:5000
    parameters:
      - uid: ${parameterize(uid.csv)}

    variables:
        username: user1
        password: 123456

- test:
    name: /api/get-token
    request:
      headers:
        Content-Type: application/json
        User-Agent: python-requests/2.18.4
        app_version: 2.8.6
        device_sn: FwgRiO7CNA50DSU
        os_platform: ios
      json:
        sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
      method: POST
      url: /api/get-token
    validate:
      - eq: [status_code, 200]
      - eq: [headers.Content-Type, application/json]
      - eq: [content.success, true]
    #        - eq: [content.token, baNLX1zhFYP11Seb]
    extract:
      - token: content.token

- test:
    name: /api/users/$uid
    request:
      headers:
        Content-Type: application/json
        User-Agent: python-requests/2.18.4
        device_sn: FwgRiO7CNA50DSU
        token: $token
      json:
        name: $username
        password: $password
      method: POST
      url: /api/users/$uid
    validate:
      - eq: [status_code, 201]
      - eq: [headers.Content-Type, application/json]
      - eq: [content.success, true]
      - eq: [content.msg, user created successfully.]

 

2.結果驗證

結果驗證相當於其他框架斷言,httprunner使用validate關鍵字

validate

支持兩種格式:

  • {"comparator_name": [check_item, expect_value]}比較符號:檢測項,期望值
  • {"check": check_item, "comparator": comparator_name, "expect": expect_value}
  • eq equals,判斷實際結果和期望結果是否相等,可以用"eq", "equals", "==", "is"
  • lt less_than, 判斷實際結果小於期望結果 ,可以用 "lt", "less_than"
  • le less_than_or_equals,判斷實際結果小於等於期望結果 ,可以用 "le", "less_than_or_equals"
  • gt greater_than,判斷實際結果大於期望結果,可以用"gt", "greater_than"
  • ge greater_than_or_equals,判斷實際結果大於等於期望結果,可以用"ge", "greater_than_or_equals"
  • ne not_equals, 判斷實際結果和期望結果不相等,可以用"ne", "not_equals"
  • str_eq string_equals 判斷轉字符串後對比 實際結果和期望結果是否相等,可以用"str_eq", "string_equals"
  • len_eq length_equals 判斷字符串或list長度,可以用"len_eq", "length_equals", "count_eq"
  • len_gt length_greater_than 判斷實際結果的長度大於和期望結果,可以用"len_gt", "count_gt", "length_greater_than", "count_greater_than"
  • len_ge length_greater_than_or_equals 實際結果的長度大於等於期望結果,可以用"len_ge", "count_ge", "length_greater_than_or_equals", "count_greater_than_or_equals"
  • len_lt length_less_than 實際結果的長度小於期望結果,可以用"len_lt", "count_lt", "length_less_than", "count_less_than"
  • len_le length_less_than_or_equals 實際結果的長度小於等於期望結果,可以用"len_le", "count_le", "length_less_than_or_equals", "count_less_than_or_equals

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章