# API測試
API(Application Programming Interface)測試的自動化是軟件測試最基本的一種類型。從本質上來說,API測試是用來驗證組成軟件的那些單個方法的正確性,而不是測試整個系統本身。API測試也稱為單元測試(Unit Testing)、模塊測試(Module Testing)、組件測試(Component Testing)以及元件測試(Element Testing)。從技術上來說,這些術語是有很大的差別的,但是在日常應用中,你可以認為它們大致相同的意思。它們背后的思想就是,必須確定系統中每個單獨的模塊工作正常,否則,這個系統作為一個整體不可能是正確的。毫無疑問,API測試對于任何重要的軟件系統來說都是必不可少的。
我們對API測試的定位是服務對外輸出的API接口測試,屬于黑盒、偏重業務的測試步驟。
看過上一章內容的朋友還記得[lua-resty-test](https://github.com/membphis/lua-resty-test),我們的API測試同樣是需要它來完成。get_client_tasks是終端用來獲取當前可執行任務清單的API,我們用它當做例子給大家做個介紹。
> nginx conf:
~~~
location ~* /api/([\w_]+?)\.json {
content_by_lua_file lua/$1.lua;
}
location ~* /unit_test/([\w_]+?)\.json {
lua_check_client_abort on;
content_by_lua_file test_case_lua/unit/$1.lua;
}
~~~
> API測試代碼:
~~~
-- unit test for /api/get_client_tasks.json
local tb = require "resty.iresty_test"
local json = require("cjson")
local test = tb.new({unit_name="get_client_tasks"})
function tb:init( )
self.mid = string.rep('0',32)
end
function tb:test_0000()
-- 正常請求
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST, body=[[{"type":[1600,1700]}]] }
)
if 200 ~= res.status then
error("failed code:" .. res.status)
end
end
function tb:test_0001()
-- 缺少body
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST }
)
if 400 ~= res.status then
error("failed code:" .. res.status)
end
end
function tb:test_0002()
-- 錯誤的json內容
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST, body=[[{"type":"[1600,1700]}]] }
)
if 400 ~= res.status then
error("failed code:" .. res.status)
end
end
function tb:test_0003()
-- 錯誤的json格式
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST, body=[[{"type":"[1600,1700]"}]] }
)
if 400 ~= res.status then
error("failed code:" .. res.status)
end
end
test:run()
~~~
nginx output:
~~~
0.000 [get_client_tasks] unit test start
0.001 \_[test_0000] PASS
0.001 \_[test_0001] PASS
0.001 \_[test_0002] PASS
0.001 \_[test_0003] PASS
0.001 [get_client_tasks] unit test complete
~~~
使用capture來模擬請求,其實是不靠譜的。如果我們要完全100%模擬客戶請求,這時候就要使用第三方cosocket庫,例如[lua-resty-http](https://github.com/pintsized/lua-resty-http),這樣我們才可以完全指定http參數。
- 序
- Lua簡介
- Lua環境搭建
- 基礎數據類型
- 表達式
- 控制結構
- if/else
- while
- repeat
- 控制結構for的使用
- break,return
- Lua函數
- 函數的定義
- 函數的參數
- 函數的返回值
- 函數回調
- 模塊
- String庫
- Table庫
- 日期時間函數
- 數學庫函數
- 文件操作
- 元表
- 面向對象編程
- FFI
- LuaRestyRedisLibrary
- select+set_keepalive組合操作引起的數據讀寫錯誤
- redis接口的二次封裝(簡化建連、拆連等細節)
- redis接口的二次封裝(發布訂閱)
- pipeline壓縮請求數量
- script壓縮復雜請求
- LuaCjsonLibrary
- json解析的異常捕獲
- 稀疏數組
- 空table編碼為array還是object
- 跨平臺的庫選擇
- PostgresNginxModule
- 調用方式簡介
- 不支持事務
- 超時
- 健康監測
- SQL注入
- LuaNginxModule
- 執行階段概念
- 正確的記錄日志
- 熱裝載代碼
- 阻塞操作
- 緩存
- sleep
- 定時任務
- 禁止某些終端訪問
- 請求返回后繼續執行
- 調試
- 調用其他C函數動態庫
- 我的lua代碼需要調優么
- 變量的共享范圍
- 動態限速
- shared.dict 非隊列性質
- 如何添加自己的lua api
- 正確使用長鏈接
- 如何引用第三方resty庫
- 使用動態DNS來完成HTTP請求
- 緩存失效風暴
- Lua
- 下標從1開始
- 局部變量
- 判斷數組大小
- 非空判斷
- 正則表達式
- 不用標準庫
- 虛變量
- 函數在調用代碼前定義
- 抵制使用module()函數來定義Lua模塊
- 點號與冒號操作符的區別
- 測試
- 單元測試
- API測試
- 性能測試
- 持續集成
- 灰度發布
- web服務
- API的設計
- 數據合法性檢測
- 協議無痛升級
- 代碼規范
- 連接池
- c10k編程
- TIME_WAIT問題
- 與Docker使用的網絡瓶頸
- 火焰圖
- 什么時候使用
- 顯示的是什么
- 如何安裝火焰圖生成工具
- 如何定位問題