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. 剑指offer.从未到头打印链表

    输入一个链表的头结点,按照 从尾到头 的顺序返回节点的值.返回的结果用数组存储. 样例 输入:[2, 3, 5] 返回:[5, 3, 2] 1.使用栈. class Solution { public ...

  2. Android Fragment(二)

    废话:在上一篇的博客中我们给出了Fragment的简单介绍,这一片博客给大家介绍一下Fragment到底该怎样用.主要都用在哪方面等等. 需求:现有一个界面,要求,竖屏时界面的背景颜色为红色,横屏时界 ...

  3. C3P0配置实战

    C3P0: 一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. 默认情况下(即没有配置连接池的 ...

  4. Windows Server2003 IIS服务器安全配置整理

    一.系统的安装   1.按照Windows2003安装光盘的提示安装,默认情况下2003没有把IIS6.0安装在系统里面.2.IIS6.0的安装 开始菜单—>控制面板—>添加或删除程序—& ...

  5. Exp7:网络欺诈防范

    Exp7:网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击? 答:同一局域网下,以及各种公共网络. 在日常生活工作中如何防范以上两攻击方法? 答: 使用最新版本的DNS服务器 ...

  6. 20155227《网络对抗》Exp6 信息收集与漏洞扫描

    20155227<网络对抗>Exp6 信息收集与漏洞扫描 实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 基础问题回答 哪些组织负责DNS,IP的管理. 全球根服务器均由美国政府 ...

  7. 20155321 《网络攻防》 Exp5 MSF基础应用

    20155321 <网络攻防> Exp5 MSF基础应用 基础问题 用自己的话解释什么是exploit,payload,encode 关于exploit,我觉得exploit是利用一些工具 ...

  8. Ubuntu中程序部署时无法加载动态库的解决方法

    Ubuntu下修改环境变量的三种方法 添加环境变量无法解决,可尝试如下操作: sudo vim /etc/ld.so.conf 在ld.so.conf中加入动态库的目录... 然后 sudo ldco ...

  9. CS229笔记:支持向量机

    考虑一个分类问题,用\(1\)表示正类标签,用\(-1\)表示负类标签,引入假设函数\(h\): \[ \begin{align*} g(z) &= \begin{cases} 1 & ...

  10. 微信小程序之分享或转发功能(自定义button样式)

    小程序页面内发起转发 通过给 button 组件设置属性open-type="share",可以在用户点击按钮后触发 Page.onShareAppMessage 事件,如果当前页 ...