为什么定义code_str是参考返回值code获取的?
来源:8-7 项目实战之如何用代码判断两个json格式是否发生变化
qq_岁小草_0
2020-06-08
“success”和“error”都是从接口返回数据res中得来的,如果res本身就是错误的呢?
比如一条case预期结果应该是请求成功的,但是这条case请求失败了,所以res中的code不是1000,则code_str就定义为“error”了。随后对比了result.json中“error”对应的value格式是一致的,然后就返回测试通过。实际上这条case一开始就失败了啊(请求失败)
写回答
1回答
-
Mushishi
2020-06-08
没有太理解哈,但是根据我理解到的:1、我们的判断接口返回是否成功有多种方式这个根据你实际项目出发 2、对比服务端返回接口的结构体是比较常见的一种方式。看下面接口
{"result":0,"data":{"globalTopBanner":[{"id":"2937","name":"2020618\u6d3b\u52a8","column_id":"284","description":"","pic":"5ed9c2b60916cf9351200096","links":"https:\/\/www.imooc.com\/act\/june2020?utm_source=imooc&utm_campaign=global_top_banner","type":"99","type_id":"0","create_time":"1591329489","uid":"2001507","is_open":"0","seqid":"1","status":"0","start_time":"1591545600","end_time":"1592582399","skillid_list":"","coupon_id":""}],"globalRightFloat":[],"globalBottomBanner":[],"readTopBanner":[],"courseIndexBanner":[{"id":"2936","name":"\u5b98\u65b9\u63a8\u8350","column_id":"388","description":"1","pic":"5ed8bcb1094bb87b00000000","links":"https:\/\/www.imooc.com\/course\/list","type":"99","type_id":"0","create_time":"1591262544","uid":"6595952","is_open":"0","seqid":"1","status":"0","start_time":"0","end_time":"0","skillid_list":"","coupon_id":""}],"coursechapterlistRight":[{"id":"2948","name":"618","column_id":"245","description":"","pic":"5ed9e310091a9f5a06400320","links":"https:\/\/www.imooc.com\/act\/june2020?utm_source=imooc&utm_campaign=course_chapterlist_right","type":"99","type_id":"0","create_time":"1591337764","uid":"2001507","is_open":"0","seqid":"0","status":"0","start_time":"1591545600","end_time":"1592582399","skillid_list":"","coupon_id":""}],"courseVideoRight":[{"id":"2949","name":"618","column_id":"246","description":"","pic":"5ed9e310091a9f5a06400320","links":"https:\/\/www.imooc.com\/act\/june2020?utm_source=imooc&utm_campaign=course_video_right","type":"99","type_id":"0","create_time":"1591337937","uid":"2001507","is_open":"0","seqid":"0","status":"0","start_time":"1591545600","end_time":"1592582399","skillid_list":"","coupon_id":""}],"courseVideoWendalistRight":[],"articleReply":[{"id":"2600","name":"\u624b\u8bb0\u8be6\u60c5\u8bc4\u8bba\u5e7f\u544a","column_id":"334","description":"","pic":"5dcba70c09df050213120400","links":"http:\/\/www.imooc.com\/read\/?mc_marking=68e5df7acf0413d74105f407af34fcdc&mc_channel=sjxqzlgg","type":"99","type_id":"0","create_time":"1573627299","uid":"2947111","is_open":"1","seqid":"0","status":"0","start_time":"0","end_time":"0","skillid_list":"","coupon_id":""}],"articleIndexRight":[{"id":"2411","name":"APP\u4e0b\u8f7d","column_id":"336","description":"","pic":"5dca55d20ae899e903200160","links":"https:\/\/www.imooc.com\/articlehttps:\/\/www.imooc.com\/mobile\/app","type":"99","type_id":"0","create_time":"1565762895","uid":"7157815","is_open":"0","seqid":"1","status":"0","start_time":"0","end_time":"0","skillid_list":"","coupon_id":""}],"articleDetailRight":[{"id":"2412","name":"\u96f6\u57fa\u7840\u5c0f\u767d\u5165\u95e8\uff01","column_id":"339","description":"","pic":"5dfadf66098cef3702800140","links":"https:\/\/class.imooc.com\/list?mc_marking=615ea9e4184f5c589b0c22a76ae7f787&mc_channel=sjggw","type":"99","type_id":"0","create_time":"1565762986","uid":"7157815","is_open":"0","seqid":"0","status":"0","start_time":"0","end_time":"0","skillid_list":"","coupon_id":""}],"courseVideoFinish":[{"id":"2951","name":"618","column_id":"376","description":"","pic":"5ed9e310091a9f5a06400320","links":"https:\/\/www.imooc.com\/act\/june2020?utm_source=imooc&utm_campaign=course_chapterlist_right","type":"99","type_id":"0","create_time":"1591338162","uid":"2001507","is_open":"0","seqid":"0","status":"0","start_time":"1591545600","end_time":"1592582399","skillid_list":"","coupon_id":""}],"courseVideoPause":[{"id":"2877","name":"\u89c6\u9891\u64ad\u653e\u6682\u505c\u5e7f\u544a\uff08\u4f53\u9a8c\u8bfe\uff09","column_id":"378","description":"","pic":"5ea6507109b99bd803480172","links":"https:\/\/www.imooc.com\/act\/onlivelist?mc_marking=d52b3fe15a7f94122243e2ff06a4c0e3&mc_channel=jybmfkzty","type":"99","type_id":"0","create_time":"1586941183","uid":"2001507","is_open":"1","seqid":"0","status":"0","start_time":"0","end_time":"0","skillid_list":"","coupon_id":""}],"courseTryChapterListRight":[{"id":"2950","name":"618","column_id":"380","description":"","pic":"5ed9e310091a9f5a06400320","links":"https:\/\/www.imooc.com\/act\/june2020?utm_source=imooc&utm_campaign=course_chapterlist_right","type":"99","type_id":"0","create_time":"1591338091","uid":"2001507","is_open":"0","seqid":"0","status":"0","start_time":"1591545600","end_time":"1592582399","skillid_list":"","coupon_id":""}],"courseTryVideoRight":[{"id":"2953","name":"618","column_id":"381","description":"","pic":"5ed9e310091a9f5a06400320","links":"https:\/\/www.imooc.com\/act\/june2020?utm_source=imooc&utm_campaign=course_chapterlist_right","type":"99","type_id":"0","create_time":"1591338310","uid":"2001507","is_open":"0","seqid":"0","status":"0","start_time":"1591545600","end_time":"1592582399","skillid_list":"","coupon_id":""}],"courseTryVideoFinish":[{"id":"2952","name":"618","column_id":"377","description":"","pic":"5ed9e310091a9f5a00000000","links":"https:\/\/www.imooc.com\/act\/june2020?utm_source=imooc&utm_campaign=course_chapterlist_right","type":"99","type_id":"0","create_time":"1591338232","uid":"2001507","is_open":"0","seqid":"0","status":"0","start_time":"1591545600","end_time":"1592582399","skillid_list":"","coupon_id":""}],"courseTryVideoPause":[{"id":"2873","name":"\u514d\u8d39\u8bfe\u64ad\u653e\u6682\u505c\u5e7f\u544a\uff08\u8bd5\u542c\u8bfe\uff09","column_id":"379","description":"","pic":"5ea6508b0934363b03480172","links":"https:\/\/www.imooc.com\/act\/onlivelist?mc_marking=990f723fa7627188261c10030aa5491c&mc_channel=txkmfkzty","type":"99","type_id":"0","create_time":"1586940103","uid":"2001507","is_open":"1","seqid":"0","status":"0","start_time":"1588082400","end_time":"1596643259","skillid_list":"","coupon_id":""}],"now_time":1591592029},"msg":"\u6210\u529f"}
你看一个正常服务端返回的机构体应该会是这种。他包含了最外层的result的状态、data数据、message提示,这是最起码的几个。但是当我们一个接口如果遇见问题或者处理失败,这个时候他的data数据肯定是没有的,他的message也就:xxx失败了的提示,code也是一些错误提示吗,如果只是判断成功与否我们是不是判断code就行,但是我们如果要看整个接口是否有变更,那么我们是不是只能看data里面的数据结构,也就是我们的结构体,因为结构体发生了变化肯定代表接口变化了,我们这个时候自动化肯定是去看结构体的key而不是去看value,因为你value机器比太不现实。
10 -
Mushishi
2020-06-19
回答了啊
022020-06-19
相似问题