最近测试中用到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. mongo-spark 安装排故 ./sbt check

    [error] at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:) [error] at com.mong ...

  2. Window XP安装Ubuntu14.04实现Samba文件共享

    安装了Ubuntu14.04之后,在虚拟机设置里设置了文件共享.但在mnt文件夹下没有hgfs这个文件夹.依照网上说的去做还是不行,仅仅好放弃.改用samba实现Windows与Ubuntu文件共享. ...

  3. aop+自定义注解

    自定义注解,并且实现,需要两个文件: 自定义注解类: package com.clc.server.annotation; import java.lang.annotation.ElementTyp ...

  4. 广大暑假训练1 E题 Paid Roads(poj 3411) 解题报告

    题目链接:http://poj.org/problem?id=3411 题目意思:N个city 由 m 条路连接,对于一条路(假设连接Cityia和 Cityb),如果从Citya 去 Cityb的途 ...

  5. Linux系统中的运行级别

    什么是运行级呢?简单的说,运行级就是操作系统当前正在运行的功能级别. 它让一些程序在一个级别启动,而另外一个级别的时候不启动. Linux系统的有效登录模式有0~9共十种,不过沿用UNIX系统的至多6 ...

  6. rtmplib rtmp协议过程分析

    转自:http://chenzhenianqing.cn/articles/1009.html 写的很好,收藏如下,向作者致敬! 没事碰到了librtmp库,这个库是ffmpeg的依赖库,用来接收,发 ...

  7. ASP.NET Core:创建一个Core项目

    ylbtech-ASP.NET Core:创建一个Core项目 1.返回顶部 1. 2. 3. 4.         5. 2.返回顶部 1.新建Razor页面 2. 3. 4.Abc 4.1.Abc ...

  8. linux-3.0内核移植到fl2440开发板(以s3c2410为模板)

    1.新建kernel文件夹,用于存放内核文件 [weishusheng@localhost ~]$ mkdir kernel 2.进入kernel,上传压并解压压缩文件 [weishusheng@lo ...

  9. bzoj2384

    树状数组+KMP 匹配问题上KMP 但是问题在于如何判断两个位置相等,我们认为如果一个位置之前比他小的数数量相同那么就是相等. 那么我们用树状数组动态维护这个东西,每次跳nxt的时候用树状数组删除数. ...

  10. SVN进行代码的托管

    svn 使用的是集中服务器 就是只有一个服务器的意思 git 是分布式服务器  服务器: 存储客户端上传的源代码. 可以在Windows上通过安装 Visual SVN Sever .  客户端: 上 ...