iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 27
0
DevOps

持續測試三十天系列 第 27

[Day 27] HttpRunner 快速上手

今天的文章,我們將進行 HttpRunner 的快速上手,將會進行以下步驟

  1. 安裝 HttpRunner
  2. 使用 HttpRunner 腳手架建立專案
  3. 使用 HttpRunner 跑一個 API 測試並產出測試報告

在開始之前,若想對 HttpRunner 的項目組織結構有所了解,建議可以先閱讀 HttpRunner 名詞解釋

測試案例 (testcase)

一條測試用例(testcase)應該是為了測試某個特定的功能邏輯而精心設計的,並且至少包含如下幾點:

  1. 明確的測試目的 - 知道想測試什麼
  2. 明確的輸入 - 對 API 的 input 是什麼
  3. 明確的測試步驟描述 - 知道 API 的使用流程
  4. 明確的預期結果對 - 預期 API 的 output 是什麼

測試步驟 (teststep)

測試用例(testcase)是測試步驟的 有序 集合,而對於接口測試來說,每一個測試步驟應該就對應一個 API 的請求描述

測試案例集 (testsuite)

測試用例集(testsuite) 是 測試用例 的 無序 集合,集合中的測試用例應該都是相互獨立,不存在先後依賴關係的


安裝 HttpRunner

若已經有 python 和 pip 環境,安裝 HttpRunner

pip install httprunner

or 

pip3 install httprunner

安裝的過程
https://ithelp.ithome.com.tw/upload/images/20191013/20120775bDAahVhIHl.png

安裝後確定是否安裝成功,

hrun -V

har2case -V

https://ithelp.ithome.com.tw/upload/images/20191013/20120775Kv4oYzu2hA.png

若需要更新 HttpRunner 的版本

pip install -U HttpRunner

使用 HttpRunner 腳手架建立專案

建立項目腳手架,專案名稱是 day27

hrun --startproject day27

HttpRunner 會幫我們建立專案目錄結構

day27
├── .env         // 環境變數,可存放測試中用到的敏感訊息
├── api          // 測試用例(testcase) 對應的 API
│   └── demo_api.yml 
├── debugtalk.py // 可以用 python 定義輔助函數,在專案內使用
├── reports      // 測試報告存放的料夾
├── testcases    // 測試案例資料夾
│   └── demo_testcase.yml
└── testsuites   // 測試案例集資料夾
    └── demo_testsuite.yml

https://ithelp.ithome.com.tw/upload/images/20191013/20120775uHKzI4FbJv.png
在 HttpRunner 中,測試用例組織主要基於三個概念:

  1. 測試用例集 (testsuite),對應一個文件夾,包含單個或多個測試用例(YAML/JSON)文件
  2. 測試用例 (testcase),對應一個 YAML/JSON 文件,包含單個或多個測試步驟
  3. 測試步驟 (teststep),對應 YAML/JSON 文件中的一個 test,描述單次接口測試的全部內容,包括發起接口請求、解析響應結果、校驗結果等

腳手架產生的 demo_api.yml 內容

name: demo api
variables:
  var1: value1
  var2: value2
request:
  url: /api/path/$var1
  method: POST
  headers:
    Content-Type: "application/json"
  json:
    key: $var2
validate:
  - eq: ["status_code", 200]

使用 HttpRunner 跑一個 API 測試並產出測試報告

使用 day27 的 Code
demo_api.yml 內容修改如下,測試 google search api

name: google search
base_url: https://www.google.com
variables:
  keyword: httprunner # 宣告 keyword 變數
request:
  url: /search
  method: GET
  params:
    q: $keyword       # 使用 keyword 變數作為搜尋的參數,這邊是 httprunner
extract:
  - title: "<title>(.*)</title>" # 用正規式找到 response html 的 title
validate:
  - eq: ["status_code", 200] # 斷言 status_code 等於 200
  - startswith: ["$title", "httprunner"] # 斷言 title 以 httprunner 開始

使用 hrun Command 跑測試

cd day27

hrun api/demo_api.yml

or 

hrun api

Console 顯示測試結果,並產出 HTML 格式的測試報告
https://ithelp.ithome.com.tw/upload/images/20191014/20120775X9KAiFV2wN.png

測試報告,1 個測試案例通過,
https://ithelp.ithome.com.tw/upload/images/20191013/201207751IXEEEsRuL.png

測試報告,檢查 google search api 的 response,
確認 status_code = 200 and title startswith httprunner
https://ithelp.ithome.com.tw/upload/images/20191013/20120775DLRN7KIcDY.png


上一篇
[Day 26] HttpRunner 的世界觀 - 技術架構與接口測試應用
下一篇
[Day 28] HttpRunner 測試用例分層
系列文
持續測試三十天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言