Postman—测试脚本

前言

对于Postman中的每个请求,我们都可以使用JavaScript语言来开发测试脚本。这也就好比单元测试。我们先看看Postman的相关界面:

编写测试脚本

Postman测试脚本本质上是在发送请求后执行的JavaScript代码,我们可以通过访问pm.response对象获取服务器返回的报文。

以下是一些测试脚本样例:

1// example using pm.response.to.have 2pm.test("response is ok", function () { pm.response.to.have.status(200); }); // example using pm.expect() pm.test("environment to be production", function () { pm.expect(pm.environment.get("env")).to.equal("production"); }); // example using response assertions pm.test("response should be okay to process", function () { pm.response.to.not.be.error; pm.response.to.have.jsonBody(""); pm.response.to.not.have.jsonBody("error"); }); // example using pm.response.to.be* pm.test("response must be valid and have a body", function () { // assert that the status code is 200 pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants // assert that the response has a valid JSON body pm.response.to.be.withBody; pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed });

我们可以根据我们的需要,添加各种各样的测试案例。

沙箱

之前在《Postman—脚本介绍》这篇文章中已经说到了,Postman中的脚本是在一个沙箱环境中运行的。这个沙箱环境和Postman本身运行的环境是完全隔离开的,也就是说,这个沙箱环境给脚本的执行提供了一个上下文环境。这个沙箱环境本身就集成了很多的工具方法,我们可以在我们的测试脚本中直接使用这些工具方法。具体的可以参考以下文档:

代码片段

为了更提高使用者的测试脚本编写效率,Postman提供了很多的代码片段,我们可以直接使用这些已经写好的代码片段来完成测试脚本的编写。

查看结果

每次运行请求时Postman都会运行测试。当然,我们可以选择不查看测试结果!

测试结果显示在响应查看器下的“Test Results”选项卡中。选项卡标题显示通过了多少测试.

参考:https://www.jellythink.com/archives/179

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Postman 使用方法详细介绍

1,下载安装:https://www.getpostman.com/apps2,打开Postman,如图所示:!(https://oscimg.oschina.net/oscnet/00f434cd831f2f74fea6f6d7b86bc46a751.png)3,创建一个接口项目!(https://oscimg.oschina.

Postman—测试脚本 - HelloWorld