httprunner做接口自動化和性能測試


 

【簡介】

HttpRunner 是一款面向 HTTP(S) 協議的通用測試框架,只需編寫維護一份YAML/JSON腳本,即可實現自動化測試、性能測試、線上監控、持續集成等多種測試需求;

 

【優缺點】

優點:基於YAML/JSON格式,專注於接口本身的編寫,   寫法簡單,可生成測試報告, 支持抓包錄制, 支持python擴展、可做接口自動化, 方便搭建接口框架;

缺點:屬領域特定語言,官方文檔不直白詳細,沒有編輯器插件對語法校驗,編寫易出錯;

【結論】

可以考慮用來做接口測試,但是語法校驗需要單獨處理。

 

httprunnerManage地址:  https://github.com/HttpRunner/HttpRunnerManager

mockserver地址:https://github.com/yinquanwang/MockServer

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

【使用教程】:

1、安裝

pip install httprunner

注:我安裝的是python3.6     httprunner  1.5.8 

驗證:hrun -V

 

2、運行命令

運行測試用例的命令為hrun

運行全部用例:hrun testcase

運行單個用例:hrun  c:/xx/xx/xx/xx.json

運行多個用例:hrun  c:/xx/xx/xx/xx.json c:/xx/xx/xx/xx.json 

運行失敗后停止執行:hrun testcase --failfast

重復運行某個case: 在test下添加times參數,對應的times值為需要重復運行的次數

跳過某個case運行:研用unittest框架中的skip(無條件跳過)、skipIf(條件為真的時候跳過)、skipUnless(條件為假的時候跳過),關鍵字加在test中

 

3、生成腳手架

hrun --startproject test_mysite

創建一個名為test_mysite的目錄,目錄下結構如圖:

 

 -------------------------------------------

說明:                                          

api目錄下為接口的詳細信息        

reports目錄下為生成的報告         

testcases目錄下為測試用例        

testsuites為測試套件     

新建debugtalk.py文件用於編寫函數輔助測試            

--------------------------------------------

4、結構說明

5、參數關聯

 

6、變量的聲明和引用

【在config中定義的variables為全局變量,在test中定義的variables為局部變量,僅供當前test引用,test會繼承config中的配置】

 

7、抽取公共變量到config [全局變量]

 

8、實現動態邏輯運算

操作步驟:在測試用例文件的同級或父級創建一個【debugtalk.py】文件,然后再其中定義相關的函數和變量;

如:定義一個生成隨機數的函數:

import random
import string
 
SECRET_KEY = "DebugTalk"
 
def get_random_string(str_len):
    random_char_list = []
    for _ in range(str_len):
        random_char = random.choice(string.ascii_letters + string.digits)
        random_char_list.append(random_char)
 
    random_string = ''.join(random_char_list)
    return random_string

 

調用:

"variables": [
  {"device_sn": "${get_random_string(12)}"}
]

9、斷言

【validate斷言檢查,eq是equals的縮寫】

  

10、httprunner常用關鍵字列舉

【config】

【test】

 

【request】

11、hook函數編寫

【hook 函數的定義放置在項目的 debugtalk.py 中,在 YAML/JSON 中調用 hook 函數仍然是采用 ${func($a, $b)} 的形式。】

  hook函數分為:setup_hooks和teardown_hooks 

  hook機制分為兩個層級: testcase的config和test

  config中的setup_hooks和teardown_hooks 類似於unittest中的setUpClass和tearDownClass

  test中的setup_hooks和teardown_hooks 針對單個case生效

12、parameters參數化(httprunner版本為1.5.8,參數化支持在testcases中進行)

注:也可以使用.csv文件進行數據驅動。

 一對一的參數化demo:

 1 - config:
 2     name: "接口測試test"
 3     request:
 4       base_url: http://xx.xx.xx
 5     parameters: 
 6       - username-password-message-code: 
 7         - ["test1", "123456", "OK", 1]
 8         - ["test2", "test", "用戶名或密碼錯誤", 2]
 9         - ["test3", "123456", "OK", 1]
10 
11 - test:
12     name: login case
13     request:
14         url: /login
15         method: POST
16         headers:
17             Content-Type: application/json
18         json:
19             username: $username
20             password: $password
21 
22     validate:
23         - eq: [status_code, 200]
24         - eq: [json.code, $code]
25         - eq: [json.message, $message]
View Code

 多對一的參數化demo:

 1 - config:
 2     name: "接口測試test"
 3     request:
 4       base_url: http://xx.xx.xx
 5     parameters: 
 6       - username: ["test1", "test2", "test3"]
 7       - password: ["123456"]
 8 
 9 - test:
