<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 眼鏡蛇 閱讀(153) 評論(0)  編輯  收藏 所屬分類: Test Tools(Postman.etc.)


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 精品女同一区二区三区免费播放| 成全动漫视频在线观看免费高清版下载| 又粗又大又长又爽免费视频| 一级毛片免费观看不收费| 久久精品亚洲中文字幕无码网站| 亚洲成人免费电影| 大桥未久亚洲无av码在线 | 青青久在线视频免费观看| 亚洲av无码日韩av无码网站冲 | 亚洲 日韩 色 图网站| 亚洲AV伊人久久青青草原| 香蕉免费一区二区三区| 无码亚洲成a人在线观看| 亚洲另类激情综合偷自拍| 国产精品视频免费一区二区三区| 美女视频黄的免费视频网页| 亚洲日韩看片无码电影| 亚洲成色WWW久久网站| 国产精品美女自在线观看免费| 日韩免费视频一区二区| 亚洲heyzo专区无码综合| 久久精品国产96精品亚洲| 国产99视频免费精品是看6| 日韩人妻一区二区三区免费| 麻豆一区二区三区蜜桃免费| 亚洲最大在线视频| 亚洲色成人WWW永久网站| 国产高清免费在线| 亚洲精品免费网站| 免费视频精品一区二区三区| 国产成人不卡亚洲精品91| 亚洲国产综合自在线另类| 亚洲人成伊人成综合网久久久| 国产又大又粗又硬又长免费| 57PAO成人国产永久免费视频| 中文字幕在线免费观看视频| 成人精品国产亚洲欧洲| 亚洲ts人妖网站| 亚洲一本综合久久| 亚洲色精品aⅴ一区区三区| 日韩精品电影一区亚洲|