接口测试-moco框架+实例
moco
1.什么是moco框架
是開發(fā)在開發(fā)的過程中,需要依賴一部分的接口,但是對方沒有提供或者環(huán)境等等情況
2.moco下載安裝
安裝jdk1.8并配置環(huán)境變量
http://central.maven.org/maven2/com/github/dreamhead/moco-runner/1.0.0/moco-runner-1.0.0-standalone.jar
下載moco-runner到本地
3.mock目錄結構
moco-runner-1.0.0-standalone.jar 放到文件夾中
同級目錄存放 configure.json(get或者post數據)
D:moco-runner
├─moco-runner-1.0.0-standalone.jar
└─configure.json
4.啟動服務
java -jar moco-runner-1.0.0-standalone.jar start -p 8888 -c configure.json
java -jar jar包的路徑 http -p 運行端口 -c 要運行的文件路徑
如果啟動失敗了,請檢查json數據是否正確
json格式很重要
單詞的拼寫也很重要
5.實際事例
把get或者post放到configure.json中
1.get接口實例
一段json數據,json的格式
[{
"key1": "values",
"key2": "values"
},
{
"key1": "values",
"key2": "values"
}
]
數據實例
[
{
"description": "一個簡單的get請求",
"request": {
"method": "get",
"uri": "/login"
},
"response": {
"text": "我是login get method",
"headers":{
"Content-Type":"text/html;charset=utf-8"
}
}
},
{
"description": "帶參數的get請求,p1和p2是兩個參數",
"request": {
"method": "get",
"uri": "/reg",
"queries": {
"p1": "v1",
"p2": "v2"
}
},
"response": {
"text": "帶參數的get請求",
"headers":{
"Content-Type":"text/html;charset=utf-8"
}
}
},
{
"description": "get請求返回json類型數據",
"request": {
"method": "get",
"uri": "/login_json"
},
"response": {
"json": {
"key":"value",
"請求方式是get":"響應結果為json類型"
},
"headers": {
"Content-Type": "application/json;charset=utf-8"
}
}
}
]
瀏覽器地址欄輸入:
http://127.0.0.1:8888/login
http://127.0.0.1:8888/reg?p1=v1&p2=v2
http://127.0.0.1:8888/login_json
2.post接口實例
數據實例
[
{
"description": "post請求,請求參數為json格式,響應格式為json",
"request": {
"method": "post",
"uri": "/post_json",
"json": {
"login_status": "successful"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=utf-8"
}
}
},
{
"description": "post請求,請求及響應都為json,并且請求帶cookies",
"request": {
"method": "post",
"uri": "/post_cookie",
"json": {
"login_status": "successful"
},
"cookies":{
"user_id":"xsdaqawea"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=utf-8"
}
}
},
{
"description": "post請求,請求及響應都為json,并且請求帶cookies和headers",
"request": {
"method": "post",
"uri": "/post_cookie_headers",
"json": {
"login_status": "successful"
},
"cookies": {
"user_id": "xsdaqawea"
},
"headers":{
"Content-Type":"application/json"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=utf-8"
}
}
},
{
"description": "post請求,請求和響應為form,入參是form形式,返回是json數據",
"request": {
"method": "post",
"url": "/login_form",
"forms": {
"username": "zhangshan",
"password": "123456"
},
"headers": {
"content-type": "application/x-www-form-urlencoded"
}
},
"response": {
"json": {
"error_code": 0,
"reason": "successed",
"username": "zhangshan",
"checkstatus": "on"
},
"status": 200
}
}
]
注意:post中
headers:請求頭,根據是form還是json格式的請求來填寫
from格式:"content-type": "application/x-www-form-urlencoded"
json格式:"content-type": "application/json"
請求參數格式以及數據,對應headers的content-type
form格式關鍵字為forms
json格式關鍵字為json
post請求的不能直接通過地址訪問,寫個requests訪問下數據吧
import requests
url1 = 'http://127.0.0.1:8888/post_json'
json1 = {"login_status": "successful"}
res1 = requests.post(url=url1, json=json1)
print(res1.json())
url2 = 'http://127.0.0.1:8888/post_cookie'
cookies2 = {"user_id": "xsdaqawea"}
json2 = {"login_status": "successful"}
res2 = requests.post(url=url2, cookies=cookies2, json=json2)
print(res2.json())
url3 = 'http://127.0.0.1:8888/post_cookie_headers'
cookies3 = {"user_id": "xsdaqawea"}
headers3 = {"Content-Type": "application/json"}
json3 = {"login_status": "successful"}
res3 = requests.post(url=url3,cookies=cookies3,headers=headers3,json=json3)
print(res3.json())
url4 = 'http://127.0.0.1:8888/login_form'
data4 = {"username": "zhangshan","password": "123456"}
headers4 = {"Content-Type": "application/x-www-form-urlencoded"}
res4 = requests.post(url=url4,data=data4,headers=headers4)
print(res4.json())
3.定制重定向
{
"description":"重定向到指定網站",
"request":{
"method":"get",
"uri":"/login_redirect"
},
"redirectTo":"https://www.baidu.com"
}
瀏覽器地址欄輸入:
http://127.0.0.1:8888/login_redirect
自動重定向到baidu
6.mock工作原理
總結
以上是生活随笔為你收集整理的接口测试-moco框架+实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 卧室灯具的选择原则及注意事项
- 下一篇: tora消息机制(事件监听,触发,取消)