当前位置:网站首页>Assertion of postman interface test

Assertion of postman interface test

2022-06-26 01:41:00 NitefullSand

Excerpt from :https://www.jianshu.com/p/5e69f2a304f8

One 、Postman Assertion

A complete interface test , Include : request -> Get the response body -> Assertion , We already know the request and get response body , Here's how to use postman To assert that .

Tests

This ”Tests” That's where we need to deal with assertions ,postman It's very humanized to help us prepare all the functions used for assertion :

SNIPPETS

Illustrate with examples : First set up an assertion scenario , Take the assertion scenario as an example Postman How assertions are used .

1、 Judge HTTP Returns the status code as 200

2、 Determine whether the response body contains :"statusCode":200

3、 Parse the response body , And decide statusCode The value of is 200,message The value of is ”Success”

First step : stay SNIPPETS in , Pull down , There is one ”Status code:Code is 200”, This is for the second 1 I'm ready for , Judge HTTP Returns whether the status code is 200. Click on this , You can see it on the left , Assertion code is added automatically , See the picture below :

Status code

The analysis is as follows :

pm.test["Status code is 200"] Medium test It's a built-in object ( function ),test["Status code is 200"] It means to give this assertion a name called ”Status code is 200”, The name can be changed by oneself .

pm.response.to.have.status(200) Medium responseCode It's a built-in object ,responseCode One of the properties in the object is code, Refer to HTTP Status code code, Judge code Is it 200.

combined , This code means : The name is ”Status code is 200” In the assertion of , Judge responseCode Object's code Property value (HTTP Status code ) Is it 200.

The second step : Also in SNIPPETS in , Find one ”Response body:Contains string”, This is for the second 2 I'm ready for , Determine the fields in the response body . After clicking , On the left , Assertion code is added automatically , See the picture below :

Contains string1

We need to modify what we want to find in the response message :

pm.expect(pm.response.text()).to.include("string_you_want_to_search");    // Compare with the second 2 A scene : Determine whether the response body contains :"statusCode":200

Contains string2

The third step : We need to parse JSON Yes , therefore , stay SNIPPETS Find ”Response body:JSON value check” And click the , On the left , Assertion code is added automatically , See the picture below :

JSON value check1

We can see that , It's actually JS Code ,jsonData Variables are actually parsed JSON After the object , stay JS in , One JSON Object gets the value of its property , It's direct jsonData.value, therefore , Let's modify the code , To judge the second 3 A scene :

pm.expect(jsonData.value).to.eql(200);    // Judge statusCode The value of is 200

pm.expect(jsonData.value).to.eql('Success');    // Judge message The value of is ”Success”

JSON value check2

We can see that there are Tests Assertion of 4 individual , Click on Send, Send a request , In the response area, you can see the following figure :PASS Indicates that the assertion passed ,FAIL Indicates that the assertion failed .

Assert the running result



author :keitwo
link :https://www.jianshu.com/p/5e69f2a304f8
 

原网站

版权声明
本文为[NitefullSand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252353542511.html