Postman—添加斷言和檢查點


前言 

postman斷言是 JavaScript語言編寫的,在postman客戶端指定區域編寫即可。

斷言會在請求返回之后,運行,並根據斷言的pass\fail情況體現在最終測試結果中。

一、斷言步驟

1、打開保存的postman完成的訪問百度經驗的一個簡單的Get請求。點擊Send,查看響應信息。

這里就設置,檢查響應信息里面包含:“百度新聞——全球最大的中文新聞平台”這幾個字、

同時設置響應httpCode為200,斷言響應成功。

2、點擊Url下方的Tests,這里設置斷言。

 

點進來后這里是空的,你如果熟悉JavaScript的函數,可以直接手動輸入,如果不熟悉,postman在右側已經提供了tests片段。

點擊Learn more tests可以了解更多代碼片段。

找到Response body:Contains string響應body包含字符串。點擊。

在Tests框內出現一段代碼片段,其他不用管,只需要將string_you_want_to_search字符串,替換為:百度新聞——全球最大的中文新聞平台

即可,而且代碼片段寫的很明白,從英文意思就可以了解該如何設置。

找到代碼片段。Status code:code is 200.點擊。

生成斷言httpCode為200的代碼片段。

Ctrl+s保存一下請求的設置。

然后點擊Send發送請求,查看響應信息。

響應中查看Tests Results。設置的檢查全部成功。

二、斷言內容詳解

1.設置環境變量--Setting an environment variable 

postman.setEnvironmentVariable("key", "value");

2.設置全局變量--Set a global variable

postman.setGlobalVariable("key", "value");

3.檢查響應中包含string--Check if response body contains a string

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

4.轉化XML格式的響應成JSON對象---Convert XML body to a JSON object

var jsonObject = xml2Json(responseBody);

5.檢查響應body中等於指定string--Check if response body is equal to a string

 tests["Body is correct"] = responseBody === "response_body_string";

6.檢查JSON某字段值--Check for a JSON value

var data = JSON.parse(responseBody);

tests["Your test name"] = data.value === 100;

7.檢查Content-Type是否包含在header返回(大小寫不敏感)--Content-Type is present (Case-insensitive checking)

 tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.

8.檢查Content-Type是否包含在header返回(大小寫敏感)--Content-Type is present (Case-sensitive)

 tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

9.檢查請求耗時時間小於200ms--Response time is less than 200ms

tests["Response time is less than 200ms"] = responseTime < 200;

10.檢查Status code為200--Status code is 200

tests["Status code is 200"] = responseCode.code === 200;

11.檢查Code name是否指定string--Code name contains a string

 tests["Status code name has string"] = responseCode.name.has("Created");

12.檢查成功post的請求status code--Succesful POST request status code

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM