zoukankan      html  css  js  c++  java
  • postman 脚本学习

    pm的脚本断言库默认似乎是集成chaijs的。所以重点也要掌握chaijs的用法,其实和其他断言库类似。玩着玩着就会了。推荐看看 简书 chaijs 中文文档

    传送门:

    # pm 脚本的教程
    https://learning.getpostman.com/docs/postman/scripts/test_scripts/
    
    # pm 沙盒环境的一些 api
    https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/
    
    # chaijs 官方 github
    https://github.com/chaijs/chai
    
    # 简书 chaijs 中文文档
    https://www.jianshu.com/p/f200a75a15d2
    
    # chaijs 官方文档
    https://www.chaijs.com/api/

    # qq 邮箱设置白名单
    noreply@notifications.getpostman.com

    # 可以呼出chrome dev tool,所以test脚本中可以使用console.log
    ctrl + shift + i

    判断数组长度

    pm.test("返回数组长度必须大于0", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData).to.have.length.above(0);
    });

    判断是否拥有属性

    pm.test("对象应该拥有指定7个属性", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData).to.have.property("attitude");
        pm.expect(jsonData).to.have.property("inefficient");
        pm.expect(jsonData).to.have.property("plug");
        pm.expect(jsonData).to.have.property("notOpen");
        pm.expect(jsonData).to.have.property("window");
        pm.expect(jsonData).to.have.property("hardware");
        pm.expect(jsonData).to.have.property("inconvenient");
        pm.expect(jsonData).to.have.property("other");
    });

    但这种写法太冗余了,可以更灵活的书写

    // have.all.keys:目标对象必须且仅能拥有全部传入的属性名(不能多,不能少)
    // contains.all.keys:目标对象必须至少拥有全部传入的属性名,但是它也可以拥有其它属性名(只能多,不能少)
    // have.any.keys:目标必须至少存在一个传入的属性名才能通过测试(至少一个)
    pm.test("对象应该拥有指定7个属性", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData).to.have.all.keys("attitude", "inefficient", "plug", "notOpen", "window", "hardware", "inconvenient", "other");
    });
  • 相关阅读:
    Chrome 过滤广告插件暂替办法
    IE8.1将支持Firefox插件,Gecko和Webkit引擎
    Adobe与Facebook联手推出Flash开发工具
    Google Chrome Greasemetal
    vmware虚机下的linux集群
    新版Safari浏览器胜在速度
    Fedora 11 Beta发布
    Sun宣布本周裁员1500人 或为IBM收购做准备
    Opera Mobile9.7将支持Flash Lite和Google Gears
    Red Hat CEO:桌面Linux无用
  • 原文地址:https://www.cnblogs.com/CyLee/p/9749767.html
Copyright © 2011-2022 走看看