十大电子商务网站,制作网站建设规划书的结构为,北京城乡建设门户网站,广州市 网站建设建设JSON断言
若服务器返回的Response Body为JSON格式的数据#xff0c;使用JSON断言来判断测试结果是较好的选择。
首先需要根据JSON Path从返回的JSON数据中提取需要判断的实际结果#xff0c;再设置预期结果#xff0c;两者进行比较得出断言结果。
下面首先介绍JSON与JSON…
JSON断言
若服务器返回的Response Body为JSON格式的数据使用JSON断言来判断测试结果是较好的选择。
首先需要根据JSON Path从返回的JSON数据中提取需要判断的实际结果再设置预期结果两者进行比较得出断言结果。
下面首先介绍JSON与JSON Path相关的基础知识。
一、JSON与JSONPath
▲ 什么是JSON
● JSON(JavaScript Object NotationJavaScript对象表示法)是一种轻量级的数据交换格式。
● JSON容易理解便于阅读和编写;同时计算机也易于解析和生成所以JSON有广泛的应用。
JSON基于如下两种结构
1.名称/值对的集合
在各种语言中这可以作为对象(object)、记录(record)、结构(struct)、字典(dictionary)、哈希表(hash table)、键控列表(keyed list)或关联数组(associative array)来实现。
2.值的有序列表
在大多数语言中这是以数组(array)、向量(vector)、列表(list)或序列(sequence)的形式实现的。
JSON用于描述文本数据结构有如下形式
1.对象(object)
对象是一组无序的名称/值对。对象以{(左大括号)开始以}(右大括号)结束。每个名称后面跟着:冒号名称/值对之间用逗号分隔。
比如
{name:zhangsan,sex:1,age:25}
2.数组(Array)
数组是值的有序集合。数组以[(左中括号)开始以](右中括号)结束。值之间用逗号分隔。
比如
{
man:[{name:zhangsan,sex:1,age:21},
{name:lisi,sex:0,age:18},
{name:wangwu,sex:0,age:25}]
}
3.值(value)
值可以是字符串、数字、true/false、null、对象或数组。
4.字符串(string)
字符串是由零个或多个Unicode字符组成的序列用双引号括起来使用反斜杠转义。
字符表示为单个字符串。字符串非常类似于C或Java中的字符串。
5.数字(number)
一系列0-9的数字组合可以为负数或者小数。还可以用e或者E表示为指数形式;数字非常类似于C或Java数字但只是不使用八进制和十六进制格式。
6.空白符(whitespace)
可以在任意成对的语法符号之间插入空白符(包括空格、换行符、回车符、横向制表符)。
现在我也找了很多测试的朋友做了一个分享技术的交流群共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源没人解答问题坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化性能安全测试开发等等方面有一定建树的技术大牛
分享他们的经验还会分享很多直播讲座和技术沙龙
可以免费学习划重点开源的
qq群号110685036 ▲ 什么是JSONPath
我们经常使用XPath来分析、转换以及有选择地从XML文档中提取数据。与XPath类似JOSNPath可以方便从JSON结构中发现和提取数据。
JSONPath中的“根成员对象”总是被称为$无论它是对象还是数组。JSONPath表达式有“dot–notation”(.号)和“bracket–notation”([]号)两种不同的表示风格。
例如
$.store.book[0].title or
$[store][book][0][title]
▲ JSONPath语法元素 ▲ JSONPath实例
{ store: {
book: [
{ category: reference,
author: Nigel Rees,
title: Sayings of the Century,
price: 8.95
},
{ category: fiction,
author: Evelyn Waugh,
title: Sword of Honour,
price: 12.99
},
{ category: fiction,
author: Herman Melville,
title: Moby Dick,
isbn: 0-553-21311-3,
price: 8.99
},
{ category: fiction,
author: J. R. R. Tolkien,
title: The Lord of the Rings,
isbn: 0-395-19395-8,
price: 22.99
}
],
bicycle: {
color: red,
price: 19.95
}
}
} 二、JSON 断言
JSON断言可以对服务器返回的JSON文档进行验证。
JSON断言有两种使用模式
1.根据JSONPath能否在JSON文档中找到路径;
2.根据JSONPath提取值并对值进行验证。
● 若文档格式为非JSON则断言失败;
● 若找不到路径断言失败;
● 若提取值与预期值不一致断言失败。
▲ 配置项
Assert JSON Path exists
用于断言的JSON元素的路径(JSONPath)。
1.Additionally assert value
是否额外验证根据JSONPath提取的值。
● 不勾选验证JSONPath能否在JSON文档中找到路径;
● 勾选验证根据JSONPath提取值是否预期。
2.Match as regular expression
预期值是否可以使用正则表达式。
● 不勾选预期值不能使用正则表达式表示;
● 勾选预期值可以使用正则表达式表示。
Expected Value
预期值。
1.Expect null
● 若验证提取的值为null则勾选此项。
这里有两个地方需要额外注意
a.验证null值还是需要勾选“Additionally assert value”否则验证的是JSONPath能否找到路径;
b.预期值不填表示空字符与null不等价。
2.Invert assertion(will fail if above conditions met)
● 若勾选表示对断言结果取反。
注意
除了null外还有一种特殊的值就是空数组预期值不能不填需要设置为[]
三、应用案例
这里仍以前面介绍过的查询被购买商品的总金额接口为例来讲述JSON断言的用法。
该接口返回的响应数据为JSON故可以使用JSON断言。
▲ 操作步骤
1.对预期结果要验证的项进行参数化
这里验证err_msgresult
将预期结果写入csv文件中
比如
case_name,goods_id,goods_attr,goods_num,error_msg,rslt case1,9,226,3,,6630元 case2,,226,1,没有找到指定的商品或者没有找到指定的商品属性。, case3,9,,1,,2298元 case4,9,226,,2308元 2.添加JSON Assertion并进行配置
JSON断言每次只能断言一个参数因此这里需要添加多个JSON断言。
假设对错误消息与商品金额这个两个返回参数值做断言。
{
err_msg: 没有找到指定的商品或者没有找到指定的商品属性。,
result: ,
qty: 1,
err_no: 1
}
$.err_msg -- 没有找到指定的商品或者没有找到指定的商品属性。
$.qty -- 1
▲ JSON断言配置
1. 断言错误消息 2. 断言商品金额 END今天的分享就到此结束了对你有所帮助的话留下小心心哈
文章转载自: http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.yqgbw.cn.gov.cn.yqgbw.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.8yitong.com.gov.cn.8yitong.com http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.bzgpj.cn.gov.cn.bzgpj.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.zdtfr.cn.gov.cn.zdtfr.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.rsqpc.cn.gov.cn.rsqpc.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.kszkm.cn.gov.cn.kszkm.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.qdlnw.cn.gov.cn.qdlnw.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn