一、什么是mock測試
mock測試就是對於某些不容易構造或者不容易獲取的對象,用一個虛擬的對象來創建以便測試的測試方法
二、mock常見場景
- 無法控制第三方系統某接口的返回,返回的數據不滿足要求
- 某依賴系統還未開發完成,就需要對被測系統進行測試
三、mock工具:moco-runner
1、moco-runner下載
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.12.0
2、moco-runner 常用屬性
屬性 | 說明 |
method | 請求方式,包含:get、post、delete、head等 |
headers | 請求頭信息 |
json | json格式的數據,可以在請求和響應中 |
uri |
請求路徑 |
text | 文本域,一般用於返回 |
queries | 請求入參 |
forms | 表單 |
cookies | |
xpaths
|
|
json_paths | |
version | |
file | 調用的文件 |
path_resource | |
redirectTo | 重定向到某個地址 |
3、構造接口放到json文件中
[{
"description": "get請求",
"request": {
"uri": "/login/1",
"method": "get",
"queries": {
"username": "123",
"password":"456"
}
},
"response": {
"text": "登錄成功",
"headers":{
"Content-Type":"text/html;charset=utf-8"
}
}
},
{
"description": "post請求",
"request": {
"uri": "/login/2",
"method": "post",
"forms": {
"username": "123",
"password":"456"
}
},
"response": {
"text": "登錄成功",
"headers":{
"Content-Type":"text/html;charset=utf-8"
}
}
},
{
"description": "post請求json格式",
"request": {
"uri": "/login/3",
"method": "post",
"headers": {
"Content-Type":"application/json;charset=utf-8"
},
"json": {
"username": "123",
"password":"456"
}
},
"response": {
"json": {
"code": 100000,
"message": "登錄成功",
"time": "2021/06/15 10:04:49"
},
"headers": {
"Content-Type":"application/json;charset=utf-8"
}
}
}
]
注:charset=utf-8解決工具中文返回亂碼問題
4、啟動moco-runner
運行該文件需要java環境
啟動命令:java -jar moco-runner-0.12.0-standalone.jar http -p 8081 -c login.json
參數說明:
java -jar 是啟動jar文件命令
http -p 是訪問協議和訪問端口號,端口號自定
-c 是模擬的接口文件
5、用postman調用模擬接口