Different types of Asserts in Postman(自动化测试,断言)

In the previous tutorial of Chai Assertion Library  we covered Postman Assertions using expect keyword. Also there are many other assertions in Postman which works around Postman Sandbox which we covered in pre-request scripts in Postman. In this tutorial we will talk about some common Different types of Asserts in Postman.

In this tutorial we will be focusing on the response part because it is extremely important as we almost all the time need to assert on the response. So we will apply assertions on:

  • Response time
  • Status Code
  • Status code meaning
  • Response Type
  • Response header
  • Post method check
  • String in Response

 

 

Different types of Asserts in Postman

For instance we can think of sending a request and checking all the above stated things on the same. In the end of this tutorial, you can also add all the assertions in one single request to practice and improve your skills. So, we will start now.

Prerequisite:

 

Assert on Response Time

This assert helps us to verify the Response Time of the Request. Below we are verifying that if the Response Time is less than 100ms. Go to the Tests tab and write the following code:

pm.test(“Response time is less than 100ms”, function () {
     pm.expect(pm.response.responseTime).to.be.below(100);
});

Response_Code_Chai

 

NOTE: This assertion can also be modified to check the time to be above a certain value (to.be.above(value)) and equal to a certain value (to.be.equal(value)).

 

Press Send and see the response.

Response_Code_Chai_Response

Note: In the above case, Assert got failed, as the response time was 1121ms. Also, the same is visible clearly in the response box as AssertionError: expected 1121 to be below 100 which is false obviously.

 

 

Assert on Response Status Code

This assertion is based on checking the Response Status Code. In the below test,  we are verifying that if the Response Status Code is 200. Test will PASS in case of Status Code 200, else it will FAIL in case of any Status Code other than 200. Write the following code in the Tests tab:

pm.test(“Status code is 200”, function () {
     pm.response.to.have.status(200);
});

Status_Code_200_Chai

 

You can place any status code inside the value box to check the value of the status. The same can also be expressed in Chai Assertion Library as

For checking status being OK

pm.test(“Status is OK”, function () {
     pm.response.to.be.ok;
});

 

For checking status being BAD REQUEST

pm.test(“Status is Bad Request”, function () {
     pm.response.to.be.badRequest;
});

 

 

Press send and see the response which is true in my case.

Status_Code_200_Chai_Response

We got the response status code to be 200 and hence our assertion has passed.

 

 

Assert on Response Status Code Meaning

This assertion is based on checking a specific property. In this assertion we will check a specific property and its value. In the example given below we are checking the property status and its value being OK.

Write the following code inside Tests tab.

pm.test(“Status is OK”, function(){
     pm.response.to.have.property(‘status’, ‘OK’);
});

Status_Code_OK_Chai

 

Press Send and see the result which will be true in my case.

Status_Code_OK_Chai_Response

This one was quite understandable, I guess.

 

 

Assert on Response Type

This assertion is based on verifying the Response Type. In the below test,  we are verifying that if the Response Type is JSON. Write the following code in the Tests tab:

pm.test(“Response if Json”, function(){
     pm.response.to.be.json;
});

Response_Json_Chai

Note: I hope you remember that in Get Request when we sent the request using weather api, we received the response in the text format rather than JSON format. We are using the same API here.

 

Press Send and see the result.

Response_Json_Chai_Response

The assertion has failed because of the response type. We expected response type to be JSON, but the response that we get in weather api is in the TEXT format.

 

 

Assert on Response Header

This assertion is based on checking whether the header has content-type or not.

Write the following in your tests tab

pm.test(“Content-Type is present”, function () {
     pm.response.to.have.header(“Content-Type”);
});

Response_To_Have_Content_Type

 

This assertion checks if the content-type header is present in the response or not. Press Send and see if it is or not.

Response_To_Have_Content_Type_Response

 

Yes, the test passed. But, how can we check if it was really present or not. As you can see besides Test ResultsHeaders is written. Go to Headers and Content-Type must be present there.

Content_Type_Header

 

So now we have seen the assertions that are commonly used. Now, we will try to use both Chai Assertion along with these assertions to create some meaningful tests.

 

 

Assert for Multiple Status Code

For this we will be using the customer register api since it uses POST method type to send the request or you can also use Weather API but ultimately the test will fail. You can download both the APIs from here.

Go to tests tab and write the following code

pm.test(“Successful POST request”, function () {
     pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

Successful_Post_Request

Note: 201 is created and 202 is Accepted.

Press send and see the response which will be pass if the status code is 201 or 202 or else will fail.

 

 

Assert on Response Text

Check if response contains a string

Write the following code in the tests tab of any API which is correct and gives response.

pm.test(“Body matches string”, function () {
     pm.expect(pm.response.text()).to.include(“string_you_want_to_search”);
});

Replace the query “string_you_want_to_search” with the string you want to search. If your response will contain the string your assertion will pass or else fail.

Press send and see the response.

 

var schema = {
 "items": {
 "type": "boolean"
 }
};
var data1 = [true, false];
var data2 = [true, 123];

pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(data1, schema)).to.be.true;
  pm.expect(tv4.validate(data2, schema)).to.be.true;
});


Assert if substring exists in target

  pm.test("Check if pattern is in target string",function () {       pm.expect('foobar').to.have.string('bar');   });


Assert the type of the target is equal to the given string type

    pm.test("Check if target is string", function () {      pm.expect('Postman').to.be.a('string');     }); 
    pm.test("Check if target is an object", function () {      pm.expect({a: 1}).to.be.an('object');     }); 
    pm.test("Check if target is undefined", function () {      pm.expect(undefined).to.be.an('undefined');     });

Assert that the target contains the keys passed

    pm.test("Check if object contains all provided keys", function () {      pm.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');     }); 
    pm.test("Checking if object contains any ONE of the keys", function () {      pm.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');     }); 
    pm.test("Check if object contains any NONE of the provided keys", function () {      pm.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');     }); 

Assert that the target contains said property

    pm.test("Check if object contains the property", function () {      pm.expect({a: 1}).to.have.property('a');     }); 

Note:

  1. Target can be an objectsetarray or map.
  2. If .keys is run without .all or .any, the expression defaults to .all.
  3. As .keys does different things based on the target’s type, it’s recommended to check the target’s type before using .keys using .a.
    pm.test("Check if object contains all the keys", function () {      pm.expect({a: 1, b: 2}).to.be.an('object').that.has.all.keys('a', 'b');     }); 

Assert the length of target

    pm.test("Check the length of the target", function () {      pm.expect('foo').to.have.lengthOf(3);     }); 
    pm.test("Check the size of the target", function () {      pm.expect([1, 2, 3]).to.have.lengthOf(2);     }); 

Assert that the target array has the same members as the given array set

    pm.test("Check if the target has same members as the array set", function () {      pm.expect([1, 2, 3]).to.have.members([2, 1, 3]);     }); 

Note:

  1. By default, .members makes strict comparison.
  2. The order of members is irrelevant.



For learning more about it we strongly recommend reading the documentation of Chai Library and Postman Sandbox.

So in this tutorial we asserted some assertions which executes under Postman Sandbox. These assertions are very helpful for a tester as he can complete his testing more easily and effectively. I hope you have got an idea of what assertions are and how to use them. You can combine the both Chai assertion and postman’s assertion to create your own custom assertions and see if you got it right or not. We will move onto our next tutorial now. Till then, keep practicing assertions. They are the heart of testing through Postman.



眼镜蛇

posted on 2020-02-21 15:10 眼镜蛇 阅读(139) 评论(0)  编辑  收藏 所属分类: Test Tools(Postman.etc.)


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