postman 官方 test 脚本样例
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 脚本样例的更多相关文章
- shell 脚本样例
1 解压文件,移动文件,删除特定目录 #!/bin/bash pa=$(cd ``; pwd) //获得当前目录的绝对路径 v_dir=${pa} mkdir ${v_dir} dirDist=${ ...
- ShardingSphere 知识库更新 | 官方样例集助你快速上手
Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...
- Nginx完整配置配置样例【官方版】
我们主要参考nginx官方给出的完整配置的样例: https://www.nginx.com/resources/wiki/start/topics/examples/full/# 完整摘录如下: n ...
- go官方的http.request + context样例
go官方的http.request + context样例 https://github.com/DavadDi/go_study/blob/master/src/httpreq_context/ma ...
- Oracle简单脚本演示样例
Oracle简单脚本演示样例 1.添加表 --改动日期:2014.09.21 --改动人:易小群 --改动内容:新增採购支付情况表 DECLARE VC_STR VARCHAR2( ...
- appium 移动web样例脚本
简单介绍 这是一个使用appium操作移动端chrome浏览器的样例程序. 样例程序 脚本实现的功能是:打开浏览器-->跳转到百度首页-->输入appium-->点击搜索按钮--&g ...
- shell脚本实例-菜单样例
1.9.1 实例需求 用户在进行Linux系统管理的过程中,经常需要用到查看进程的信息.用户的信息等常用的功能.本例针对这一需求,使用shell编程实现基本的系统管理 功能.通过本程序,可以按照要求实 ...
- Ajax框架,DWR介绍,应用,样例
使用Ajax框架 1. 简化JavaScript的开发难度 2. 解决浏览器的兼容性问题 3. 简化开发流程 经常使用Ajax框架 Prototype 一个纯粹的JavaScript函数库,对Ajax ...
- [hadoop系列]Pig的安装和简单演示样例
inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...
随机推荐
- MetaMask/Website
https://github.com/MetaMask/Website 将这个包下载下来之后运行npm install出现下面的问题 gyp: No Xcode or CLT version dete ...
- java读写properties配置文件方法
1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...
- Android 将系统的back键模拟成为home键的功能
@Override public void onBackPressed() { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFl ...
- centos 7 163 yum 源 python 2.7.5
安装 repo 源. repo 源一般包括 base, updates, Extras 三部分. $ cd /etc/yum.repos.d/ $ wget http://mirrors.163.co ...
- Debuggex – 超好用的正则表达式可视化调试工具
正则表达式通常被用来检索或替换符合某个模式的文本内容,编写正则是开发人员的必备技能.简单的正则表达式一下就能看懂含义,但是复杂的正则理解起来就很困难了.有了这款可视化的正则调试工具,以后编写正则表达式 ...
- React等开发工具记录
React Native :React 起源于 Facebook 的内部项目,结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生 ...
- MyBatis在Oracle中插入数据并返回主键的问题解决
引言: 在MyBatis中,希望在Oracle中插入数据之时,同一时候返回主键值,而非插入的条数... 环境:MyBatis 3.2 , Oracle. Spring 3.2 SQL Snipp ...
- Docker服务器的图形显示方案
问题描述:一般docker实操时都是作为服务器,以字符方式交互,非常不方便.本人尝试各种图形解决方案,最终找到完美方案. 最初本人尝试过VNC和SSH方式,最终被否定了.1, 本来docker服务器是 ...
- 有关java(初学笔记)
JAVA的主要优势:跨平台性,可以在Linux,windows,mac三个系统上运行. 跨平台的核心:JAVA虚拟机--JVM 原理就是将Java语言在这个系统上翻译.JAVA在jvm上运行,jvm进 ...
- 20155328 《网络对抗》 实验九 Web安全
20155328 <网络对抗> 实验九 Web安全 基础 实验过程记录 在实验开始之前先把webgoat的jar包放到home目录下.打开终端,用命令java -jar webgoat-c ...