![v2-629e263c0f3ee17f4965d76fc5c86cd2_1440w.jpg?source=172ae18b](http://img-02.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-629e263c0f3ee17f4965d76fc5c86cd2_1440w.jpg?source=172ae18b)
接口测试之postman使用
简介:Postman是chrome的一个插件,是常用的接口测试工具,能够发送任何类型的http请求,支持GET/PUT/POST/DELETE等。Postman非常简单易用,通过填写URL,header,body等就可以发送一个请求,用来测试api接口非常方便。
安装
可以通过chrome插件或者Mac app进行安装。
发送请求
![v2-5ac0221ad243648edcc1b3442fc74619_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-5ac0221ad243648edcc1b3442fc74619_b.jpg)
![v2-1283ad7402b3aa544ca6b4d4a6f89861_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-1283ad7402b3aa544ca6b4d4a6f89861_b.jpg)
![v2-c61594eb7ff50051b55ea387fa7aebc3_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-c61594eb7ff50051b55ea387fa7aebc3_b.jpg)
![v2-9e6650864729e8f54fa01afd17ed8095_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-9e6650864729e8f54fa01afd17ed8095_b.jpg)
Postman还支持一键运行整个集合内的测试,点击Start Test按钮,Postman将会开始运行某个集合内的所有request,并显示运行的结果:
![v2-dffe22ce412e2e2c626e521eb03413ca_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-dffe22ce412e2e2c626e521eb03413ca_b.jpg)
![v2-5a42b307bb13f0a83bd7e1c06f5e299f_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-5a42b307bb13f0a83bd7e1c06f5e299f_b.jpg)
![v2-bbe4245c6370830a4f8eb5b2744d56ce_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-bbe4245c6370830a4f8eb5b2744d56ce_b.jpg)
![v2-6d93bf6f0f5a3620f6f589d898482f7d_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-6d93bf6f0f5a3620f6f589d898482f7d_b.jpg)
环境变量添加完成后,测试eapi接口时,在你要使用的变量名上附上双花括号,如:{{URL}}。在postman右上角选择要使用的环境环境即可:
![v2-2f11133fcce9f42bcf33502adf64ba81_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-2f11133fcce9f42bcf33502adf64ba81_b.jpg)
全局变量和动态变量
![v2-e945bc71f5ef5622571063901703c997_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-e945bc71f5ef5622571063901703c997_b.jpg)
![v2-c8f1782ff20c4f684cd3e9457cbd4116_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-c8f1782ff20c4f684cd3e9457cbd4116_b.jpg)
![v2-35df7655357338773c5213d2f1fbe67b_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-35df7655357338773c5213d2f1fbe67b_b.jpg)
![v2-ccb2e36e67940a40a860ad7be0a8bd58_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-ccb2e36e67940a40a860ad7be0a8bd58_b.jpg)
Test Scripts
Postman的Tests标签可以用来为每个request编写测试代码,用来测试分析response,例如:检查状态码,检查响应串,验证JSON,检查header,限制应答时间等。测试代码会在发送request并且接收到responses后执行。执行后结果被显示在一个tab里,并在头部显示多少test通过了测试。
我们来看一些测试分析reponse的测试实例:
设置环境变量
postman.setEnvironmentVariable("key", "value");
获取环境变量
postman.getEnvironmentVariable("key");
检查reponse的body中是否含有字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
将XML的body转换为Json对象
var jsonObject = xml2Json(responseBody);
response的响应时间小于200ms
tests["Response time is less than 200ms"] = responseTime < 200;
状态码为200
tests["Status code is 200"] = responseCode.code === 200;
检查Json的值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
内容类型存在(区分大小写)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
示例:测试点睛eapi添加计划接口,编写测试脚本,执行request后,测试结果会出现在Tests tab下:
![v2-d28d078051ee6123c56a3b55c4a00320_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=013240a5-3230-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-d28d078051ee6123c56a3b55c4a00320_b.jpg)
小结
Postman确实是一个很有用的接口测试工具,当然还有很多其他框架以及工具可用于接口自动化测试,大家可以各取所需。以上便是作者在工作实践中对postman用法的总结,希望对大家能有点帮助。