mock测试(挡板测试)

https://www.cnblogs.com/lc-blogs/p/17027617.html

mock测试(挡板测试)就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。
比如:测试天气这个接口,最近上海都没有下雪,那么我怎么去模拟下雪这个场景?需要等到真正下雪的哪天才可以吗?这个时候就可以通过mock来帮助我们完成下雪的场景了

  1. 后端代码还未开发完,前端代码需要调用后端接口进行调试,怎么办?
  2. 无法控制第三方系统某个接口的返回,返回的数据不满足要求?
  3. 需要跟第三方联调但对方还未开发完成,如何提早测试?
  4. 不用等开发人员开发完接口再进行自动化测试case的编写,在这个过程中可以修改、补case,在后端开发接口完成以后,只需要执行测试case,省去了很大的工作量,并且这些完善的用例脚本,用自动化去执行,效果更好。

fiddler之AutoResponder

AutoResponder参考:AutoResponder的使用

postman之Mock Server

Mock Server参考:Mock Server的使用

moco-runner

Moco是一个简单搭建模拟服务器的框架(工具),可以模拟http、https、socket等协议。基于Java开源(必须java环境)。
准备工具:jdk环境:jdk的安装与配置

http协议

1、下载

下载地址:Central Repository: com/github/dreamhead/moco-runner
GitHub:GitHub - dreamhead/moco: Easy Setup Stub Server

mock测试(挡板测试)_第1张图片

mock测试(挡板测试)_第2张图片

启动命令:
java -jar moco的jar文件  协议类型  -p 端口号 -c json配置文件

java -jar moco-runner-1.3.0-standalone.jar http -p 9090 -c result.json

//jar包的名称根据自己下载的jar包版本来写;
//http 代表这个模拟的是http请求;
//-p 9090 是定义端口号;
//-c result.json 是编辑请求与响应的json文件名;

2、使用

  • 新建文件夹,例如:mock
  • 将下载的moco-runner-1.3.0-standalone.jar放到mock文件夹中

3、模拟get接口响应(不带参数)

1)新建json文件(例如:result.json)放到mock文件夹中,json文件用来存放接口mock响应数据

mock测试(挡板测试)_第3张图片

2)打开result.json文件,模拟get请求不带参数,编辑如下:

[
	{
		"description": "首页(不带参数)",
		"request": {
			"uri": "/index",
			"method": "get"
		},
		"response": {
			"text": "进入首页成功!"
		}
	}
]

//description:接口描述信息
//request:所有请求的数据
//uri:请求路径
//method:HTTP请求类型GET|POST|PUT|DELETE
//response:所有的响应数据
//text:响应的文本

3)启动

  • 在创建的mockrunner文件夹中启动cmd窗口

mock测试(挡板测试)_第4张图片

  • 在cmd窗口输入命令:java -jar moco-runner-1.3.0-standalone.jar http -p 9090 -c result.json
java -jar moco-runner-1.3.0-standalone.jar http -p 9090 -c result.json

//jar包的名称根据自己下载的jar包版本来写;
//http 代表这个模拟的是http请求;
//-p 9090 是定义端口号;
//-c result.json 是编辑请求与响应的json文件名;

下图表示启动成功:

  • 停止mock服务,使用Ctrl+c,或者关闭cmd窗口

  • 启动服务后,在浏览器中访问:127.0.0.1:9090/index

    mock测试(挡板测试)_第5张图片

postmanq发送请求示例

mock测试(挡板测试)_第6张图片

4、模拟get接口响应(带参数)

  • 编辑mock.json文件,模拟get请求带参数,编辑如下:
[
	{
		"description": "个人主页",
		"request": {
			"uri": "/userhomepage",
			"method": "get",
			"queries": {
				"userid": "3882",
				"name": "lucy"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"code": "10000",
				"message": "进入个人主页成功",
				"data": {
					"name": "lucy",
					"age": "19",
					"addr": "beijing",
					"tel": "15512348888",
					"lastlogin": "2020-10-01 10:55:23"
				}
			}
		}
	}
 ]

//headers设置response的返回数据为application/json
//request中的queries:定义get请求参

postman发送请求示例

mock测试(挡板测试)_第7张图片

