关于mock工具的推荐moco-runner

mac2024-05-14  28

mock在我们测试中经常被用到,这里提一下自己经常用的一种简单快捷的小工具moco-runner。moco-runner的使用很简单,下面做一个简单的说明:

1、下载moco-runner工具包也就是一个小jar包,我这里使用的是moco-runner-0.11.0-standalone.jar

下载传送门:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/moco-runner-0.11.0-standalone.jar

moco 基本属性, 14 known properties: "method", --使用的方法,包含:get、post、delete、head等 "headers", --请求头信息 "json", --json格式的数据,可以在请求和响应中 "factory", "uri", -- 项目路径。如/postwithparam "text", -- 文本域,一般用于返回 "cookies", -- cookies信息 "xpaths", "json_paths", "version", "file", "queries", --get方法,传参时用 "path_resource", "forms" --post方法,传参时用 "redirectTo" --重定向到某个地址

2.根据需要简单构造mock server返回值:

模拟get请求: startupGet.json

[ { "description":"模拟一个没有参数的get请求", "request":{ "uri":"/getdemo", "method":"get" }, "response":{ "text":"this is the no param request" } }, { "description":"模拟一个带参的get请求", "request":{ "uri":"/getwithparam", "method":"get", "queries": { "name": "huhansan", "age": "18" } }, "response":{ "text":"i have coming back now." } } ]

模拟post请求 startupPost.json

[ { "description":"模拟一个post请求", "request":{ "uri":"/postDemo", "method":"post" }, "response":{ "text":"This is my first moco post demo... " } }, { "description":"模拟一个带参的post请求", "request":{ "uri":"/postwithparam", "method":"post", "forms": { "name": "huhansan", "sex": "male" } }, "response":{ "text":"i am come back now...huhansan" } } ]

模拟带cookie的请求 startupWithCookies.json

[ { "description":"这是一个会返回cookies信息的get请求", "request":{ "uri":"/getCookies", "method":"get" }, "response":{ "cookies":{ "login":"true" }, "text":"Congratulation for get the cookies info." } }, { "description":"这是一个带cookies信息的get请求", "request":{ "uri":"/get/with/cookies", "method":"get", "cookies":{ "login":"true" } }, "response":{ "text":"This is with the cookies can be get request" } }, { "description":"这是一个带cookies信息的post请求", "request":{ "uri":"/post/with/cookies", "method":"post", "cookies":{ "login":"true" }, "json":{ "name":"huhansan", "age":"18" } }, "response":{ "status":"200", "json":{ "huhansan":"success", "status":"1" } } } ]

模拟一个带header的请求 startupwithHeader.json

[ { "description":"带header的post请求", "request":{ "uri":"/post/with/headers", "method":"post", "headers":{ "Content-Type":"application/json" }, "json": { "name": "xx", "sex": "male" } }, "response":{ "json":{ "xx":"success", "status":"1" } } } ]

模拟一个带重定向的请求 startupWithRedirect.json

[ { "description":"这是一个带自动重定向的请求,重定向到百度", "request":{ "uri":"/redirect" }, "redirectTo":"http://www.baidu.com" }, { "description":"重定向到一个自己的网站", "request":{ "uri":"/redirect/topath" }, "redirectTo":"/redirect/new" }, { "description":"这是被重定向到的请求", "request":{ "uri":"/redirect/new" }, "response":{ "text":"redirect successful." } } ]

然后将模拟的请求保存为不同的json文件,在服务器上或者本地启动moco-runnner server,以startupGet.json为例 命令如下:

java -jar ./moco-runner-0.11.0-standalone.jar http -p 8889 -c startupGet.json

然后你可以使用jmeter工具去测试,如果你是本地访问的话,那么访问地址是

http://localhost:8889/getdemo     method选择:get

将返回:"this is the no param request

最新回复(0)