Test examples

样例来源:

https://learning.getpostman.com/docs/postman/scripts/test_examples/

Test scripts are run after a request is sent and a response has been received from the server.

Let’s look at some examples of Postman tests. Most of these are available as snippets inside Postman. You can have as many tests as you want for a request.

Setting an environment variable

pm.environment.set("variable_key", "variable_value");

Setting a nested object as an environment variable

var array = [1, 2, 3, 4];
pm.environment.set("array", JSON.stringify(array, null, 2)); var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
pm.environment.set("obj", JSON.stringify(obj));

Getting an environment variable

pm.environment.get("variable_key");

Getting an environment variable (whose value is a stringified object)

// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.

var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));

Clear an environment variable

pm.environment.unset("variable_key");

Set a global variable

pm.globals.set("variable_key", "variable_value");

Get a global variable

pm.globals.get("variable_key");

Clear a global variable

pm.globals.unset("variable_key");

Get a variable

This function searches for the variable across globals and the active environment.

pm.variables.get("variable_key");

Check if response body contains a string

pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

Check if response body is equal to a string

pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});

Check for a JSON value

pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});

Content-Type is present

pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});

Response time is less than 200ms

pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

Status code is 200

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

Code name contains a string

pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});

Successful POST request status code

pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

Use TinyValidator for JSON data

var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123]; pm.test('Schema is valid', function() {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});

Decode base64 encoded data

var intermediate,
base64Content, // assume this has a base64 encoded value
rawContent = base64Content.slice('data:application/octet-stream;base64,'.length); intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
pm.test('Contents are valid', function() {
pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
});

Send an asynchronous request

This function is available as both a pre-request and test script.

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});

Convert XML body to a JSON object

var jsonObject = xml2Json(responseBody);

postman 官方 test 脚本样例的更多相关文章

  1. shell 脚本样例

    1  解压文件,移动文件,删除特定目录 #!/bin/bash pa=$(cd ``; pwd) //获得当前目录的绝对路径 v_dir=${pa} mkdir ${v_dir} dirDist=${ ...

  2. ShardingSphere 知识库更新 | 官方样例集助你快速上手

    Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...

  3. Nginx完整配置配置样例【官方版】

    我们主要参考nginx官方给出的完整配置的样例: https://www.nginx.com/resources/wiki/start/topics/examples/full/# 完整摘录如下: n ...

  4. go官方的http.request + context样例

    go官方的http.request + context样例 https://github.com/DavadDi/go_study/blob/master/src/httpreq_context/ma ...

  5. Oracle简单脚本演示样例

    Oracle简单脚本演示样例 1.添加表 --改动日期:2014.09.21 --改动人:易小群 --改动内容:新增採购支付情况表 DECLARE VC_STR           VARCHAR2( ...

  6. appium 移动web样例脚本

    简单介绍 这是一个使用appium操作移动端chrome浏览器的样例程序. 样例程序 脚本实现的功能是:打开浏览器-->跳转到百度首页-->输入appium-->点击搜索按钮--&g ...

  7. shell脚本实例-菜单样例

    1.9.1 实例需求 用户在进行Linux系统管理的过程中,经常需要用到查看进程的信息.用户的信息等常用的功能.本例针对这一需求,使用shell编程实现基本的系统管理 功能.通过本程序,可以按照要求实 ...

  8. Ajax框架,DWR介绍,应用,样例

    使用Ajax框架 1. 简化JavaScript的开发难度 2. 解决浏览器的兼容性问题 3. 简化开发流程 经常使用Ajax框架 Prototype 一个纯粹的JavaScript函数库,对Ajax ...

  9. [hadoop系列]Pig的安装和简单演示样例

    inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...

随机推荐

  1. WorldWind源码剖析系列:星球类World

    星球类World代表通用的星球类,因为可能需要绘制除地球之外的其它星球,如月球.火星等.该类的类图如下. 需要说明的是,在WorldWind中星球球体的渲染和经纬网格的渲染时分别绘制的.经纬网格的渲染 ...

  2. leetcode322—Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  3. Java使用PropertyDescriptor获取实体类中私有属性的值,并给私有属性赋值

    大家都知道Java类中的私有的(private)属性是获取不到的(即使使用继承依然获取不到),那如果非要获取私有属性的值怎么办呢?一般的做法是将该java类封装称为一个JavaBean,即封装该私有属 ...

  4. Jmeter—开篇

    Jmeter以开源.轻便著称,做接口测试.性能测试都可以借助Jmeter,从这篇开始记录我使用到的Jmeter功能. 安装 Jmeter官网:http://jmeter.apache.org/ 去官网 ...

  5. HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. echarts 中文API

    http://echarts.baidu.com/echarts2/doc/doc.html#Toolbox

  7. day32

    今日内容 1.基于TCP协议(通信循环) 2.基于TCP协议(连接循环) 3.粘包问题 4.模拟SSH实现远程执行命令 服务器端 ################################### ...

  8. [Lydsy1805月赛]对称数 BZOJ5361

    分析: 这个题,还是蛮有趣的.考虑,如果l,r区间内的所有数出现奇数次,那么[l-1,r]的抑或和等于所得抑或和. 之后怎么维护呢,主席树维护区间抑或和,记得将每个点附上一个ull级别的随机数,之后抑 ...

  9. Storm 安装部署

    环境要求JDK 1.6+java -versionPython 2.6.6+python -V ZooKeeper3.4.5+storm 0.9.4+ 单机模式上传解压 $ .tar.gz $ cd ...

  10. mfc 基类与子类

    基类(父类) 派生类(子类) 一.基类(父类) 基类(又称为父类,基类与派生类是相对的关系! 通过继承机制,可以利用已有的数据类型来定义新的数据类型.所定义的新的数据类型不仅拥有新定义的成员,而且还同 ...