当前位置:网站首页>Postman assertion
Postman assertion
2022-07-06 04:48:00 【Cool leg hair!】
Postman Assertion
Postman Introduction to assertions
- postman Assertion aid JavaScript - js Language code , Automatically judge whether the expected results are consistent with the actual results .
- Assertion The code is written in Tests Label .
Postman Common assertions
1. Assertion response status code
Status code: Code is 200
- stay Tests In the label , Choose Status Code:code is 200, Generate corresponding code
- To adjust properly test() Method parameter 1, and In anonymous functions Expected results .
- Click on send Button , Send a request , Execute assertion code .
- Look at the assertion results .
// Assertion response status code Is it 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm: representative postman An example of
test(): yes pm An example of a method . There are two parameters
Parameters 1: After the assertion is successful , Give a text prompt . You can modify ."Status code is 200"
Parameters 2: Anonymous functions .
pm.response.to.have.status(200);
// Meaning :postman The response result should contain the status code 200
200 ——> Expected results !
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-3wT6V0cS-1656839916613)( The interface test - The first 03 God - Class notes .assets/image-20210709103914593.png)]](/img/26/38bc54b39096c91ee498cd7dfb5212.png)
2. Assert whether the response body contains a string
Response body: Contains string
// The assertion response body contains the specified string
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm:postman An example of
test(): postman Example method , There are two parameters
ginseng 1: Text prompt message displayed after assertion , It can be changed .
ginseng 2: Anonymous functions
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
// Meaning :pm expect The response text in , contain xxxx character string .
"string_you_want_to_search" ——> Expected results . You can modify

3. Assert whether the response body is equal to a string ( object )
Response body: Is equal to a string
// Assertion Response body Equal to a string ( object )
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
pm.response.to.have.body("response_body_string");
// intend ,pm Of There should be... In the response Response body xxx
"response_body_string" ——> Expected results . You can modify
4. Assertion JSON data
Response body: JSON value check
// Assertion json Response result of
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
var jsonData = pm.response.json();
// var jsonData: use js Syntax defines a variable .jsonData It's the variable name
// pm.response.json(); On behalf of the response json result
/* give an example :response.json(); { "success": true, "code": 10000, "message": " Successful operation !", "data": "95c78d75-721c-40fb-b2d5-742fea42cbd5" } */
pm.expect(jsonData.value).to.eql(100);
// Meaning :pm expect json result key Corresponding value be equal to xxx
// to.eql(100); Medium 100 Represents the expected result . Modifiable .
/* give an example : jsonData.value Of value: take :success、code、message、data */
Example :
// Assertion json Response result of -success The value of is true
pm.test(" Assertion response result success The value of is true", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.success).to.eql(true);
});
pm.test(" In the assertion response result code The value of is 10000", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.code).to.eql(10000);
});
pm.test(" In the assertion response result message The value of is Successful operation ", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.message).to.eql(" Successful operation !");
});

5. Assertion response header
Response headers: Content-Type header check
// Assertion response header
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
pm.response.to.have.header("Content-Type");
// pm Response The head contains Content-Type
// Example : Can be in header in , add to In the response header key Corresponding value determine . use , Partition .
// Assertion response header
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type", "application/json;charset=UTF-8");
});
Postman How assertions work

边栏推荐
- Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
- Can Flink SQL read multiple topics at the same time. How to write in with
- Lagrange polynomial
- [buuctf.reverse] 159_[watevrCTF 2019]Watshell
- L'introduction en bourse de MSK Electronics a pris fin: 800 millions de RMB d'actifs de Henan étaient des actionnaires
- [mathematical modeling] differential equation -- sustainable development of fishing industry
- 2021 robocom world robot developer competition - undergraduate group (semi-finals)
- Quick sort
- 11. Intranet penetration and automatic refresh
- MIT CMS. 300 session 8 – immersion / immersion
猜你喜欢

Uva1592 Database

Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码

Codeforces Round #804 (Div. 2)

The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder

Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems

ue5 小知识点 开启lumen的设置

Etcd database source code analysis -- etcdserver bootstrap initialization storage

Delete subsequence < daily question >

Postman前置脚本-全局变量和环境变量

How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
随机推荐
RTP gb28181 document testing tool
cdc 能全量拉去oracle 表嘛
Quick sort
力扣(LeetCode)186. 翻转字符串里的单词 II(2022.07.05)
L'introduction en bourse de MSK Electronics a pris fin: 800 millions de RMB d'actifs de Henan étaient des actionnaires
Codeforces Round #804 (Div. 2)
Application of Flody
图论的扩展
优秀PM必须经历这3层蜕变!
Platformio create libopencm3 + FreeRTOS project
SQL注入漏洞(MSSQL注入)
ue5 小知识点 开启lumen的设置
What should the project manager do if there is something wrong with team collaboration?
驱动开发——HelloWDM驱动
Orm-f & Q object
[network] channel attention network and spatial attention network
IPv6 comprehensive experiment
Quatre méthodes de redis pour dépanner les grandes clés sont nécessaires pour optimiser
CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
Request (request object) and response (response object)