『居善地』接口测试 — 13、Moco框架的使用
提示:我们上一篇文章介绍了什么是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 !"
}
},
{
"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文件配置,所有的数据值是固定的,
如: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文件中接口编写要接收的数据不一致,则无法请求成功。
『居善地』接口测试 — 13、Moco框架的使用的更多相关文章
- 『居善地』接口测试 — 12、Moco框架介绍
目录 1.Mock功能介绍 2.Moco框架介绍 3.Moco框架在接口测试中的作用 4.Moco框架的优点 5.Moco框架的下载与启动 (1)Moco框架的下载 (2)Moco框架的启动 1.Mo ...
- 『居善地』接口测试 — 6、Httpbin服务介绍
目录 1.Httpbin服务介绍 2.在Windows系统中部署Httpbin服务 3.在Linux系统中部署Httpbin服务 4.Httpbin访问方式 5.Httpbin常用调试接口 6.总结: ...
- 『居善地』接口测试 — 7、Requests库使用proxies代理发送请求
目录 1.代理的了解 2.代理的分类 (1)正向代理 (2)反向代理 (3)总结 3.Requests库使用代理 4.总结 1.代理的了解 在上图中我们可以把Web server看成是Google服务 ...
- 『居善地』接口测试 — 3、Requests库介绍
目录 1.Requests库 2.Requests库文档 3.Requests库安装 4.Requests库的使用 (1)使用步骤 (2)示例练习 5.补充:Json数据和Python对象互相转化 1 ...
- 『居善地』接口测试 — 4、Requests库发送GET请求
目录 1.使用Requests库发送带参数的GET请求 2.查看GET请求的内容 3.带请求头.参数的Get请求 Requests库GET请求是使用HTTP协议中的GET请求方式对目标网站发起请求. ...
- 『居善地』接口测试 — 5、使用Requests库发送POST请求
目录 1.请求正文是application/x-www-form-urlencoded 2.请求正文是raw (1)json格式文本(application/json) (2)xml格式文本(text ...
- 『居善地』接口测试 — 11、接口签名sign原理
目录 1.什么是加密以及解密? 2.加密方式的分类 (1)对称加密 (2)非对称加密 (3)总结: 3.接口签名sign原理 (1)什么是接口签名? (2)为什么需要做接口签名 (3)接口签名的实践方 ...
- 『心善渊』Selenium3.0基础 — 24、Selenium的expected_conditions模块详细介绍
目录 1.EC模块介绍 2.EC模块常用类 3.EC模块的使用 4.EC模块综合使用 (1)title_is(title)示例 (2)presence_of_element_located(locat ...
- 『政善治』Postman工具 — 13、Postman接口测试综合练习
目录 (一)项目接口文档 1.鉴权接口 2.注册接口 3.登录接口 4.用户信息接口 5.注销接口 (二)网站上手动验证 (三)Postman测试实现 1.准备工作 (1)创建一个Collection ...
随机推荐
- XCTF-ics-05
ics-05 题目描述 其他破坏者会利用工控云管理系统设备维护中心的后门入侵系统 解题步骤 用dirsearch和御剑扫了一下,只有index.php,尝试了一边,也只有index.php,也就是设备 ...
- python通过字符串定义函数名
记录python里的一个有意思的小技巧:通过字符串定义函数名称. import sys m=sys.modules[__name__] def temp(x): return x+1 setattr( ...
- 二、多线程之Thread中run 和start 区别
Thread使用run 和start 区别 结论:run()方法将作为当前调用线程本身的常规方法调用执行,并且不会发生多线程. System.out.println("开始测试多线程&quo ...
- 『政善治』Postman工具 — 7、Postman中保存请求(Collections集合)
目录 1.创建Collection 2.保存Request请求 3.查看保存的请求 4.Collection下还可以创建文件夹 5.补充:Postman中的变量 6.总结 1.创建Collection ...
- React 代码共享最佳实践方式
任何一个项目发展到一定复杂性的时候,必然会面临逻辑复用的问题.在React中实现逻辑复用通常有以下几种方式:Mixin.高阶组件(HOC).修饰器(decorator).Render Props.Ho ...
- web&HTML
内容索引 1. web概念概述 2. HTML web概念概述 * JavaWeb: * 使用Java语言开发基于互联网的项目 * 软件架构: 1. C/S: Client/Server 客户端/服务 ...
- Windows服务与会话的理解
服务 Windows NT操作系统是基于客户/服务器模式的(C/S).将操作系统中最基本的部分放到内核中,而把操作系统的绝大多数部分都放到微内核外面的一组服务器(进程)中实现.如对进程管理的进程管理服 ...
- “深度评测官”——记2020BUAA软工软件案例分析作业
项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任建) 这个作业的要求在哪里 个人博客作业-软件案例分析 我在这个课程的目标是 完成一次完整的软件开发经历并以博客的方式记录开发 ...
- 三分钟了解B2B CRM系统的特点
最近很多朋友想了解什么是B2B CRM系统,说到这里小Z先来给大家说说什么是B2B--B2B原本写作B to B,是Business-to-Business的缩写.正常来说就是企业与企业之间的生意往来 ...
- Linux_交换分区SWAP
一.交换分区SWAP 1️⃣:交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用. 2️⃣:交换分区一般指定虚拟内存的大小 ...