博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Postman—测试脚本
阅读量:4709 次
发布时间:2019-06-10

本文共 1512 字,大约阅读时间需要 5 分钟。

前言

对于Postman中的每个请求,我们都可以使用JavaScript语言来开发测试脚本。这也就好比单元测试。我们先看看Postman的相关界面:

编写测试脚本

Postman测试脚本本质上是在发送请求后执行的JavaScript代码,我们可以通过访问pm.response对象获取服务器返回的报文。

以下是一些测试脚本样例:

// example using pm.response.to.havepm.test("response is ok", function () { pm.response.to.have.status(200); }); // example using pm.expect() pm.test("environment to be production", function () { pm.expect(pm.environment.get("env")).to.equal("production"); }); // example using response assertions pm.test("response should be okay to process", function () { pm.response.to.not.be.error; pm.response.to.have.jsonBody(""); pm.response.to.not.have.jsonBody("error"); }); // example using pm.response.to.be* pm.test("response must be valid and have a body", function () { // assert that the status code is 200 pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants // assert that the response has a valid JSON body pm.response.to.be.withBody; pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed });

我们可以根据我们的需要,添加各种各样的测试案例。

沙箱

之前在《Postman—脚本介绍》这篇文章中已经说到了,Postman中的脚本是在一个沙箱环境中运行的。这个沙箱环境和Postman本身运行的环境是完全隔离开的,也就是说,这个沙箱环境给脚本的执行提供了一个上下文环境。这个沙箱环境本身就集成了很多的工具方法,我们可以在我们的测试脚本中直接使用这些工具方法。具体的可以参考以下文档:

代码片段

为了更提高使用者的测试脚本编写效率,Postman提供了很多的代码片段,我们可以直接使用这些已经写好的代码片段来完成测试脚本的编写。

查看结果

每次运行请求时Postman都会运行测试。当然,我们可以选择不查看测试结果!

测试结果显示在响应查看器下的“Test Results”选项卡中。选项卡标题显示通过了多少测试.

参考:https://www.jellythink.com/archives/179

转载于:https://www.cnblogs.com/101718qiong/p/9685585.html

你可能感兴趣的文章
Word直接发表博客测试
查看>>
最短路模板
查看>>
sublime text2 中Emmet常用的技巧 和快捷键
查看>>
[PWA] Caching with Progressive libraries
查看>>
Intellij IDEA Run Dashboard面板
查看>>
_event_team
查看>>
SetProcessAffinityMask的问题
查看>>
NSArray排序方法讲解
查看>>
JavaScript中的 NaN 与 isNaN
查看>>
IP,子网掩码,交换机,路由
查看>>
3. 股票投资方法
查看>>
linux详解sudoers
查看>>
java MAT 分析
查看>>
poj2828
查看>>
vs2015 Android SDK
查看>>
虚拟分区安装
查看>>
GeSHi Documentation
查看>>
PAT甲级1057 Stack【树状数组】【二分】
查看>>
Google内部培训过1.8万人的机器学习速成课
查看>>
基变换与坐标变换
查看>>