提示:我們上一篇文章介紹了什麼是Moco框架,以及Moco框架的啟動方式。本篇文章主要說說如何使用Moco框架來輔助我們進行測試。

當需要呼叫介面來編寫測試用例的時候,此時該介面並沒有被實現,這個時候我們就可以用Moco框架來模擬一個接口出來。

使用Moco模擬介面以下功能:

  • 攔截服務:httphttps
  • 請求方式:GET,POST。
  • 模擬請求地址:URL。
  • 模擬引數:包括headercookie的資料。
  • 模擬響應結果。
  • 支援重定向。

1、Moco框架第一個練習

編寫一個Json檔案,介面所有的資訊都配置在該json檔案中。

[
{
"description": "第一個Moco框架例子。", # 描述:增加介面的可讀性
"request": {
"uri": "/api/moco/demo",
},
"response": {
"text": "hello Moco !"
}
}
]

把Moco框架的jar包和上面編輯好的Json檔案放在同一個資料夾中。

在cmd命令列或者PyCharm的命令列終端執行啟動命令。

  • 進入Json檔案的所在目錄。
  • 執行命令:java -jar ./moco-runner-0.12.0-standalone.jar http -p 12306 -c test.json

Moco服務啟動後,我們可以使用Requests庫請求介面,也可以用瀏覽器介面。

# 1.匯入requests庫
import requests # 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/demo" # 3.傳送請求
response = requests.get(url=url)
print(response.text)

瀏覽器訪問介面:

2、Get方法的Mock實現

我們主要是看Json檔案怎麼寫,其他步驟和上面練習一樣。

(1)沒有引數的get請求

[
{
"description": "模擬一個沒有引數的get請求。",
"request": {
"uri": "/api/moco/get/demo",
"method": "get" # 這裡添加了要給method屬性
},
"response": {
"text": "hello get request !"
}
}
]

(2)有引數的get請求

[
{
"description": "模擬一個沒有引數的get請求。",
"request": {
"uri": "/api/moco/get/demo",
"method": "get"
},
"response": {
"text": "hello get request !"
}
},
{
"description": "模擬一個帶引數的get請求。",
"request": {
"uri": "/api/moco/get/param/demo",
"method": "get",
"queries": { # get請求引數的選項,queries固定屬性。
"name": "xiaoming",
"age": "18"
}
},
"response": {
"text": "hello xiaoming !"
}
}
]

說明:請求地址為:http://127.0.0.1:12306/api/moco/get/param/demo?name=xiaoming&age=18

3、Post方法的Mock實現

(1)沒有引數的post請求

[
{
"description": "模擬一個不帶資料的post請求。",
"request": {
"uri": "/api/moco/post/demo",
"method": "post"
},
"response": {
"text": "hello post request !"
}
}
]

提示:POST請求就不能用瀏覽器進行查看了。只能用Request庫或者JMeter,Postman等進行檢視。(能進行介面呼叫的工具都可以)

# 1.匯入requests庫
import requests # 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/demo" # 3.傳送請求
response = requests.post(url=url)
print(response.text)

(2)有引數的post請求

[
{
"description": "模擬一個帶資料post請求。",
"request": {
"uri": "/api/moco/post/param/demo",
"method": "post",
"forms": { # post請求帶引數,引數要新增到forms屬性中。
"name": "xiaoming",
"age": "18"
}
},
"response": {
"text": "hello post xiaoming !"
}
}
]

呼叫介面檢視結果,如下所示:

# 1.匯入requests庫
import requests # 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/param/demo" data = {
"name": "xiaoming",
"age": "18"
} # 3.傳送請求
response = requests.post(url=url, data=data)
print(response.text)

4、請求中加入Cookies

使用的是request中的cookies屬性。

(1)get請求

[
{
"description": "模擬一個帶cookie的get請求。",
"request": {
"uri": "/api/moco/get/cookies/demo",
"method": "get",
"cookies": { # 這裡新增cookies引數
"login": "true"
}
},
"response": {
"text": "hello get cookies !"
}
}
]

呼叫介面檢視結果,如下所示:

# 1.匯入requests庫
import requests # 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/get/cookies/demo" cookies = {
"login": "true"
} # 3.傳送請求
response = requests.get(url=url, cookies=cookies)
print(response.text)