5、模拟post请求

  • 1)传递参数 Content-Type: application/json 为JSON格式,传递的参数是以JSON格式,参数包含在一对花括号中,以键值对的方式。
    编辑mock.json文件,模拟post请求,编辑如下:
[
	{
		"description": "登录,请求Content-Type:application/json",
		"request": {
			"uri": "/login",
			"method": "post",
			"headers": {
				"Content-Type": "application/json"
			},
			"json": {
				"username": "admin",
				"password": "123456"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"code": "10000",
				"message": "登录成功",
				"data": {
					"userid": "3882",
					"name": "lucy",
					"addr": "chengdu",
					"tel": "13355556666",
					"token": "mocktoken9gd878ad"
				}
			}
		}
	}
]

//headers中的"Content-Type": "application/json"指传递参数以json方式
//headers中的"token": "mocktoken"指请求头中必须加上token才能访问
//status:定义http响应状态码

postman发送请求示例

mock测试(挡板测试)_第8张图片

  • 2)传递参数 Content-Type: multipart/form-data 为form-data格式,参数写在body中以参数名称和值对应关系,常用于传递参数和文件,请求的Content-Type字段中有一个boundary字段,传参时每个参数都是由一对boundary包含。
    编辑mock.json文件,模拟post请求,编辑如下:
[
	{
		"description": "商品详情页,请求Content-Type:multipart/form-data",
		"request": {
			"uri": "/details",
			"method": "post",
			"forms": {
				"goodid": "1",
				"date": "20220101"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"goodid": "1",
				"name":"pears",
				"status":"1",
				"stock":"25",
				"vip_amount ":"24",
				"sale_amount":"18",
				"market_amount":"23.6",
				"attribute_value":"丰富营养",
				"product_attr_vals":"单果300g",
				"share_title":"分享标题",
				"share_descrip":"分享描述",
				"license":"营业执照",
				"employType":0
			}
		}
	}
]

postman发送请求示例

mock测试(挡板测试)_第9张图片

  • 3)传递参数 Content-Type: application/x-www-form-urlencoded 格式,和get请求传递参数一样的方式,区别get请求参数拼接在url中,post请求参数在body中。
    编辑mock.json文件,模拟post请求,编辑如下:
