25. Postman use

Postman download and install

Whether at the interface testers and developers a high probability are not open around a tool, and that is Postman. Of course, there may be some interface test tools such as soapUI, Jmeter, Robot Framework and so on. The four tools I have used, by comparison, is relatively easy to use Postman, so there will be written on a bit about the use of Postman.

The benefits of using the tool is convenient because in practice we will combine the use of tools and code. We visited Postman's official website , click download, of course, Postman also points 32 and 64, Tell me what you can choose according to their own computer operating system. As shown below:

Installation process is relatively simple, basically is to keep clicking Next, after the installation is complete, we can choose to skip the registration or registration, where we choose to skip the registration. Then you can see Postman main interface, as shown below:

Send an HTTP request

Here, the project has been set up real good, the database environment has been configured, interface documentation has been provided, Postman has also been installed, what is not said, the first wave of the interface to the test. Before using Postman do interface testing, make sure that more than a few have built a good environment.

Login using Postman test

The interface test documentation we can get, the request address is localhost: port number / login, because we are using port 8001 to start the project, the request address is http: // localhost: 8001 / login, request parameters are username, password, request method is the POST, so the input interface Postman in the following, as shown below:

By returning the value we can see that we have a successful login, because here we enter the correct interface address parameters (super administrator username we create is admin, password is admin111111.), Requesting the way, if we enter the wrong request in what way or request parameter will happen then? As shown below:

Interface login request type error

wrong user name or password

Create a blog interface Postman test

我们根据接口文档可以看到,创建博客需要传入4个参数:id、title、author、content。其实在接口测试的时候,我们最好先设计一下接口测试用例。比如这四个参数正常、异常组合一共有多少种情况,如果我们把每一种情况都测到,那么接口测试应该是做的非常成功的。 下面我将演示部分异常、正常情况。如下图所示

测试标题过长

id类型错误

title为空

请求方法错误

断言

做测试的同学都知道,没有预期结果的测试用例是不能称作测试用例的,postman断言其实也就算是预期结果。那么如何使用postman做断言呢?其实很简单,用到tests 这个模块。如下图所示:

那么我们的查询博客接口如何断言呢?很简单,从响应内容做断言。比如查询一篇博客,响应内容如下:

通过上图我们发现,我们可以断言 status、message、以及响应状态码,那么如何做断言呢?点击Tests,输入如下内容,然后再运行,如下图所示:

var jsonData = JSON.parse(responseBody);

tests["Check respose status value"] = jsonData.status === 1;
//断言status是1


tests["Check respose message value"] = jsonData.message === "success"//断言message是success

pm.test("判断data里面第一个json数据的id为12", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data[0].id).to.eql(12);});


pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});// 断言响应状态码是200

通过上面断言我们发现,断言方式其实还是比较简单的,为什么输入上面这些内容呢?因为我们的返回值是Json格式内容,所以使用JSON.parse(responseBody)得到返回值。jsonData.status对应的是返回值里的status。至于状态码就比较简单了,直接点击,就会自动生成断言,如下图所示:

使用Postman自带脚本

使用Postman生成接口测试脚本,我觉得是Postman 的一个黑科技。那么如何使用呢?也特别简单,当我们运行完测试后,点击code,可以让我们选择我们想要生成的语言代码,比如我们点击Python,然后选择requests,如下图所示:

当然生成的脚本也只是一个参考,我们在实际工作中可能不会这么干。对于生成的脚本可能有点陌生,不过不用着急,我们继续往下看,下面内容就会讲如何使用Python做接口测试了,当我们学会使用Python做接口测试时,再回头看就比较简单了。不过有兴趣的同学,可以先研究一下生成的脚本。

更多Postman的使用可以关注我的博客园-POSTMAN专栏

欢迎关注微信公众号:软件测试汪。软件测试交流群:809111560

Guess you like

Origin www.cnblogs.com/suim1218/p/11014989.html