Postman脚本——断言测试

pm对象提供了测试相关功能:

  • pm.test(testName:String, specFunction:Function):Function:测试函数。
  • pm.expect(assertion:*):Function → Assertion:允许在响应数据上做测试,使用ChaiJS expect BDD语法。

可以使用pm.response.to.have.*pm.response.to.be.*来构建断言。

示例:

测试状态码:

js 复制代码
pm.test("请求状态码200", function() {
  pm.response.to.have.status(200); 
});
js 复制代码
pm.test("请求状态码200", function() {
  pm.expect(pm.response.code).to.eql(200); 
});

测试响应时间:

js 复制代码
pm.test("请求响应时间少于200ms", () => {
  pm.expect(pm.response.responseTime).to.be.below(200); 
});

是否满足指定条件:

js 复制代码
tests["字符串asdf包含a字符"] = "asdf".includes("a"); //旧的语法(已弃用,不推荐使用)

多断言组合测试:

测试脚本也可以包含多个断言作为单个测试,可以将相关断言组合在一起。如果包含的任一个断言失败,整个测试将失败,所有断言都必须成功,测试才能通过。

js 复制代码
pm.test("The response has all properties", () => {
  const responseJson = pm.response.json(); //将响应体body转换成JSON格式
  pm.expect(responseJson.type).to.eql('vip'); // type等于vip
  pm.expect(responseJson.name).to.be.a('string'); // name值的类型为string类型
  pm.expect(responseJson.id).to.have.lengthof(1); // id的长度为1
}

测试结果如下:

相关推荐
小冯的编程学习之路17 小时前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
Ten peaches20 小时前
Selenium-Java版(操作元素)
java·selenium·测试工具·html
傻小胖1 天前
三种常见接口测试工具(Apipost、Apifox、Postman)
测试工具·postman
辛普森Mmmm1 天前
Postman接口测试
测试工具·postman
程序员三藏1 天前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
头疼的程序员2 天前
allure生成测试报告(搭配Pytest、allure-pytest)
测试工具·pytest
程序员杰哥2 天前
自动化测试基础知识详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
程序员小远3 天前
自动化测试与功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
第三方软件测评3 天前
第三方软件测评中心分享:软件功能测试类型和测试工具
功能测试·测试工具
慢一点会很快4 天前
【网络分析工具】网络工具wireshark、TCPdump、iperf使用详解
测试工具·wireshark·tcpdump