您的位置:首页 > 其它

『居善地』接口测试 — 13、Moco框架的使用

2021-06-17 22:57 926 查看

目录

提示:我们上一篇文章介绍了什么是Moco框架,以及Moco框架的启动方式。本篇文章主要说说如何使用Moco框架来辅助我们进行测试。

当需要调用接口来编写测试用例的时候,此时该接口并没有被实现,这个时候我们就可以用Moco框架来模拟一个接口出来。

使用Moco模拟接口以下功能:

  • 拦截服务:
    http
    https
  • 请求方式:GET,POST。
  • 模拟请求地址:URL。
  • 模拟参数:包括
    header
    cookie
    的数据。
  • 模拟响应结果。
  • 支持重定向。

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 !"
}
},
{
"descript
343d
ion": "模拟一个带参数的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文件配置,所有的数据值是固定的,

如:

description
request
response
redirectTo
等这些都是固定的,不能修改,修改可能连Moco服务都启动不来。

还有

request
的属性值,如:
uri
method
cookies
headers
,也是必须这样写的。

还有GET请求传递参数用

queries
属性,POST请求传递参数用
forms
json
属性都可以。(PUT,DELETE请求同Post请求。)

(2)Moco框架原理:

就是把所有接口的数据,包括发送请求的所有数据和返回结果的所有数据,以Json数据格式进行编写。

把这些数据放入Moco框架提供的HTTP或者HTTPS的服务上,就实现了接口数据的模拟。

在使用的时候,我们只要按照json文件中接口配置的信息进行请求即可,如果调用接口传递的数据和Json文件中接口编写要接收的数据不一致,则无法请求成功。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: