前言

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

编写测试脚本

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

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

// example using pm.response.to.have
pm.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

Postman—测试脚本的更多相关文章

  1. postman常用测试脚本

    测试脚本: 设置环境变量 var jsonData = JSON.parse(responseBody); postman.setGlobalVariable("5KMST", j ...

  2. postman(九):postman接口测试脚本集成到jenkins

    本篇的目的是实现使用jenkins远程执行postman接口测试脚本 准备工作:一台linux服务器(可以用虚拟机搭建一个),linux服务器上安装好node.js.newman,部署好jenkins ...

  3. 三、postman测试断言设置语法

    postman的基本介绍跟基本的用法,在此就不做过多赘述,主要是受限于时间,网上有很多大神总结的已经算是很详尽了,给出链接并感谢大神的辛苦与奉献: postman基本用法:http://www.jia ...

  4. 使用Postman测试请求

    Postman是什么 在开发或者调试网络程序或者是网页B/S模式的程序的时候,需要一些方法来跟踪网页请求,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具. 网页调试工具Postm ...

  5. postman测试实例--断言

    postman测试实例--断言 让我们来看看postman测试的一些例子. 其中大部分是作为内部postman片段. 大多数测试是为单行的JavaScript语句一样简单. 只要你想一个请求,你可以有 ...

  6. 使用 Postman 测试你的 API

    使用 Postman 测试你的 API Intro 最近想对 API 做一些自动化测试,看了几个工具,最后选择了 postman,感觉 postman 的设计更好一些,我们可以在请求发送之前和请求获取 ...

  7. API测试之Postman使用全指南(原来使用 Postman测试API如此简单)

    Postman Postman是一个可扩展的API开发和测试协同平台工具,可以快速集成到CI/CD管道中.旨在简化测试和开发中的API工作流. Postman 工具有 Chrome 扩展和独立客户端, ...

  8. Postman接口测试脚本编写规范

    Postman接口测试脚本编写规范 1.前言 2.名词解释 3.接口测试脚本规范 3.1接口测试脚本编写的规范 3.2 Postman使用规范 4.单个接口测试 5.整个流程的开发过程 1.前言 本规 ...

  9. SPF邮件伪造漏洞测试脚本

    测试脚本: # -*- coding: utf-8 -*- import socket,select,base64,os,re,time,datetime class mail: def __init ...

随机推荐

  1. cmake-file

    file: File manipulation command. file(WRITE filename "message to write"... ) file(APPEND f ...

  2. 《Forward团队-爬虫豆瓣top250项目-代码设计规范》

    成员:马壮,李志宇,刘子轩,年光宇,邢云淇,张良 1.缩进采用一个Tab键 2.大括号:如: if (条件){ 语句: } 3.分行:不把多条语句放在同一行 4.变量命名:统一用英文 5.注释:注释前 ...

  3. noip第11课作业

    1.    数字比较 定义一个函数check(n,d),让它返回一个布尔值,如果数字d在正整数n的某位中出现则返回true,否则返回false. 例如:check(325719,3)==true:ch ...

  4. 对C++里面 的知识积累:

    unique()[去重函数] unique()是C++标准库函数里面的函数,其功能是去除相邻的重复元素(只保留一个),所以使用前需要对数组进行排序 上面的一个使用中已经给出该函数的一个使用方法,对于长 ...

  5. svg transform

    看了这个页面的说明:http://www.2cto.com/kf/201301/186980.html 总结如下:transform包括:translate(tx,ty),scale(sx,sy),r ...

  6. 团队项目(第三周)—GG队

    需求改进&系统设计 队员 学号 叶尚文(队长) 3116008802 蔡晓晴 3216008808 杜婷萱 3216008809 龙剑初 3116004647 于泽浩 3116004661 一 ...

  7. Moving XML/BI Publisher Components Between Instances

    As it is well known fact that XMLPublisher stores the metadata and physical files for templates and ...

  8. Delphi Dll 动态调用例子(3)-仔细看一下

    http://blog.163.com/bxf_0011/blog/static/35420330200952075114318/ Delphi 动态链接库的动态和静态调用 为了让人能快速的理解 静态 ...

  9. Nginx an upstream response is buffered to a temporary file

    1.错误日志:warn:an upstream response is buffered to a temporary file 解决办法:增加fastcgi_buffers 8 4K;     fa ...

  10. (php)实现万年历

    <?php //修改页面编码 header("content-type:text/html;charset=utf-8"); //获取当前年 $year=$_GET['y'] ...