当前位置:网站首页>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 !
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
边栏推荐
- C'est un petit résumé de l'étude.
- Bill Gates posted his 18-year-old resume and expected an annual salary of $12000 48 years ago
- [HBZ sharing] how to locate slow queries in cloud database
- Database - MySQL storage engine (deadlock)
- 团队协作出了问题,项目经理怎么办?
- 也算是學習中的小總結
- Why does MySQL need two-phase commit
- Scala function advanced
- 饼干(考试版)
- flink sql 能同时读多个topic吗。with里怎么写
猜你喜欢
Database - MySQL storage engine (deadlock)
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
11. Intranet penetration and automatic refresh
图论的扩展
麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东
[network] channel attention network and spatial attention network
Delete subsequence < daily question >
Basic explanation of turtle module - draw curve
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
随机推荐
[NOIP2009 普及组] 分数线划定
The most detailed and comprehensive update content and all functions of guitar pro 8.0
2021robocom robot developer competition (Preliminary)
行业专网对比公网,优势在哪儿?能满足什么特定要求?
Introduction of several RS485 isolated communication schemes
Tengine kernel parameters
Summary of three log knowledge points of MySQL
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
2021 RoboCom 世界机器人开发者大赛-本科组(复赛)
Bubble sort
Selection of slow motion function
2021 robocom world robot developer competition - undergraduate group (semi-finals)
What should the project manager do if there is something wrong with team collaboration?
项目经理,你会画原型嘛?项目经理需要做产品设计了?
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Database - MySQL storage engine (deadlock)
Nestjs配置文件上传, 配置中间件以及管道的使用
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
Lagrange polynomial