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");
});
原文地址:https://www.cnblogs.com/CyLee/p/9749767.html