最近测试中用到postman,使用后就简单总结下常用的断言,下面带图的自己最常用的,其他的没怎么用。

postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可。

断言会在请求返回之后,运行,并根据断言的pass\fail情况体现在最终测试结果中。

1.设置环境变量--Setting an environment variable

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

2.设置全局变量--Set a global variable

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

3.检查响应中**string--Check if response body contains a string

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

4.转化XML格式的响应成JSON对象---Convert XML body to a JSON object

var jsonObject = xml2Json(responseBody);

5.检查响应body中等于指定string--Check if response body is equal to a string

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

6.检查JSON某字段值--Check for a JSON value

var data = JSON.parse(responseBody);

tests["Your test name"] = data.value === 100;

7.检查Content-Type是否**在header返回(大小写不敏感)--Content-Type is present (Case-insensitive checking)

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

8.检查Content-Type是否**在header返回(大小写敏感)--Content-Type is present (Case-sensitive)

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

9.检查请求耗时时间小于200ms--Response time is less than 200ms

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

10.检查Status code为200--Status code is 200

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

11.检查Code name**指定string--Code name contains a string

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

12.检查成功post的请求status code--Succesful POST request status code

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

13.为JSON data使用微小验证器--Use TinyValidator for JSON data

var schema = {

"items": {

"type": "boolean"

}

};

var data1 = [true, false];

var data2 = [true, 123];

console.log(tv4.error);

tests["Valid Data1"] = tv4.validate(data1, schema);

tests["Valid Data2"] = tv4.validate(data2, schema);

Sample data files

JSON files are composed of key/value pairs

 

postman断言分析的更多相关文章

  1. postman 断言解析

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

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

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

  3. postman断言

    较旧的写作邮差测试风格 较旧的Postman测试编写风格依赖于特殊tests对象的设置值.您可以为对象中的元素设置描述性键,然后说明它是真还是假.例如,tests["Body contain ...

  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. API测试工具SoapUI & Postman对比分析

    本文由葡萄城技术团队于博客园原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 最近公司要引入API测试工具,经过调查和了解,最终决定在SoapUI ...

  7. postman 断言

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

  8. postman断言的方法

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

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

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

随机推荐

  1. 利用WebViewJavascriptBridge与UIWebView进行交互

    事情的起因还是因为项目需求驱动.折腾了两天,由于之前没有UIWebView与JS交互的经历,并且觉得这次在功能上有一定的创造性,特此留下一点文字,方便日后回顾. 我要实现这样一个需求:按照本地的CSS ...

  2. mysql无法远程访问

    最近使用Navicat for MySQl访问远程mysql数据库,出现报错, 显示“1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connect to ...

  3. DataSnap的如果网络断线,如何恢复?

    timer代码很简单:var adbsevertime :TDateTime;begin try adbsevertime := ClientModule1.ServerMethods1Client. ...

  4. 织梦DEDE多选项筛选_联动筛选功能的实现_二次开发

    织梦默认的列表页没有筛选功能,但有时候我们做产品列表页的时候,产品的字段比较多,很多人都需要用到筛选功能,这样可以让用户更方便的找到自己所需要的东西,实现这个联动筛选功能需要对织梦进行二次开发,下面就 ...

  5. codeforces 667B B. Coat of Anticubism(水题)

    题目链接: B. Coat of Anticubism time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. nodejs 打造 多人对战游戏服务器(初级入门)

    使用socket.set 和 socket.get 在存取玩家信息 百牛信息技术bainiu.ltd整理发布于博客园 socket.get('playerinfo', function (err, p ...

  7. 多线程-threading模块2

    从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个 t(t1.t2....),如果创建的线程较多时这样极其不方便.下面对通过例子进行改进:   #coding:utf-8 impor ...

  8. HBase之四--(2):spring hadoop 访问hbase

    1.  环境准备: Maven Eclipse Java Spring 2. Maven  pom.xml配置 <dependency> <groupId>org.apache ...

  9. Gradle系列之一 Groovy语法精讲

    Gradle技术之一 Groovy语法精讲 gradle脚本是基于groovy语言开发的,想要学好gradle必须先要对groovy有一个基本的认识 1. Groovy特点 groovy是一种DSL语 ...

  10. 如何实现session的共享?

    1.以cookie加密的方式保存在客户端. 优点是减轻服务器端的压力 缺点是受到cookie的大小限制,可能占用一定带宽,因为每次请求会在头部附带一定大小的cookie信息,另外这种方式在用户禁止使用 ...