主要包含三種測試:javascript
實際上意義就是UI界面到數據庫之間,數據流通過的全部過程。php
LAMP(Linux Apache MySQL PHP)/LNMP(Linux Nginx MySQL PHP):只有 Web 服務器,沒有應用服務器。html
Linux / Windows + Java / Asp.net(C#) + Apache/Nginx + Tomcat/IIS + MySQL/Oracle/SQL server前端
咱們須要關注的是 Web 接口測試。java
Web 接口測試的方法:python
Postman 本來是一個 Chrome 瀏覽器的插件,如今已經提供了 Windows、MacOS 和Linux 的獨立安裝版本。接下來用Windows的版原本進行安裝和使用。web
下載 Postman 的安裝包,分爲 32位 和 64位。https://www.getpostman.comshell
安裝 Postman數據庫
註冊用戶(Sign Up) 並登陸 (Sign In)編程
能夠在多臺電腦登陸,作過的測試會自動同步。
安裝完而且登陸之後能夠開始測試。
Web 接口經過 HTTP(S)請求,是一個URL,URL請求結果,會獲得數據,數據的格式主要有兩種,一種JSON,一種 XML。主要用JSON演示。
接下來用一個例子,來分別JSON和XML表示如下的表格數據。
employee_id | first_name | last_name | phone_number | hire_date | job_id | salary | |
---|---|---|---|---|---|---|---|
100 | Steven | King | SKING | 515.123.4567 | 6/17/1987 | AD_PRES | 24000 |
101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 9/21/1989 | AD_VP | 17000 |
首先用 JSON的形式,JSON是「鍵值對」(Key Value)的形式
{
"employees": [{ "employee_id": 100, "first_name": "Steven", "last_name": "King", "email": "SKING", "phone_number": "515.123.4567", "hire_date": "6/17/1987", "job_id": "AD_PRES", "salary": 24000 }, { "employee_id": 101, "first_name": "Neena", "last_name": "Kochhar", "email": "NKOCHHAR", "phone_number": "515.123.4568", "hire_date": "9/21/1989", "job_id": "AD_VP", "salary": 17000 }] }
對應的 XML 格式:
<?xml version="1.0" encoding="UTF-8" ?> <employees> <employee> <employee_id>100</employee_id> <first_name>Steven</first_name> <last_name>King</last_name> <email>SKING</email> <phone_number>515.123.4567</phone_number> <hire_date>6/17/1987</hire_date> <job_id>AD_PRES</job_id> <salary>24000</salary> </employee> <employee> <employee_id>101</employee_id> <first_name>Neena</first_name> <last_name>Kochhar</last_name> <email>NKOCHHAR</email> <phone_number>515.123.4568</phone_number> <hire_date>9/21/1989</hire_date> <job_id>AD_VP</job_id> <salary>17000</salary> </employee> </employees>
Web 接口的定義來決定測試內容
接口的實質對象:數據
測試 心知天氣 API:https://www.seniverse.com/
登陸 心知天氣 (先註冊)
讀 接口的 API 文檔
以 獲取實時天氣做爲示例:
API的方法和URL:GET, https://api.seniverse.com/v3/weather/now.json
API的參數:
參數名 | 參數類型 | 參數意義 | 是否必選 |
---|---|---|---|
key | string | 你的API密鑰 | true |
location | string | 查詢的地理位置 | true |
language | string | 結果表示的語言 | false,默認簡體中文 |
unit | string | 結果表示的單位(華氏度,攝氏度) | false,默認攝氏度 |
參數名 | 參數類型 | 參數意義 |
---|---|---|
location | 對象:包括id, name, country, time_zone, time_zone_offset | |
now | 對象:包括 text,code, temperature, feel_like… | |
last_update | 日期 |
用Postman開始測試
new tab 中輸入 方式,和URL
點擊 Params 設置參數
點擊 send 開始發送請求
查看請求結果是否有輸出,格式是否和上述的響應參數對應
{
"results": [ { "location": { "id": "WS10730EM8EV", "name": "Shenzhen", "country": "CN", "path": "Shenzhen,Shenzhen,Guangdong,China", "timezone": "Asia/Shanghai", "timezone_offset": "+08:00" }, "now": { "text": "Cloudy", "code": "4", "temperature": "25" }, "last_update": "2017-05-09T10:05:00+08:00" } ] }
編輯 Tests 頁面,添加斷言。
var jsonData = JSON.parse(responseBody); tests["檢查城市名稱"] = jsonData.results[0].location.name === "Shenzhen";
使用Postman 登陸然之系統,測試登陸接口
使用然之 4.2 或者以上版本
修改然之系統 配置文件 C:\xampp5\htdocs\ranzhi\config\my.php
,在最後添加一行配置,以下圖:
$config->notEncryptedPwd = true;
重啓 Apache
打開 Chrome,輸入然之的網址,打開登陸頁面
打開 Fiddler,並設置 Chrome 進行抓包
在 Chrome 中進行登陸,提交用戶名和密碼
在 Fiddler 中捕獲剛剛的登陸 POST 請求
打開 Postman,新建 URL,POST方法。
須要設置 Postman,點擊 File | Settings,進行設置
在 Postman 輸入剛剛 Fiddler 請求的 POST 的 URL
輸入上述 URL
在 Fiddler 中複製請求的第二行到空行之間的 消息報頭(Head),Postman 輸入 Head
在 Fiddler 中複製請求的正文,在 Postman 中 body 選擇 raw,並輸入
在 Postman 的test 中輸入如下測試內容:
var jsonData = JSON.parse(responseBody); tests["檢查locate"] = jsonData.locate === "\/sys\/index.html"; tests["檢查result"] = jsonData.result === "success"; tests["Status code is 200"] = responseCode.code === 200;
在 Chrome 中退出登陸,注意此步驟很重要,退出之後纔可以進行 Postman 登陸
在 Postman 中點擊 Send,進行測試
查看 Postman 的運行結果。
Web 接口測試的準備
在上面四個的基礎上,注意Web 接口須要認證,尤爲是支付業務,這裏用一個例子,而且是POST請求 + Basic Auth 認證,來闡述此部分。
示例的步驟:
打開 https://www.pingxx.com/ Ping++ 在線支付網站,註冊一個帳號,並登陸,會自動建立一個應用。
進入自動建立的應用,進入應用的控制面板界面。
獲取 APP[id]
,按如圖的方式獲取到,並等下使用
獲取 Test Secret Key
,按照如圖的方式獲取到,並依舊等下使用
按照下操做,查閱 API 文檔,並開始測試。
選擇 Charge,建立一個 支付訂單,查閱 API 文檔的請求參數,響應參數,依舊 方法和URL
在Postman 中建立新的測試,輸入 POST 和 URL
設置參數,其中須要用到 APP[id],爲以前步驟獲取到的。
在 請求中使用 認證,Basic Auth,輸入 以前步驟獲取到的 Test Secret Key,做爲用戶名。
添加測試斷言。
用 JavaScript腳本,查詢 JSON 對象的值,並作檢查。點擊右側的現成菜單,會自動生成檢查 JSON 的框架。
var jsonData = JSON.parse(responseBody); tests["檢查object值"] = jsonData.object === "charge"; tests["檢查order_no值"] = jsonData.order_no === "99887766554433221100"; tests["檢查amount值"] = jsonData.amount === 9900;
點擊 Send 開始測試。
微信紅包支付接口測試的接口描述
在 Postman 中輸入 POST 方法和 URL
在 Postman 中 使用 Http Basic Auth 認證。輸入以前獲取的 Secret_test_key。根據 API文檔進行的操做
在 Postman 中操做
輸入 請求的參數,注意 POST 請求的請求參數,輸入到 Body 中
在 Test 設置斷言,剛纔說要設置 Object 的驗證。
輸入如下內容:
var jsonData = JSON.parse(responseBody); tests["檢查 Object 屬性"] = jsonData.object === "red_envelope"; tests["檢查 金額 屬性"] = jsonData.amount === 6000;
點擊Send,檢查。
到管理平臺查看訂單結果。
Postman 支持導出用例以及帳戶的同步功能。
接口實例:
天氣預報接口:
接口URL: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
網絡服務描述語言: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
該接口有5個方法:
經過輸入參數,能夠調用這些接口,並獲得請求的數據。
查詢本天氣預報Web Services支持的國內外城市或地區信息:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName=廣東
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName=北京
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName=上海
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName=湖北
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName=河南
查詢得到本天氣預報Web Services支持的洲、國內外省份和城市信息
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportDataSet
查詢得到本天氣預報Web Services支持的洲、國內外省份和城市信息
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince
查詢得到根據城市或地區名稱查詢得到將來三天內天氣狀況、如今的天氣實況、天氣和生活指數
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=深圳
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=59493
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=北京
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=54511
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=上海
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=58367
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=香港
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=45005
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=Chicago
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=72530
Postman 做爲工具,有UI的工具能夠完整的操做 Web API(Application Programming Interface),有些場景,還須要在自動化測試方案中操做 Web API。對於 Python 來講有兩種主流的工具操做 HTTP Web API。
和 Selenium 同樣,requests 也是第三方的工具庫。類比 Selenium 操做瀏覽器,requests 操做 HTTP 請求。
部署 requests 的過程
pip install requests
requests 有兩個常見的方法
上面兩個方法都會返回一個 Response 類的對象。注意 Response 類 屬於 requests。
做爲自動化接口測試工具,相似於 Selenium,咱們須要 用 Page-Object 方式進行設計
GET /weather/now.json
獲取指定城市的天氣實況。付費用戶可獲取所有數據,免費用戶只返回天氣現象文字、代碼和睦溫3項數據。注:中國城市暫不支持雲量和露點溫度。
請求地址示例
參數
key
你的API密鑰
location
所查詢的位置參數值範圍:城市ID
例如:location=WX4FBXXFKE4F城市中文名
例如:location=北京省市名稱組合
例如:location=遼寧朝陽、location=北京朝陽城市拼音/英文名
例如:location=beijing(如拼音相同城市,可在以前加省份和空格,例:shanxi yulin)經緯度
例如:location=39.93:116.40**(緯度前經度在後,冒號分隔)**IP地址
例如:location=220.181.111.86(某些IP地址可能沒法定位到城市)「ip」兩個字母
自動識別請求IP地址,例如:location=ip
language
語言 (可選)參數值範圍:點此查看
unit
單位 (可選)參數值範圍:c
當參數爲c時,溫度c、風速km/h、能見度km、氣壓mbf
當參數爲f時,溫度f、風速mph、能見度mile、氣壓inch默認值:c
返回結果 200
{
"results": [{ "location": { "id": "C23NB62W20TF", "name": "西雅圖", "country": "US", "timezone": "America/Los_Angeles", "timezone_offset": "-07:00" }, "now": { "text": "多雲", //天氣現象文字 "code": "4", //天氣現象代碼 "temperature": "14", //溫度,單位爲c攝氏度或f華氏度 "feels_like": "14", //體感溫度,單位爲c攝氏度或f華氏度 "pressure": "1018", //氣壓,單位爲mb百帕或in英寸 "humidity": "76", //相對溼度,0~100,單位爲百分比 "visibility": "16.09", //能見度,單位爲km千米或mi英里 "wind_direction": "西北", //風向文字 "wind_direction_degree": "340", //風向角度,範圍0~360,0爲正北,90爲正東,180爲正南,270爲正西 "wind_speed": "8.05", //風速,單位爲km/h千米每小時或mph英里每小時 "wind_scale": "2", //風力等級,請參考:http://baike.baidu.com/view/465076.htm "clouds": "90", //雲量,範圍0~100,天空被雲覆蓋的百分比 #目前不支持中國城市# "dew_point": "-12" //露點溫度,請參考:http://baike.baidu.com/view/118348.htm #目前不支持中國城市# }, "last_update": "2015-09-25T22:45:00-07:00" //數據更新時間(該城市的本地時間) }] }
具體的步驟:
封裝 requests 到 box_requests.py
import requests from model.http_response import HttpResponse class BoxRequests: def get(self, url, param): """ 用 Get 的方式請求 URL :param url: :param param: 參數 :return: model 中的 HttpResponse 類的對象 """ r = requests.get(url, param) http_response = HttpResponse() http_response.status_code = r.status_code http_response.json_dict = r.json() return http_response
HttpResponse
class HttpResponse: status_code = None json_dict = None
建立一個 page :senivers_realtime_weather_page.py
from base.box_requests import BoxRequests class SeniversRealtimeWeatherPage: base_url = "/weather/now.json" def get_weather_by_ip(self, host, api_key): """ 根據IP地址所在的地方查詢天氣 :param host: :param api_key: :return: 自定義的 HttpResponse 類的對象 """ br = BoxRequests() # 建立一個字典,用來傳參數 param = { "key": api_key, "location": "ip" } # 用 host 和 全局 base_url 拼成 URL http_response = br.get(host + self.base_url, param) return http_response
建立一個 case :seniverse_api_tests.py
import unittest from pages.api.senivers_realtime_weather_page import SeniversRealtimeWeatherPage
class SeniverseApiTests(unittest.TestCase):
api_key = None
page = None
host = None
def setUp(self):
self.api_key = 「MJX11XSAPG」
self.host = 「https://api.seniverse.com/v3」
self.page = SeniversRealtimeWeatherPage()
def test_query_realtime_weather_by_ip(self):
r = self.page.get_weather_by_ip(self.host, self.api_key)
self.assertEqual(200, r.status_code, 「狀態碼返回不正確!」)
expected_city = 「深圳」
actual_city = r.json_dict[「results」][0][「location」][「name」]
self.assertEqual(expected_city, actual_city, 「城市名稱與ip不相符合」)
if name == 「main「:
unittest.main()
「`
示例以下:
請求:POST https://api.pingxx.com/v1/charges
發起一次支付請求時須要建立一個新的 charge
對象,獲取一個可用的支付憑據用於客戶端向第三方渠道發起支付請求。若是使用測試模式的 API Key,則不會發生真實交易。當支付成功後,Ping++ 會發送 Webhooks 通知。
請求參數 | |
---|---|
order_no**REQUIRED** | 商戶訂單號,適配每一個渠道對此參數的要求,必須在商戶系統內惟一。( alipay : 1-64 位, wx : 2-32 位, bfb : 1-20 位, upacp : 8-40 位, yeepay_wap :1-50 位, jdpay_wap :1-30 位, qpay :1-30 位, cmb_wallet :10 位純數字字符串。注:除 cmb_wallet 外的其餘渠道推薦使用 8-20 位,要求數字或字母,不容許特殊字符)。 |
app [ id ]EXPANDABLE****REQUIRED | 支付使用的 app 對象的 id , expandable 可展開,查看 如何獲取App ID 。 |
channel**REQUIRED** | 支付使用的第三方支付渠道。參考 支付渠道屬性值 。 |
amount**REQUIRED** | 訂單總金額(必須大於0),單位爲對應幣種的最小貨幣單位,人民幣爲分。如訂單總金額爲 1 元, amount 爲 100,麼麼貸商戶請查看申請的借貸金額範圍。 |
client_ip**REQUIRED** | 發起支付請求客戶端的 IPv4 地址,如: 127.0.0.1。 |
currency**REQUIRED** | 三位 ISO 貨幣代碼,目前僅支持人民幣 cny 。 |
subject**REQUIRED** | 商品的標題,該參數最長爲 32 個 Unicode 字符,銀聯全渠道( upacp / upacp_wap )限制在 32 個字節。 |
body**REQUIRED** | 商品的描述信息,該參數最長爲 128 個 Unicode 字符,yeepay_wap 對於該參數長度限制爲 100 個 Unicode 字符。 |
extra*optional* | 特定渠道發起交易時須要的額外參數,以及部分渠道支付成功返回的額外參數,詳細參考 支付渠道 extra 參數說明 。 |
time_expire*optional* | 訂單失效時間,用 Unix 時間戳表示。時間範圍在訂單建立後的 1 分鐘到 15 天,默認爲 1 天,建立時間以 Ping++ 服務器時間爲準。 微信對該參數的有效值限制爲 2 小時內;銀聯對該參數的有效值限制爲 1 小時內。 |
metadata*optional* | 參考 元數據 。 |
description*optional* | 訂單附加說明,最多 255 個 Unicode 字符。 |
返回
返回一個支付憑據 charge
對象。鑑於支付渠道對 order_no
的合法性要求,爲了保證支付請求的正確處理,請務必保證對於同一支付渠道下,不一樣支付產品間 order_no
的惟一性。例如:已在微信公衆號下使用的 order_no
則沒法在微信支付以及微信公衆號掃碼下重複使用,該規則一樣適用於其餘同類渠道。若是發生錯誤,則會返回錯誤碼和錯誤詳情,詳見 錯誤。
根據 Postman 執行這個測試的狀況。選擇如下的數據:
data:params
把上述的兩個數據,變成Python 的字典傳遞到 requests.post() 中,
檢查獲得的響應中的 Json 對象的值。
修改 box_requests,添加 post_json方法
def post_json(self, url, data=None, json=None, **kwargs): """ 用 POST 的方式請求 URL :param url: :param data: 提交的數據 :param json: 提交的 json (可選) :param kwargs: 額外的參數 :return: """ r = requests.post(url, data, json, **kwargs) http_response = HttpResponse() http_response.status_code = r.status_code http_response.json_dict = r.json() return http_response
建立 page,爲測試用例服務
class PingxxChargeCreatePage: base_url = "/charges" def create_charge(self, host, data, headers): br = BoxRequests() # 用 host 和 全局 base_url 拼成 URL http_response = br.post_json(host + self.base_url, data=data, headers=headers) return http_response
建立 test_case
import unittest from pages.api.pingxx_charge_create_page import PingxxChargeCreatePage class PingxxApiTests(unittest.TestCase): test_secret_key = None app_id = None page = None host = None def setUp(self): self.app_id = "app_rfv1SGmPKijLnPef" self.test_secret_key = "c2tfdGVzdF80bXpqelRpenpQQ0NqVEt5VEN5VGVqYlA6" self.host = "https://api.pingxx.com/v1" self.page = PingxxChargeCreatePage() def test_create_charge_by_wechat(self): data = { "order_no": "88888888666666664444444422222222", "app[id]": self.app_id, "channel": "wx", "amount": 10000000, "client_ip": "192.168.1.202", "currency": "cny", "subject": "iphone 999 正品行貨", "body": "正規的水貨 iphone 8 1000臺", "description": "iphone 8 一打。。。。訂單附加說明,最多 255 個 Unicode 字符。" } headers = {"Authorization": "Basic %s" % self.test_secret_key} r = self.page.create_charge(self.host, data, headers) self.assertEqual(200, r.status_code, "狀態碼返回不正確!") expected_object = "charge" actual_object = r.json_dict["object"] self.assertEqual(expected_object, actual_object, "對象是否爲支付檢查失敗!")
if name == 「main「:
unittest.main()
「`