#Postman--接口测试工具Postman的使用

一、什么是Postman?
Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件。其主要功能包括:

(1)模拟各种HTTP requests
从常用的 GET、POST 到 RESTful 的 PUT 、 DELETE …等等。 甚至还可以发送文件、送出额外的 header。

(2)Collection 功能(测试集合)
Collection 是 requests的集合,在做完一個测试的時候, 你可以把這次的 request 存到特定的 Collection 里面,如此一來,下次要做同样的测试时,就不需要重新输入。而且一个collection可以包含多条request,如果我们把一个request当成一个test case,那collection就可以看成是一个test suite。通过collection的归类,我们可以良好的分类测试软件所提供的API.而且 Collection 还可以 Import 或是 Share 出來,让团队里面的所有人共享你建立起來的 Collection。

(3)人性化的Response整理
一般在用其他工具來测试的時候,response的内容通常都是纯文字的 raw, 但如果是 JSON ,就是塞成一整行的 JSON。这会造成阅读的障碍 ,而 Postman 可以针对response内容的格式自动美化。 JSON、 XML 或是 HTML 都會整理成我们可以阅读的格式

(4)内置测试脚本语言
Postman支持编写测试脚本,可以快速的检查request的结果,并返回测试结果

(5)设定变量与环境
Postman 可以自由 设定变量与Environment,一般我们在编辑request,校验response的时候,总会需要重复输入某些字符,比如url,postman允许我们设定变量来保存这些值。并且把变量保存在不同的环境中。比如,我們可能会有多种环境, development 、 staging 或 local, 而这几种环境中的 request URL 也各不相同,但我们可以在不同的环境中设定同样的变量,只是变量的值不一样,这样我们就不用修改我们的测试脚本,而测试不同的环境。

二、安装Postman
(1)下载Postman,下载路径:http://files.cnblogs.com/files/mafly/postman-4.1.2.rar
(2)打开谷歌浏览器,选择“更过工具”->“扩展程序”->“加载已解压的扩展程序”->选择已解压好的Postman安装目录->启用,选择左下方“电脑状态键”->“所有程序”->“Chrome 应用”下的Postman,启动之。

三、使用说明
(1)最常见的是get请求,例如:
在这里插入图片描述
测试接口:

@ResponseBody
@RequestMapping("/hello.action")//未显示声明请求类型时为GET请求
public String hello(HttpServletResponse response, HttpServletRequest request) throws Exception {
	String userid = request.getHeader("userid");
	System.out.println("userid:"+userid);
	if(userid == null){
		throw new Exception("This is a exception");
	}
	return "hello world!";
}

(2)带入参的Get请求
在这里插入图片描述
测试接口:

@ResponseBody
@RequestMapping("/testInfo.action")
public String testInfo(HttpServletRequest request,@RequestParam("username") String username) throws Exception {
System.out.println("username:"+username);
	if(userid == null){
		throw new Exception("This is a exception");
	}
	return "this is a request of testInfo.action";
}

对于get请求的入参,我们可以使用@RequestParam注解来接收入参值。

(3)Postman中Headers传参与后台处理
如果是对于用户请求,通常Headers传参用于用户唯一标识符,请求传入了它服务器才会对请求作出响应。我们可以先在Postman中传入headers参数:
在这里插入图片描述
在后台接口中可以使用request的getHeader(参数)来获取header参数值,例如:

@ResponseBody
@RequestMapping("/testInfo.action")
public String testInfo(HttpServletRequest request,@RequestParam("username") String username) throws Exception {
	String userid = request.getHeader("userid");
	System.out.println("userid:"+userid);
	System.out.println("username:"+username);
	if(userid == null){//header参数检查
		throw new Exception("This is a exception");
	}
	return "this is a request of testInfo.action";
}

(4)post请求
对于post请求,我们既可以传入参数,也可以不传入参数:
在这里插入图片描述
对应接口为:

@ResponseBody
@RequestMapping(value="/testPostInfo.action",method={RequestMethod.POST})
public String testPostInfo(HttpServletRequest request,@RequestParam("username") String username) throws Exception {
	String userid = request.getHeader("userid");
	System.out.println("userid:"+userid);
	System.out.println("username:"+username);
	if(userid == null){
		throw new Exception("This is a exception");
	}
	return "this is a request of testPostInfo.action";
}

(5)技巧
A.传json数据时,需要选择post方式且Headers中需要传入Content-Type: application/json,在body一栏选中raw。
B.list用[]表示,对象使用{}表示,map用{key:value}来表示。
C.常见错误及其原因:
400:400-错误的请求。一般是需要传参的URL的传参不完整或没有传参。例如,如果username参数没有传,那么就会报告HTTP Status 400 - Required String parameter ‘username’ is not present类似的错误。
404:后台没有这个方法或者前台url传输错误不匹配,会报告一个名为HTTP Status 404的错误。
405:用来访问本页面的HTTP谓词不被允许(方法不被允许),例如,后台的POST方法使用GET方式进行请求,会获得一个名为HTTP Status 405 - Request method ‘GET’ not supported。
500:后台逻辑有问题。

猜你喜欢

转载自blog.csdn.net/yzh18373476791/article/details/85058792
今日推荐