zoukankan      html  css  js  c++  java
  • Postman中的测试脚本(Test scripts)

    一、postman测试脚本

    测试脚本是在发送请求之后运行的,并且已经从服务器接收到响应。

    二、测试举例

    1、设置环境变量
    pm.environment.set("variable_key", "variable_value");
    2、将嵌套对象设置为环境变量
    1 var array = [1, 2, 3, 4];
    2 pm.environment.set("array", JSON.stringify(array, null, 2));
    3  
    4 var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
    5 pm.environment.set("obj", JSON.stringify(obj));
    3、获取环境变量
    pm.environment.get("variable_key");
    4、获取环境变量(其值是严格化对象)
    var array = JSON.parse(pm.environment.get("array"));
    var obj = JSON.parse(pm.environment.get("obj"));
    5、清除环境变量
    pm.environment.unset("variable_key");
    6、设置全局变量
    pm.globals.set("variable_key", "variable_value");
    7、获取全局变量
    pm.globals.get("variable_key");
    8、清除全局变量
    pm.globals.unset("variable_key");
    9、获取变量
    pm.variables.get("variable_key");
    10、检查响应体是否包含字符串
    pm.test("Body matches string", function () {
        pm.expect(pm.response.text()).to.include("string_you_want_to_search");
    });
    11、检查响应体是否等于字符串
    pm.test("Body is correct", function () {
        pm.response.to.have.body("response_body_string");
    });
    12、检查JSON值
    pm.test("Your test name", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.value).to.eql(100);
    });
    13、内容类型存在
    pm.test("Content-Type is present", function () {
        pm.response.to.have.header("Content-Type");
    });
    14、响应时间小于200毫秒
    pm.test("Response time is less than 200ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(200);
    });
    15、状态代码为200
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });
    16、代码名包含字符串
    pm.test("Status code name has string", function () {
        pm.response.to.have.status("Created");
    });
    17、成功后请求状态代码
    pm.test("Successful POST request", function () {
        pm.expect(pm.response.code).to.be.oneOf([201,202]);
    });
    18、为JSON数据使用TinyValidator
    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;
    });
    19、解码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
    });
    20、发送异步请求
    pm.sendRequest("https://postman-echo.com/get", function (err, response) {
        console.log(response.json());
    });
    21、将XML体转换为JSON对象
    var jsonObject = xml2Json(responseBody);
  • 相关阅读:
    项目实战从 0 到 1 学习之Flink (24)Flink将kafka的数据存到redis中
    LeetCode107. 二叉树的层次遍历 II
    LeetCode102. 二叉树的层序遍历
    LeetCode341. 扁平化嵌套列表迭代器
    【总结】二叉树的前中后序遍历(递归和非递归)
    LeetCode145. 二叉树的后序遍历
    LeetCode94. 二叉树的中序遍历
    LeetCode144. 二叉树的前序遍历
    LeetCode71. 简化路径
    LeetCode150. 逆波兰表达式求值
  • 原文地址:https://www.cnblogs.com/datacenter/p/14070345.html
Copyright © 2011-2022 走看看