【技巧】ApiPost如何设置断言

195 阅读2分钟

ApiPost是一款国产的接口测试和接口文档生成工具

apipost下载地址

我这边想说的是关于断言语法的东西,apipost后执行脚本是一个JavaScript执行环境,可以通过JS脚本来编写pre-request Script和test Script。pre-request Script可以用来修改一些默认参数。也就是说其实请求的预置脚本跟测试脚本都是用js语法来写的,如果熟悉js完全可以忽略本文,肯定能写出更牛的脚本;下面列出的是一些常用的预置脚本跟测试脚本,如果对js语法不了解,可能通过这样的积累也能起到想要的效果,语法也很简单,多敲几次就掌握其中的套路了;

在操作执行脚本的时候我们现需要配置环境变量,可以看这篇文章apipost环境变量配置

ApiPost预执行脚本设置

我们可以在预执行脚本中编辑我们的脚本

![](https://upload-images.jianshu.io/upload_images/23373837-28a37358edb170ca?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

apipost在预执行脚本中给了几个预执行脚本的例子,选择需要的预执行脚本点击就可以添加到预执行脚本当中。

![](https://upload-images.jianshu.io/upload_images/23373837-25f801e91184d487?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

ApiPost设置后执行脚本

ApiPost的后执行脚本可以使用断言功能

![](https://upload-images.jianshu.io/upload_images/23373837-fe7c9e7fe7d3f41b?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

我们可以选择可以选择响应状态码是否为200

![](https://upload-images.jianshu.io/upload_images/23373837-03b7b3237bf70c79?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

运行查看结构,这里响应码200是成功的。

![](https://upload-images.jianshu.io/upload_images/23373837-dfc82bb2617ef7b5?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

我们还可以自己设置响应码,比如我们把200改成404

![](https://upload-images.jianshu.io/upload_images/23373837-8a75e3bff0856c78?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

这里因为我们请求成功之后响应的是200,然后断言设置的是响应码是404所以断言测试显示的失败

![](https://upload-images.jianshu.io/upload_images/23373837-f288238487f7f1ad?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

然后还有其他断言比如响应时间等等你们可以自己尝试。

![](https://upload-images.jianshu.io/upload_images/23373837-755fe0f3c8fea390?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)