较旧的写作邮差测试风格

较旧的Postman测试编写风格依赖于特殊tests对象的设置值。您可以为对象中的元素设置描述性键,然后说明它是真还是假。例如,tests["Body contains user_id"] = responsebody.has("user_id");将检查响应主体是否包含user_id字符串。

您可以根据需要添加任意数量的密钥,具体取决于您要测试的内容。您可以在“ 测试”选项卡下的响应查看器中查看测试结果。选项卡标题显示传递了多少测试,并在此处列出了您在tests变量中设置的键。如果值的计算结果为true,则测试通过。

设置环境变量

postman.setEnvironmentVariable("key", "value");

将嵌套对象设置为环境变量

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

获取环境变量

postman.getEnvironmentVariable("key");

获取环境变量(其值是字符串化对象)

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

var array = JSON.parse(postman.getEnvironmentVariable("array"));
var obj = JSON.parse(postman.getEnvironmentVariable("obj"));

清除环境变量

postman.clearEnvironmentVariable("key");

设置全局变量

postman.setGlobalVariable("key", "value");

获取全局变量

postman.getGlobalVariable("key"); 

清除全局变量

postman.clearGlobalVariable("key");

检查响应主体是否包含字符串

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

将XML主体转换为JSON对象

var jsonObject = xml2Json(responseBody);

检查响应主体是否等于字符串

tests["Body is correct"] = responseBody === "response_body_string";

检查JSON值

var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;

存在Content-Type(不区分大小写的检查)

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.

内容类型存在(区分大小写)

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

响应时间小于200毫秒

tests["Response time is less than 200ms"] = responseTime < 200;

响应时间在特定范围内(包含下限,上限不包括)

tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001); // _ is the inbuilt Lodash v3.10.1 object, documented at https://lodash.com/docs/3.10.1

状态代码是200

tests["Status code is 200"] = responseCode.code === 200;

代码名称包含一个字符串

tests["Status code name has string"] = responseCode.name.has("Created");

成功的POST请求状态代码

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

使用TinyValidator获取JSON数据

var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123]; tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
console.log("Validation failed: ", tv4.error);

解码base64编码数据

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
tests["Contents are valid"] = CryptoJS.enc.Utf8.stringify(intermediate); // a check for non-emptin
================================================================================================================
新版postman断言写法:

设置环境变量

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

将嵌套对象设置为环境变量

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));

获取环境变量

pm.environment.get("variable_key");

获取环境变量(其值是字符串化对象)

// 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"));

清除环境变量

pm.environment.unset("variable_key");

设置全局变量

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

获取全局变量

pm.globals.get("variable_key");

清除全局变量

pm.globals.unset("variable_key");

得到一个变量

此函数在全局变量和活动环境中搜索变量。

pm.variables.get("variable_key");

检查响应主体是否包含字符串

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

检查响应主体是否等于字符串

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

检查JSON值

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

内容类型存在

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

响应时间小于200毫秒

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

状态代码是200

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

代码名称包含一个字符串

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

成功的POST请求状态代码

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

使用TinyValidator获取JSON数据

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;
});

JSON模式验证器

var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
"properties": {
"alpha": {
"type": "boolean"
}
}
}; pm.test('Schema is valid', function() {
pm.expect(ajv.validate(schema, {alpha: true})).to.be.true;
pm.expect(ajv.validate(schema, {alpha: 123})).to.be.false;
});

解码base64编码数据

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
});

发送异步请求

此功能既可用作预请求脚本,也可用作测试脚本。

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

将XML主体转换为JSON对象

var jsonObject = xml2Json(responseBody);

postman断言的更多相关文章

  1. postman 断言解析

    最近在学习postman官方文档, 顺势翻译出来,以供学习! postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可. 断言会在请求返回之后,运行,并根据断言的pa ...

  2. postman断言作用及怎么使用

    这段时间一直在学习postman,在请求中使用断言,很多人不是很了解postman断言,其实呢,postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可. 1.设置环 ...

  3. postman断言分析

    最近测试中用到postman,使用后就简单总结下常用的断言,下面带图的自己最常用的,其他的没怎么用. postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可. 断言 ...

  4. postman 断言学习

    请求 url :https://www.v2ex.com/api/nodes/show.json?name=python get请求 postman发起请求并做断言 断言: tests["B ...

  5. 二、postman断言及正则表达式取值

    postman老式断言与新式断言总结:本文以微信开发者文档为例 断言处如图所示 一.老式断言 老式断言总结:var variables相当于代码中定义的变量,test['']=true;相当于pyth ...

  6. postman 断言

    //断言 pm.test("message等于'操作成功'", function () { var jsonData = pm.response.json(); console.l ...

  7. postman断言的方法

    1.在test添加断言 2.检查response的body中是否包含字符串: tests["Body matches string"] = responseBody.has(&qu ...

  8. postman断言的几种方式(二)

    1.检查响应体是否包含字符串 pm.test("Body matches string", function () { pm.expect(pm.response.text()). ...

  9. Postman基本使用——get、post请求、断言、环境变量

      Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件. 它提供功能强大的 Web API & HTTP 请求调试. 它能够发送任何类型的HTTP 请求 (GET, ...

随机推荐

  1. C#(.net)实现用apache activemq传递SQLite的数据

    版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处:http://www.cnblogs.com/2186009311CFF/p/6382623.html. C#(.net)实现用apache ...

  2. 阿里云 Serverless 应用引擎(SAE)发布 v1.2.0,支持一键启停、NAS 存储、小规格实例等实用特性

    近日,阿里云 Serverless 应用引擎(SAE)发布 v1.2.0版本,新版本实现了以下新功能/新特性: 一键启停开发测试环境:企业开发测试环境一般晚上不常用,长期保有应用实例,闲置浪费很高.使 ...

  3. 【bzoj1096】[ZJOI2007]仓库建设

    *题目描述: L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总 ...

  4. Linux Shell中捕获CTRL+C

    #!/bin/bash trap 'onCtrlC' INTfunction onCtrlC () { echo 'Ctrl+C is captured'} while true; do echo ' ...

  5. functional-page-navigator 组件

    functional-page-navigator 组件:是一个非常强大的组件,用于跳转插件的功能页 functional-page-navigator组件的属性: version:类型 字符串 跳转 ...

  6. Linux双网卡绑定和解除

    转载双网卡绑定和解除  一定要在服务管理中关闭NetworkManager服务并禁用自动启动,因为NetworkManager服务是实时生效的,一旦设置错,管理员就得回到机房接显示器配置网络连接. 以 ...

  7. p2p传输协议

    老司机是如何飙车的——P2P传输协议 转载来自2017-03-27 15:23 点波蓝字关注变智者 秋明山上人行稀,常有车手较高低,如今车道依旧在,不见当年老司机.其实老司机们从未离去,只不过好的车手 ...

  8. IntelliJ IDEA的常用设置

    1.设置IDEA主题样式 ①设置方法: ②效果:设置为Darcula之后整体的风格就是暗黑主题,如上图. 2.设置编辑区主题 ①设置方法: 注:由于IDEA自带的编辑区主题比较少,想要更多的编辑区主题 ...

  9. 刷题——有重复元素的全排列(Permutations II)

    题目如上所示. 我的解决方法(参考了九章的答案!): class Solution { public: /* * @param : A list of integers * @return: A li ...

  10. 第一章:Java语言概述与环境开发

    1.计算机高级语言按程序的执行方式可以分为编译型和解释型两种: 2.JAVA程序的执行过程必须经过先编译后解释两个步骤: 3.JAVA语言里负责执行字节码文件的是JAVA虚拟机 (Java Virtu ...