[
	{
		"description": "订单详情页,请求Content-Type:application/x-www-form-urlencoded",
		"request": {
			"uri": "/order",
			"method": "post",
			"headers": {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			"forms": {
				"ordernum": "20201010",
				"userid": "3218"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"orderNo": "LO20111216292785711001",
				"productId":2,
				"skuId":22,
				"expert":0,
				"count":1,
				"payType":"WECHAT_PAY",
				"payClient":"MINI",
				"mobile":"15812345678",
				"goodname":"apple",
				"orderType":"NORMAL",
				"logistics":"NONE"
			}
		}
	}
]

postman发送请求示例

mock测试(挡板测试)_第10张图片

mock文件参数详解

description:请求描述(程序不会识别到)

request:请求内容

  • uri:请求的url资源路径
  • method:请求类型,支持HTTP协议相关的请求类型,如post/get/put/delete等
  • headers:request的请求头
  • token:接口请求必须在headers带的token
  • cookies:接口进行cookie信息的校验
  • queries:get请求的请求参数
  • forms:post的表单请求体
  • json:request的请求json数据,当然也可是本地的json文件(可实现数据分离)

response:响应内容

  • status:定义http响应状态码
  • headers:response的响应头
  • json:response的响应json数据,当然也可是本地的json文件(可实现数据分离)
  • text:text响应体
  • redirectTo:请求时重定向

注:多个请求的mock可以写到一个文件中,每个mock之间以英文的逗号间隔,以下为不同请求方式的接口

[
	{
		"description": "首页",
		"request": {
			"uri": "/index",
			"method": "get"
		},
		"response": {
			"status": 200,
			"text": "Welcome to the the home page..."
		}
	},
	{
		"description": "个人主页",
		"request": {
			"uri": "/userhomepage",
			"method": "get",
			"headers": {
				"token": "mocktoken9gd878ad"
			},
			"queries": {
				"userid": "3882",
				"name": "lucy"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"code": "10000",
				"message": "进入个人主页成功",
				"data": {
					"name": "lucy",
					"age": "19",
					"addr": ["beijing","shanghai","chengdu"],
					"tel": "15512348888",
					"lastlogin": "2020-10-01 10:55:23"
				}
			}
		}
	},
	{
		"description": "登录,请求Content-Type:application/json",
		"request": {
			"uri": "/login",
			"method": "post",
			"headers": {
				"Content-Type": "application/json"
			},
			"json": {
				"username": "admin",
				"password": "123456"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"code": "10000",
				"message": "登录成功",
				"data": {
					"userid": "3882",
					"name": "lucy",
					"addr": "chengdu",
					"tel": "13355556666",
					"token": "mocktoken9gd878ad"
				}
			}
		}
	},
	{
		"description": "商品详情页,请求Content-Type:multipart/form-data",
		"request": {
			"uri": "/details",
			"method": "post",
			"forms": {
				"goodid": "1",
				"date": "20220101"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"goodid": "1",
				"name":"pears",
				"status":"1",
				"stock":"25",
				"vip_amount ":"24",
				"sale_amount":"18",
				"market_amount":"23.6",
				"attribute_value":"丰富营养",
				"product_attr_vals":"单果300g",
				"share_title":"分享标题",
				"share_descrip":"分享描述",
				"license":"营业执照",
				"employType":0
			}
		}
	},
	{
		"description": "订单详情页,请求Content-Type:application/x-www-form-urlencoded",
		"request": {
			"uri": "/order",
			"method": "post",
			"headers": {
				"Content-Type": "application/x-www-form-urlencoded",
				"token": "mocktoken9gd878ad"
			},
			"forms": {
				"ordernum": "20201010",
				"userid": "3218"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"orderNo": "LO20111216292785711001",
				"productId":2,
				"skuId":22,
				"expert":0,
				"count":1,
				"payType":"WECHAT_PAY",
				"payClient":"MINI",
				"mobile":"15812345678",
				"goodname":"apple",
				"orderType":"NORMAL",
				"logistics":"NONE"
			}
		}
	},
	{
		"description": "物流详情,请求Content-Type:application/json,必须加上token才能访问",
		"request": {
			"uri": "/logistics",
			"method": "post",
			"headers": {
				"Content-Type": "application/json",
				"token": "mocktoken9gd878ad"
			},
			"json": {
				"LogisticCode": "118461988807",
				"ShipperCode": "SF"
			}
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"EBusinessID": "1109259",
				"OrderCode": "1",
				"ShipperCode": "SF",
				"LogisticCode": "118461988807",
				"Success": true,
				"CallBack":"",
				"State": 3,
				"Reason": null,
				"Traces": [
					{
					"AcceptTime": "2014/06/25 08:05:37",
					"AcceptStation": "正在派件..(派件人邓裕富,电话:18718866310)[深圳市]",
					"Remark": "超时1小时"
					},
					{
					"AcceptTime": "2014/06/25 04:01:28",
					"AcceptStation": "快件在深圳集散中心,准备送往下一站深圳[深圳市]",
					"Remark": "准时到达"
					},
					{
					"AcceptTime": "2014/06/25 01:41:06",
					"AcceptStation": "快件在深圳集散中心[深圳市]",
					"Remark": "提前10分钟"
					},
					{
					"AcceptTime": "2014/06/24 20:18:58",
					"AcceptStation": "已收件[深圳市]",
					"Remark": "准时到达"
					},
					{
					"AcceptTime": "2014/06/24 20:55:28",
					"AcceptStation": "快件在深圳,准备送往下一站深圳集散中心[深圳市]",
					"Remark": "超时30分钟"
					},
					{
					"AcceptTime": "2014/06/25 10:23:03",
					"AcceptStation": "派件已签收[深圳市]",
					"Remark": "超时17分钟"
					},
					{
					"AcceptTime": "2014/06/25 10:23:03",
					"AcceptStation": "签收人是:已签收[深圳市]",
					"Remark": "送货上门签收"
					}
				]
			}
		}
	},
	{
		"description": "上传json文件,仅能postman使用binary访问",
        "request": {
            "uri": "/file",
            "method": "post",
            "file": {
                "json": "C:\\data.json"
            }
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json": {
				"code":"10000",
				"message":"文件上传成功",
				"data":{
				}
			}
		}
	},
	{
		"description":"重定向到百度",
		"request":{
		  "uri":"/redirect",
		  "method":"get"
		},
		"redirectTo":"http://www.baidu.com",
		"response": {
			"status": 301
		}
	},
	{
		"description": "查询天气",
		"request": {
			"uri": "/weatherquery",
			"method": "get"
		},
		"response": {
			"status": 200,
			"headers": {
				"Content-Type": "application/json;charset=UTF-8"
			},
			"json":{
				"reason": "查询成功",
				"result":{
					"city":"苏州",
					"realtime":{
						"temperature":"4",
						"humidity":"82",
						"info":"阴天",
						"wid":"02",
						"direct":"西北方向",
						"power":"3",
						"aqi":"80"
					},
					"future":[{
							"date": "2019-02-22",
							"temperature":"1/7摄氏",
							"weather":"小雨转多yun",
							"wid":{
								"day":"07",
								"night":"12"
							},
							"direct":"北风转西北风"
						},
						{
							"date":"2019-02-23",
							"temperature":"2/11摄氏",
							"weather":"多云转阴",
							"wid":{
								"day":"01",
								"night":"02"
							},
							"direct": "北风转东北风"
						},
						{
							"date": "2019-02-24",
							"temperature": "6/12摄氏",
							"weather": "多云",
							"wid": {
								"day": "01",
								"night": "05"
							},
							"direct": "东北风转北风"
						},
						{
							"date": "2019-02-25",
							"temperature": "5/12摄氏",
							"weather": "小雨转晴",
							"wid": {
								"day": "07",
								"night": "09"
							},
							"direct": "东风转北风向"
						},
						{
							"date": "2019-02-26",
							"temperature": "5/11摄氏",
							"weather": "多云转小yu",
							"wid": {
								"day": "01",
								"night": "07"
							},
							"direct": "东南风向"
						}
					]
				},
			"error_code": 0
		}
	}
}
]