(2)post請求

[
{
"description": "模擬一個帶cookie的post請求。",
"request": {
"uri": "/api/moco/post/cookies/demo",
"method": "post",
"cookies": {
"login": "true"
},
"json": { # post請求的引數也可以用json格式的資料進行傳輸
"name": "xiaoming",
"age": "18"
}
},
"response": {
"status": 201,
"json": {
"text": "hello post cookies !"
}
}
}
]

呼叫介面檢視結果,如下所示:

# 1.匯入requests庫
import requests # 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/cookies/demo" cookies = {
"login": "true"
} json = {
"name": "xiaoming",
"age": "18"
} # 3.傳送請求
response = requests.post(url=url, json=json ,cookies=cookies)
print(response.text)

5、請求中加入Header

使用的是request中的headers屬性。

Header是新增請求頭資訊,關於請求頭資訊get請求和post請求都是一樣的。

[
{
"description": "模擬一個帶Header的post請求。",
"request": {
"uri": "/api/moco/post/headers/demo",
"method": "post",
"headers": { # 新增請求頭資訊
"content-type": "application/json"
},
"json": {
"name": "xiaoming",
"age": "18"
}
},
"response": {
"status": 201,
"json": {
"text": "hello get Headers !"
}
}
}
]

呼叫介面檢視結果,如下所示:

# 1.匯入requests庫
import requests # 2.明確請求地址
url = "http://127.0.0.1:12306/api/moco/post/headers/demo" headers = {
"content-type": "application/json"
} json = {
"name": "xiaoming",
"age": "18"
} # 3.傳送請求
response = requests.post(url=url, json=json, headers=headers)
print(response.text)

6、Moco模擬重定向

重定向使用的是和request同級的redirectTo屬性。

[
{
"description": "重定向到百度",
"request": {
"uri": "/api/moco/redirect/demo",
"method": "get"
},
"redirectTo": "http://www.baidu.com"
},
{
"description": "重定向到自己的介面",
"request": {
"uri": "/api/moco/redirect/new/demo",
"method": "get"
},
"redirectTo": "http://www.baidu.com",
"response": {
"text": "hello redirectTo !"
}
}
]

使用瀏覽器進行測試就可以。

還有更多的使用方式請檢視文件:https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md

7、綜合練習

Json檔案內容,如下所示:

[
{
"description": "department by dep_id",
"request": {
"uri": "/api/departments/",
"method": "get",
"queries": {
"$dep_id_list": "T001"
}
},
"response": {
"json": {
"count": 1,
"next": null,
"previous": null,
"results": [
{
"dep_id": "T001",
"dep_name": "php學院",
"master_name": "老李",
"slogan": "啦啦啦啦"
}
]
}
}
},
{
"description": "update department by dep_id",
"request": {
"uri": "/api/departments/T03/",
"method": "put",
"json": {
"data": [
{
"dep_id": "T03",
"dep_name": "C++",
"master_name": "C++-Master",
"slogan": "Here is Slogan"
}
]
}
},
"response": {
"status": 201,
"json": {
"dep_id": "T03",
"dep_name": "C++",
"master_name": "C++-Master",
"slogan": "Here is Slogan"
}
}
}
]

8、總結:

(1)Json檔案的配置屬性說明:

像我們上面練習過的Json檔案配置,所有的資料值是固定的,

如:descriptionrequestresponseredirectTo等這些都是固定的,不能修改,修改可能連Moco服務都啟動不來。

還有request的屬性值,如:urimethodcookiesheaders,也是必須這樣寫的。

還有GET請求傳遞引數用queries屬性,POST請求傳遞引數用formsjson屬性都可以。(PUT,DELETE請求同Post請求。)

(2)Moco框架原理:

就是把所有介面的資料,包括髮送請求的所有資料和返回結果的所有資料,以Json資料格式進行編寫。

把這些資料放入Moco框架提供的HTTP或者HTTPS的服務上,就實現了介面資料的模擬。

在使用的時候,我們只要按照json檔案中介面配置的資訊進行請求即可,如果呼叫介面傳遞的資料和Json檔案中介面編寫要接收的資料不一致,則無法請求成功。