顾翔老师的《软件测试技术实战设计、工具及管理》网上购买地址:
https://item.jd.com/34295655089.html
《基于Django的电子商务网站》网上购买地址:
https://item.jd.com/12082665.html
店铺二维码:
来源:http://www.51testing.com
我们如果在命令行发送post请求,会很麻烦,效率不高。同时也不方便接口的管理,所以一些优秀的接口测试工具应运而出,postman就是绕不开的话题之一
1. 主要功能
postman主要帮我们干了三件事:
把相关集合放到一个集合当中方便管理
对指定接口发送请求
下面我们使用postman工具操作getList接口:
新建一个集合TestDemo,用来管理相关用例
添加一条请求(addRequest)
编辑请求,修改url,请求方式和参数
发送请求查看结果
以上步骤,是postman的主要功能,也是我们前三个小节一直叨念的部分,只不过通过postman来完成,下面是前面没有讲到过的东西----断言
断言
按照截图打开断言区域:
我们按照用例来设置断言:
设计用例(功能):
是否按照创建时间倒序排列
是否返回指定的条数
pm.test("是否含有create_time节点", function () { pm.expect(pm.response.text()).to.include("create_time");});pm.test("返回2条数据", function () { var jsonData = pm.response.json(); var num = jsonData.result.length; pm.expect(num).to.eql(2);});pm.test("时间倒序排列", function () { var jsonData = pm.response.json(); var time1 = jsonData.result[0].create_time; var time2 = jsonData.result[1].create_time; pm.expect(time2).to.be.below(time1);});
发送请求后,店家Test Results查看结果如下:
红色的提示表示断言失败,这个时候我们需要修改接口代码(getList_fix.php):
<?php // 告诉浏览器返回为json类型header('Content-Type:application/json; charset=utf-8');// 获取参数$page = $_GET['page'];$size = $_GET['size'];/** * 在真正的开发过程中,会操作数据库,我们暂时用一个多维数组代替 */$data_list = array( array('id'=>1,'name'=>'成龙','create_time'=>1587890857000), array('id'=>2,'name'=>'周润发','create_time'=>1587804457000), array('id'=>3,'name'=>'周星驰','create_time'=>1587690857000), array('id'=>4,'name'=>'李连杰','create_time'=>1587590857000), array('id'=>5,'name'=>'梁朝伟','create_time'=>1587490857000), array('id'=>6,'name'=>'张曼玉','create_time'=>1587390857000), array('id'=>7,'name'=>'惠英红','create_time'=>1587290857000), array('id'=>8,'name'=>'林青霞','create_time'=>1587190857000), array('id'=>9,'name'=>'王祖贤','create_time'=>1587090857000), array('id'=>10,'name'=>'巩俐','create_time'=>1586890857000));foreach($data_list as $key=>$value){ if($key >= ($page -1) * $size && $key < $page * $size){ $temp[] = $value; }}// 组织的数据$data = array( 'code' => 200, 'msg' => '请求成功', 'result' => $temp);// PHP数组转json$rst = json_encode($data,JSON_UNESCAPED_UNICODE);// 打印结果echo $rst;
按照上面的流程,创建一个请求,添加断言,在此访问返回结果如下:
返回绿色说明测试通过
稳重的断言代码是由js编写,大家书写断言的时候不要被代码吓到,因为postman已经提供了比较丰富的断言片段,直接点击即可生成。
————————————————————
顾老师课程欢迎报名
软件安全测试
https://study.163.com/course/courseMain.htm?courseId=1209779852&share=2&shareId=480000002205486
接口自动化测试
https://study.163.com/course/courseMain.htm?courseId=1209794815&share=2&shareId=480000002205486
DevOps 和Jenkins之DevOps
https://study.163.com/course/courseMain.htm?courseId=1209817844&share=2&shareId=480000002205486
DevOps与Jenkins 2.0之Jenkins
https://study.163.com/course/courseMain.htm?courseId=1209819843&share=2&shareId=480000002205486
Selenium自动化测试
https://study.163.com/course/courseMain.htm?courseId=1209835807&share=2&shareId=480000002205486
性能测试第1季:性能测试基础知识
https://study.163.com/course/courseMain.htm?courseId=1209852815&share=2&shareId=480000002205486
性能测试第2季:LoadRunner12使用
https://study.163.com/course/courseMain.htm?courseId=1209980013&share=2&shareId=480000002205486
性能测试第3季:JMeter工具使用
https://study.163.com/course/courseMain.htm?courseId=1209903814&share=2&shareId=480000002205486
性能测试第4季:监控与调优
https://study.163.com/course/courseMain.htm?courseId=1209959801&share=2&shareId=480000002205486
Django入门
https://study.163.com/course/courseMain.htm?courseId=1210020806&share=2&shareId=480000002205486
啄木鸟顾老师漫谈软件测试
https://study.163.com/course/courseMain.htm?courseId=1209958326&share=2&shareId=480000002205486