//https://www.getpostman.com/docs/v6/postman/scripts/test_examples

//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);
Sample data files //JSON files are composed of key/value pairs. Download JSON file
For CSV files, the top row needs to contain variable names. //Download CSV file //Set the request to be executed next
postman.setNextRequest("request_name"); //Stop workflow execution
postman.setNextRequest(null); postman.getResponseHeader(headerName)
/*Test-only: returns the response header with name “headerName”, if it exists. Returns null if no such header exists. Note: According to W3C specifications, header names are case-insensitive. This method takes care of this. */ //will return the same value.
postman.getResponseHeader("Content-type")
postman.getResponseHeader("content-Type")
pm.test("A single user was returned", function () {
pm.expect(pm.response.json().results).to.have.lengthof(1);
}); //Gender tests
pm.test("Gender is male", function () {
pm.expect(pm.response.json().result[0].gender).to.equal("male");
pm.expect(pm.response.json().result[0].gender).to.equal("mr");
}); //National test
pm.test("The user is from the United States", function () {
pm.expect(pm.response.json().result[0].nat).to.equal("US");
}); //----------------------------------------------------
//Here are some examples: // 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
});

test examples/test scripts的更多相关文章

  1. NGUI学习笔记(一)UILabel介绍

    来个前言: 作为一个U3D程序员,自然要写一写U3D相关的内容了.想来想去还是从UI开始搞起,可能这也是最直观同时也最重要的部分之一了.U3D自带的UI系统,也许略坑,也没有太多介绍的价值,那么从今天 ...

  2. react-router+webpack+gulp路由实例

    背景:新项目要开始了,有一种想要在新项目中使用react的冲动,应该也是一个单页面的应用,单页应用就涉及到一个路由的问题.于是最近在网上找了蛮多关于react-router的文章,也遇到了许多的坑,经 ...

  3. SteamVR Unity工具包(VRTK)之激光和移动

    简单激光指针(VRTK_ SimplePointer) 简单指针(Simple Pointer)脚本从控制器尾部发出一个有色光束来模拟激光束.这在场景中指向对象很有用,它能判断所指向的对象以及对象距控 ...

  4. SteamVR Unity工具包(VRTK)之概览和控制器事件

    快速上手 · 克隆仓库  git clone https://github.com/thestonefox/SteamVR_Unity_Toolkit.git · 用Unity3d打开SteamVR_ ...

  5. datatables使用总结篇

    <!doctype html> <html> <head> <meta charset="gbk"/> <meta name= ...

  6. 深入理解Fabric环境搭建的详细过程

    博主之前的文章都是教大家怎么快速的搭建一个Fabric的环境,但是其中大量的工作都隐藏到了官方的脚本中,并不方便大家深入理解其中的过程,所以博主这里就将其中的过程一步步分解,方便大家! 前面的准备工作 ...

  7. 记一次安装Ipython的流程

    这是一个悲伤的安装ipython的过程. 写下来留个教训吧. 也是希望对博友一些帮助吧. 注: 我也写了一篇window下安装bpython的文章(个人感觉bpython要比ipython强大的多), ...

  8. coTurn 运行在Windows平台的方法及服务与客户端运行交互流程和原理

    coTurn是一个开源的STUN和TURN及ICE服务项目,只是不支持Windows.为了在window平台上使用coTurn源码,需要在windows平台下安装Cygwin环境,并编译coTurn源 ...

  9. [uboot] (第四章)uboot流程——uboot编译流程

    http://blog.csdn.net/ooonebook/article/details/53000893 以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为 ...

随机推荐

  1. liunx trac 安装记录

    1,下载地址 http://trac.edgewall.org/   2.安装 apache,python, mysql  3,安装trac (我的是0.12)  tar -zxvf  你下载的安装包 ...

  2. c++ malloc与free

    今天看STL内存配置器的时候,第一级配置器就是直接用malloc.free来管理内存. 而free和malloc都只需要传入或传出一个指针就能分配和释放内存了. 编译器是如何知道,这个指针指向的空间的 ...

  3. Qt编写百度离线版人脸识别+比对+活体检测

    在AI技术发展迅猛的今天,很多设备都希望加上人脸识别功能,好像不加上点人脸识别功能感觉不够高大上,都往人脸识别这边靠,手机刷脸解锁,刷脸支付,刷脸开门,刷脸金融,刷脸安防,是不是以后还可以刷脸匹配男女 ...

  4. QWidget编写的安卓app

    最近为了配套人脸识别的整套设备去检测,特意做了个机关控制app,需要现场修改前端设备和服务器设备的一些参数以便进行检测.qt做一些简单的app还是非常方便的.特意增加了禁用屏保功能.apk文件体验:h ...

  5. css3整理--text-overflow

    text-overflow语法: text-overflow : clip | ellipsis clip:表示不显示省略标记(...),而只是简单的裁切,需要在一定的高度范围内配合overflow: ...

  6. Python Tkinter 学习成果:点歌软件music

    笔者工作业余时间也没什么爱好,社交圈子也小,主要娱乐就是背着自己带电瓶的卖唱音响到住地附近找个人多的位置唱唱KtV. 硬件上点歌就用笔记本电脑,歌曲都是网上下载的mkv格式的含有两个音轨的视频.因此点 ...

  7. rabbitmq在centos 7上的安装

    一.安装步骤 参考了官网文档: http://www.rabbitmq.com/install-rpm.html#package-dependencies 这里大概介绍下. rabbitmq-serv ...

  8. ansible register基础使用讲解

    当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register . 举个例子: 我们需要判断 zip 包是否存在,如果存在了就执行一 ...

  9. [Html5] HTML5 开发手机应用

    上次周例会我给大家做了题目为:<漫游移动平台前端开发>的汇报,现在推荐一些额外的学习资料.          依照目前iPhone/Android 迅速提升市占率的情势来看,未来如果想要在 ...

  10. 解析xml文件的几种技术与Dom4j与sax之间的对比

    一.解析xml文件的几种技术:dom4j.sax.jaxb.jdom.dom 1.dom4j dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常优秀的 ...