# 一 斷言
測試指定的restful api是否正常,判斷它的響應值是否符合預期標準,需要用到斷言知識。在soapUI里斷言使用的Groovy語言。在項目中測試起來非常簡單,測試過程如下。
1,準備測試數據
以下是準備的測試數據,它是一個JSON格式的數據列表。在resources節點中,它包含3個用戶消息子節點。
```
{
"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"
}
],
"time": "2017年06月13日 10時25分07秒",
"pageNum": 1
}
```
2 添加斷言
在HTTP Request里添加斷言,如下圖所示。

然后在彈出的Add Assertion窗口,選擇Script項。

3 判斷total屬性的值
使用Script Assertion 測試JSON 格式的列表,在Script Assertion 窗口中寫入如下代碼:
```
def booksRoot = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent);
def total = booksRoot.get("total");
assert total == 3
```
返回結果如下:

可以看到斷言中total對應的值與測試值是一樣的,如果故意寫錯,會怎么樣呢?錯誤的斷言如下:
```
def booksRoot = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent);
def total = booksRoot.get("total");
assert total == 10
```
返回結果如下:

可以看到soapUI提示斷言中total的判斷是錯誤的,應該為3。
4 判斷resources節點的長度
使用的斷言腳本如下:
~~~
def root = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent);
def resources = root.get("resources");
assert resources.size() == 3
~~~
5 判斷resource第一個節點的username屬性為test03-SD
~~~
def root = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent);
def resources = root.get("resources");
assert resources[0].get('username') == 'test03-SD'
~~~
# 二 例子
1,測試帶參數的restful api
請求地址:?
http://127.0.0.1:8083/auth/api/v2/user/userList?param={"user\_userType":"1","startpage":"1","pagesize":"10","morgId":"e1339799-628e-11e6-9e1b-e0db55cd9154"}
請求方法: get
返回json格式數據:
```
[
{
"uuid": "053c951668a04144addc3336fc3967ad",
"created": 1491976927000,
"username": "吉林域管理員",
"password": null,
"firstname": "吉林",
"lastname": null,
"email": "123@qq.com",
"state": "enabled",
"apiKey": null,
"secretKey": null,
"salt": "c271b448fe2bf0dacadc7680a85116bc",
"userType": "1",
"tenantId": null,
"sex": "2",
"location": null,
"contractNumber": null,
"qq": null,
"photoPath": null,
"incorrectLoginAttempts": 0,
"roleList": null,
"tenantName": null,
"morgId": "9ea8e621-628e-11e6-9e1b-e0db55cd9154",
"morgName": "吉林分公司",
"userMorgUuid": null,
"uorgName": null,
"uorgPath": null,
"projectName": null,
"morgPath": "//吉林分公司",
"roleUuid": null,
"userIds": null
},
{
"uuid": "088d6cf6a9c345b2b7191fe9a8366fcb",
"created": 1487226069000,
"username": "湖北域管理員2345",
"password": null,
"firstname": "111",
"lastname": null,
"email": "1@1.cn",
"state": "enabled",
"apiKey": null,
"secretKey": null,
"salt": "a41cd19102835984efba2f03af18b619",
"userType": "1",
"tenantId": null,
"sex": "1",
"location": null,
"contractNumber": null,
"qq": null,
"photoPath": null,
"incorrectLoginAttempts": 0,
"roleList": null,
"tenantName": null,
"morgId": "9ebbe3c1-628e-11e6-9e1b-e0db55cd9154",
"morgName": "湖北分公司",
"userMorgUuid": null,
"uorgName": null,
"uorgPath": null,
"projectName": null,
"morgPath": "//湖北分公司",
"roleUuid": null,
"userIds": null
}
]
```
soapUI中Http Requst配置,添加請求參數。

? 測試斷言:
~~~
def root = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent);
log.info( root.size() )
assert root.size() == 2
~~~
- 第一章-測試理論
- 1.1軟件測試的概念
- 1.2測試的分類
- 1.3軟件測試的流程
- 1.4黑盒測試的方法
- 1.5AxureRP的使用
- 1.6xmind,截圖工具的使用
- 1.7測試計劃
- 1.8測試用例
- 1.9測試報告
- 2.0 正交表附錄
- 第二章-缺陷管理工具
- 2.1缺陷的內容
- 2.2書寫規范
- 2.3缺陷的優先級
- 2.4缺陷的生命周期
- 2.5缺陷管理工具簡介
- 2.6缺陷管理工具部署及使用
- 2.7軟件測試基礎面試
- 第三章-數據庫
- 3.1 SQL Server簡介及安裝
- 3.2 SQL Server示例數據庫
- 3.3 SQL Server 加載示例
- 3.3 SQL Server 中的數據類型
- 3.4 SQL Server 數據定義語言DDL
- 3.5 SQL Server 修改數據
- 3.6 SQL Server 查詢數據
- 3.7 SQL Server 連表
- 3.8 SQL Server 數據分組
- 3.9 SQL Server 子查詢
- 3.10.1 SQL Server 集合操作符
- 3.10.2 SQL Server聚合函數
- 3.10.3 SQL Server 日期函數
- 3.10.4 SQL Server 字符串函數
- 第四章-linux
- 第五章-接口測試
- 5.1 postman 接口測試簡介
- 5.2 postman 安裝
- 5.3 postman 創建請求及發送請求
- 5.4 postman 菜單及設置
- 5.5 postman New菜單功能介紹
- 5.6 postman 常用的斷言
- 5.7 請求前腳本
- 5.8 fiddler網絡基礎及fiddler簡介
- 5.9 fiddler原理及使用
- 5.10 fiddler 實例
- 5.11 Ant 介紹
- 5.12 Ant 環境搭建
- 5.13 Jmeter 簡介
- 5.14 Jmeter 環境搭建
- 5.15 jmeter 初識
- 5.16 jmeter SOAP/XML-RPC Request
- 5.17 jmeter HTTP請求
- 5.18 jmeter JDBC Request
- 5.19 jmeter元件的作用域與執行順序
- 5.20 jmeter 定時器
- 5.21 jmeter 斷言
- 5.22 jmeter 邏輯控制器
- 5.23 jmeter 常用函數
- 5.24 soapUI概述
- 5.25 SoapUI 斷言
- 5.26 soapUI數據源及參數化
- 5.27 SoapUI模擬REST MockService
- 5.28 Jenkins的部署與配置
- 5.29 Jmeter+Ant+Jenkins 搭建
- 5.30 jmeter腳本錄制
- 5.31 badboy常見的問題
- 第六章-性能測試
- 6.1 性能測試理論
- 6.2 性能測試及LoadRunner簡介
- 第七章-UI自動化
- 第八章-Maven
- 第九章-測試框架
- 第十章-移動測試
- 10.1 移動測試點及測試流程
- 10.2 移動測試分類及特點
- 10.3 ADB命令及Monkey使用
- 10.4 MonkeyRunner使用
- 10.5 appium工作原理及使用
- 10.6 Appium環境搭建(Java版)
- 10.7 Appium常用函數(Java版)
- 10.8 Appium常用函數(Python版)
- 10.9 兼容性測試