https协议

添加https证书

1、在C:\Users\Administrator路径下新建.keystore用来保存证书

(设置的电脑名称不一样对应路径有区别)

mock测试(挡板测试)_第11张图片

2、生成https证书文件(可以是cer或者jks格式)

使用win+r输入cmd,在cmd窗口输入如下命令

keytool -genkey -alias cps.cer -keyalg RSA -keystore C:\Users\Administrator\.keystore\httpsmoco.cer -keysize 1024 -validity 99999

操作是在命令行里完成的,命令解释:
-alias 别名,每个证书都要起自己独有的名字
-keyalg 如果是RSA算法,那么就是RSA
-keystore 填写完整的keystore文件保存路径以及文件名
-keysize 秘钥长度,一般1024
-validity 有效天数

之后会让输入 秘钥库 的密码,连续输入两遍,这里自行设置即可,然后会要求填写一些项目,姓名,组织机构,城市等等,最后会要求输入 证书 的密码,也是连续输入两遍,与证书库密码一样的话可以直接回车确(秘钥库密码和证书密码启动moco要使用)

mock测试(挡板测试)_第12张图片

3、JKS秘钥库使用专用命令再次尝试:

keytool -importkeystore -srckeystore C:\Users\Administrator\.keystore\httpsmoco.cer -destkeystore C:\Users\Administrator\.keystore\httpsmoco.cer -deststoretype pkcs12

操作如下:

mock测试(挡板测试)_第13张图片

这样证书就生成成功,可以去目录查看。操作如下:

mock测试(挡板测试)_第14张图片

4、启动https服务

进入moco-runner-1.3.0-standalone.jar所在目录,然后进入 cmd 界面,输入启动https服务的命令

java -jar moco-runner-1.3.0-standalone.jar https -p 443 -c result.json --https C:\Users\Administrator\.keystore\httpsmoco.cer --cert httpsmoco --keystore httpsmoco

命令解释:
【443】https服务默认开启的是443端口
【C:\Users\Administrator.keystore\httpsmoco.cer】cer证书所在路径
【--cert httpsmoco】秘钥库的密码(在第2步设置的秘钥库口令)
【--keystore httpsmoco】证书/秘钥的密码(在第2步设置的秘钥口令)

你可能感兴趣的:(github,java,开发语言)