• 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);
  • 相关阅读:
    MySQL事务提交流程详解
    MySQL启动过程详解五:GTID的处理
    MySQL启动过程详解二:核心模块启动 init_server_components()
    网络安全:SQL注入,XSS攻击,CSRF攻击
    Dropdown下拉菜单
    VUE:身份证组件
    window系统下 解决MySQL 8报错sql_mode=only_full_group_by:this is incompatible with sql_mode=only_full_group_by
    上传图片:点击按钮后进入对话框,点击➕号直接上传图片,点击确定即保存
    VUE: TreeChart树形图
    vue生成二维码:vueqr二维码插件使用
  • 原文地址:https://www.cnblogs.com/datacenter/p/14070345.html
Copyright © 2020-2023  润新知