1. //https://www.getpostman.com/docs/v6/postman/scripts/test_examples
  2.  
  3. //Setting an environment variable
  4. pm.environment.set("variable_key", "variable_value");
  5.  
  6. //Setting a nested object as an environment variable
  7. var array = [1, 2, 3, 4];
  8. pm.environment.set("array", JSON.stringify(array, null, 2));
  9.  
  10. var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
  11. pm.environment.set("obj", JSON.stringify(obj));
  12.  
  13. //Getting an environment variable
  14. pm.environment.get("variable_key");
  15.  
  16. //Getting an environment variable (whose value is a stringified object)
  17.  
  18. //-- These statements should be wrapped in a try-catch block if the data is coming from an unknown source.
  19. var array = JSON.parse(pm.environment.get("array"));
  20. var obj = JSON.parse(pm.environment.get("obj"));
  21.  
  22. ---------------------------------------------------
  23.  
  24. //Clear an environment variable
  25. pm.environment.unset("variable_key");
  26.  
  27. //Set a global variable
  28. pm.globals.set("variable_key", "variable_value");
  29.  
  30. //Get a global variable
  31. pm.globals.get("variable_key");
  32.  
  33. //Clear a global variable
  34. pm.globals.unset("variable_key");
  35.  
  36. //Get a variable
  37. //This function searches for the variable across globals and the active environment.
  38. pm.variables.get("variable_key");
  39.  
  40. //Check if response body contains a string
  41. pm.test("Body matches string", function () {
  42. pm.expect(pm.response.text()).to.include("string_you_want_to_search");
  43. });
  44.  
  45. //Check if response body is equal to a string
  46. pm.test("Body is correct", function () {
  47. pm.response.to.have.body("response_body_string");
  48. });
  49.  
  50. //Check for a JSON value
  51. pm.test("Your test name", function () {
  52. var jsonData = pm.response.json();
  53. pm.expect(jsonData.value).to.eql(100);
  54. });
  55.  
  56. //Content-Type is present
  57. pm.test("Content-Type is present", function () {
  58. pm.response.to.have.header("Content-Type");
  59. });
  60.  
  61. //Response time is less than 200ms
  62. pm.test("Response time is less than 200ms", function () {
  63. pm.expect(pm.response.responseTime).to.be.below(200);
  64. });
  65.  
  66. //Status code is 200
  67. pm.test("Status code is 200", function () {
  68. pm.response.to.have.status(200);
  69. });
  70.  
  71. //Code name contains a string
  72. pm.test("Status code name has string", function () {
  73. pm.response.to.have.status("Created");
  74. });
  75.  
  76. //Successful POST request status code
  77. pm.test("Successful POST request", function () {
  78. pm.expect(pm.response.code).to.be.oneOf([201,202]);
  79. });
  80.  
  81. //Use TinyValidator for JSON data
  82. var schema = {
  83. "items": {
  84. "type": "boolean"
  85. }
  86. };
  87. var data1 = [true, false];
  88. var data2 = [true, 123];
  89.  
  90. pm.test('Schema is valid', function() {
  91. pm.expect(tv4.validate(data1, schema)).to.be.true;
  92. pm.expect(tv4.validate(data2, schema)).to.be.true;
  93. });
  94.  
  95. //Decode base64 encoded data
  96.  
  97. var intermediate,
  98. base64Content, // assume this has a base64 encoded value
  99. rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
  100.  
  101. intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
  102. pm.test('Contents are valid', function() {
  103. pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
  104. });
  105.  
  106. //Send an asynchronous request
  107. //This function is available as both a pre-request and test script.
  108. pm.sendRequest("https://postman-echo.com/get", function (err, response) {
  109. console.log(response.json());
  110. });
  111.  
  112. //Convert XML body to a JSON object
  113. var jsonObject = xml2Json(responseBody);
  114. Sample data files
  115.  
  116. //JSON files are composed of key/value pairs.
  117.  
  118. Download JSON file
  119. For CSV files, the top row needs to contain variable names.
  120.  
  121. //Download CSV file
  122.  
  123. //Set the request to be executed next
  124. postman.setNextRequest("request_name");
  125.  
  126. //Stop workflow execution
  127. postman.setNextRequest(null);
  128.  
  129. postman.getResponseHeader(headerName)
  130. /*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. */
  131.  
  132. //will return the same value.
  133. postman.getResponseHeader("Content-type")
  134. postman.getResponseHeader("content-Type")
  1. pm.test("A single user was returned", function () {
  2. pm.expect(pm.response.json().results).to.have.lengthof(1);
  3. });
  4.  
  5. //Gender tests
  6. pm.test("Gender is male", function () {
  7. pm.expect(pm.response.json().result[0].gender).to.equal("male");
  8. pm.expect(pm.response.json().result[0].gender).to.equal("mr");
  9. });
  10.  
  11. //National test
  12. pm.test("The user is from the United States", function () {
  13. pm.expect(pm.response.json().result[0].nat).to.equal("US");
  14. });
  15.  
  16. //----------------------------------------------------
  17. //Here are some examples:
  18.  
  19. // example using pm.response.to.have
  20. pm.test("response is ok", function () {
  21. pm.response.to.have.status(200);
  22. });
  23.  
  24. // example using pm.expect()
  25. pm.test("environment to be production", function () {
  26. pm.expect(pm.environment.get("env")).to.equal("production");
  27. });
  28.  
  29. // example using response assertions
  30. pm.test("response should be okay to process", function () {
  31. pm.response.to.not.be.error;
  32. pm.response.to.have.jsonBody("");
  33. pm.response.to.not.have.jsonBody("error");
  34. });
  35.  
  36. // example using pm.response.to.be*
  37. pm.test("response must be valid and have a body", function () {
  38. // assert that the status code is 200
  39. pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants
  40. // assert that the response has a valid JSON body
  41. pm.response.to.be.withBody;
  42. pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed
  43. });

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. 【Spring Boot && Spring Cloud系列】在spring-data-Redis中如何使用切换库

    前言 Redis默认有16个库,默认连接的是index=0的那一个.这16个库直接是相互独立的. 一.在命令行中切换 select 1; 二.在Spring中如何切换 1.在RedisConnecti ...

  2. JVM常用工具使用之jmap

    一.参考链接 https://www.cnblogs.com/yjd_hycf_space/p/7753847.html 二.个人的总结 一般我习惯使用 jmap -dump:live,format= ...

  3. Unity3D Shader图像扭曲过场效果

    把脚本挂在摄像机上 using UnityEngine; using System.Collections; [RequireComponent(typeof(Camera))] public cla ...

  4. “找女神要QQ号码”——java篇

    题目就是这样的: 给了一串数字(不是QQ号码),根据下面规则可以找出QQ号码: 首先删除第一个数,紧接着将第二个数放到这串数字的末尾,再将第三个数删除,并将第四个数放到这串数字的末尾...... 如此 ...

  5. spring boot 通过Maven + tomcat 自动化部署

    使用maven创建的springboot项目,默认是jar包,springboot还有自己带的tomcat. 现在为了简单实现本地自动发布项目到服务器,需要通过发布war包的形式,通过maven将项目 ...

  6. python偏函数的运用

    摘要:python的设计核心原则就是简洁——在这种原则的指导下,诞生了lambda表达式和偏函数:二者都让函数调用变得简洁.本文主要为你介绍偏函数的应用. 1.为什么要使用偏函数 如果我们定义了一个函 ...

  7. 富文本编辑器TinyMCE

    最近项目中用到了javascript富文本编辑器,从网上找开源控件,发现很多可选,参考下面文章,列出了很多可用的插件http://www.cnblogs.com/ywqu/archive/2009/1 ...

  8. postgresql----数组类型和函数

    postgresql支持数组类型,可以是基本类型,也可以是用户自定义的类型.日常中使用数组类型的机会不多,但还是可以了解一下.不像C或JAVA高级语言的数组下标从0开始,postgresql数组下标从 ...

  9. Java除法和js

    java 除 向下取整 js 保留小数

  10. poj3264 balanced lineup【线段树】

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...