SoapUI 测试
很多小伙伴不知道,在 SoapUI 写了一个接口之后,如何去进行测试,今天分享一下 SoapUI 中测试一个接口。
准备数据源
首先我们准备一份数据源。
{
"total": 3,
"resources": [
{
"username": "test03-SD",
"created": "1496800026000",
"uuid": "8f6cae8a24ab4d0887dd5907430075e7",
"contractNumber": "131"
},
{
"username": "test02",
"created": "1489479452000",
"name": "8bbf9fded675472aa852cf1940bc8234",
"contractNumber": "133"
},
{
"username": "test01",
"created": "1487576620000",
"name": "156b396f9b354467b5d1d1a1014b2d10"
}
],
"pageNum": 1
}
创建测试脚本
接着,我们在 HTTP Request 中创建 断言~

弹窗中,我们选择 script,代表 脚本断言 的意思:

然后我们在 Script Assertion 中填写脚本:
def booksRoot = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent);
def total = booksRoot.get("total");
assert total == 3
接口测试 通过
我们 发送请求,发现返回的数据符合我们的断言,也就是 Pass

接口测试 不通过
我们修改下 Script Assertion
def booksRoot = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent);
def total = booksRoot.get("total");
assert total == 10
因为 total 为 3
才是正确的,所以断言不通过~

Apifox 可视化测试
Apifox 这款 API 工具也能很好地完成 接口测试,在 Apifox 中,像上面的 Script Assertion,甚至都不用写脚本,直接使用 可视化断言即可!
创建接口
我们创建一个接口,并填写 名称、url、method 等重要信息

设置测试断言
Apifox 提供了 可视化断言

我们只需要填入一些 断言信息 即可:
- 断言名称
- 断言条件
- JSON PATH 表达式

然后我们点击 保存
接口测试 通过
现在我们发送请求,可以看到,测试通过!

接口测试 不通过
我们回到 修改文档 界面,如果我们把 3 改成 10,然后保存,去发送,会发生啥呢?

这时就会测试不通过~

Apifox

Apifox 是一体化 API 协作平台,可以实现 API 文档、API 调试、API Mock、 API 自动化测试,是更先进的 API 设计/开发/测试工具。Apifox 提供了一种全面的 API 管理解决方案。使用 Apifox ,你可以在统一的平台上设计、调试、测试以及协作你的 API,消除了在不同工具之间切换和数据不一致的问题。 简化了你的 API 工作流,并确保了前端、后端和测试人员之间的高效协作,点击免费使用。