10     name: login case
11     request:
12         url: /login
13         method: POST
14         headers:
15             Content-Type: application/json
16         json:
17             username: $username
18             password: $password
19 
20     validate:
21         - eq: [status_code, 200]
View Code

 多對多的參數化demo:

[使用笛卡爾積組合:共2x3=6種組合]

 1 - config:
 2     name: "接口測試test"
 3     request:
 4       base_url: http://xx.xx.xx.xx
 5     parameters: 
 6       - username: ["test1", "test2", "test3"]
 7       - password: ["123456","111111"]
 8 
 9 - test:
10     name: login case
11     request:
12         url: /login
13         method: POST
14         headers:
15             Content-Type: application/json
16         json:
17             username: $username
18             password: $password
19     validate:
20         - eq: [status_code, 200]
View Code

 使用csv文件參數化demo:

login.csv:

 testcases:

 1 - config:
 2     name: "接口測試test"
 3     request:
 4       base_url: http://xx.xx.xx.xx
 5     parameters: 
 6       - username-password: ${P(login.csv)}
 7       
 8 - test:
 9     name: login case
10     request:
11         url: /login
12         method: POST
13         headers:
14             Content-Type: application/json
15         json:
16             username: $username
17             password: $password
18 
19     validate:
20         - eq: [status_code, 200]
21         - eq: [json.code, 1]
22         - eq: [json.message, 'OK']
View Code

 運行結果:

 

 報告截圖:

(報告的模板可以替換,位置在\python\Lib\site-packages\httprunner\templates下,但是2.0.0中只有一個默認的模板,1.5.8中有兩個模板,可以拷貝一個,但是運行會報錯)

默認模板:

 替換模板:(hrun testcases/test.yaml --html-report-template D:/Python/Python36/Lib/site-packages/httprunner/templates/extent_report_template.htm

 

13、接口自動化demo(httprunner版本為2.0.0,支持分層的思想,並且參數化需要放在suite中進行)

【如果暫時很難理解分層思想,可以不用過多糾結,寫的case多了會自然考慮到這些因素,屆時再去找相應的文檔學習理解】

【api】

  [test0105.yaml]

 1 name: test0105
 2 variables:
 3     - username: name2
 4     - password: pwd2
 5 request:
 6     method: POST
 7     url: http://xx.xx.xx
 8     headers:
 9         Content-Type: application/json
10     json:
11         username: $username
12         password: $password
13 validate:
14     -   eq:
15         -   status_code
16         -   200
View Code

 

[test0106.yaml]

 1 name: test0106
 2 variables:
 3     - username: name1
 4     - password: pwd1
 5 request:
 6     method: POST
 7     url: http://xx.xx.xx
 8     headers:
 9         Content-Type: application/json
10     json:
11         username: $username
12         password: $password
13 validate:
14     -   eq:
15         -   status_code
16         -   200
View Code

 

【testcases】

[test0105.yaml]

 

1 - config:
2     name: "testcase05"
3 
4 - test:
5     name: testcase1
6     api: api/test0105.yaml
View Code

 

[test0106.yaml]

1 - config:
2     name: "testcase06"
3 
4 - test:
5     name: testcase2
6     api: api/test0106.yaml
View Code

 

【testsuite】

 1 config:
 2     name: "單接口測試testsuites"
 3 testcases:
 4     create1:
 5         testcase: testcases/test0105.yaml
 6         parameters:
 7          - username-password: ${P(data/login.csv)}
 8 
 9 
10     create2:
11         testcase: testcases/test0106.yaml
12         parameters:
13             - username-password: ${P(data/login.csv)}
View Code

 

【data】

【運行結果】

 【報告】

 

 運行:在test_mysite路徑下,hrun d:\xx\xx\testsuites\xxxx.yaml

 報告:在當前路徑下運行,就去這個路徑下找reports文件夾,若是沒有,則新建一個reports文件夾,生成報告;

 

11、httprunner性能測試

 原理: 使用的還是locust,但是locusts -f 和 locust -f的區別是前者可以運行.yaml或.json文件,並且轉換為python文件,再按照locust的方式運行;

 【步驟:】 

   1) 安裝locustio

     pip install locustio

   2)   在項目路徑下執行

                 locusts -f xxxx/xxxxxx.yaml     或者是   locusts -f xxxx/xxxxxx.json

   3)  訪問   http://localhost:8089/,設置參數,點擊start

      

   4)  查看結果:

    

 

              

 

   5) 分析測試結果。

    

 

*** 有興趣可加入測試交流群一起學習:QQ群號:744419090 ***

參考資料:

1、https://cn.httprunner.org/

2、https://www.cnblogs.com/snailgirl/p/10309519.html

3、https://www.cnblogs.com/yoyoketang/p/11575646.html

 

若有疑問,歡迎討論指正;


免責聲明!

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



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