『居善地』接口测试 — 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 !" } }, { "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文件中接口编写要接收的数据不一致,则无法请求成功。
- 13、Spring工具类的使用
- 如何优雅地使用httprunner进行接口测试
- Android学习笔记13:EditText的使用
- 如何进行接口测试,接口测试的几种工具的使用
- 13_session_使用和创建的时机_删除
- 使用Eclipse-Maven-git做Java开发(13)--导入git仓库的代码到eclipse
- Windows Phone 7 Tip (13) -- 如何搜集应用使用数据
- 第十七章 个人渣翻译 spf13插件---Tagbar(ctags) 使用教程
- Ubuntu(13):如何使用fcitx输入法
- 关于jmeter接口测试使用规则(第三天
- python+selenium自动化测试-13使用JavaScript操作页面元素(含改变元素属性值)
- C++编程规范之13:确保资源为对象所拥有。使用显示的RAII和智能指针
- Java 13发布;Facebook总部员工跳楼自杀;微软开源使用二十余年的 C++ 标准库 | Q新闻
- Java基础学习笔记13——(StringBuffer的使用,数组高级,Arrays工具类,Integer和Character等包装类)
- 使用fiddle进行接口测试
- Python系列视频教程: Django【13讲】第八讲 数据库使用2
- Elasticsearch Java API 的使用(13)—分组聚合之一
- Python学习:13、使用python实现汉诺塔(河内塔)
- 使用Python的requests库作接口测试——响应头中的链接字段
- 使用Python的requests库作接口测试——对HTTP动词的支持