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


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


    網站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 精品视频在线免费观看| 涩涩色中文综合亚洲| 亚洲中文字幕无码爆乳AV| 又色又污又黄无遮挡的免费视| 大学生美女毛片免费视频| 国内自产拍自a免费毛片| 成人au免费视频影院| 在线观看91精品国产不卡免费| 韩国二级毛片免费播放| 国产成人免费福利网站| 亚洲国产综合精品中文字幕 | 日本不卡免费新一区二区三区| 日批视频网址免费观看| 午夜免费啪视频在线观看| 久久国产色AV免费观看| 四虎永久在线精品免费观看视频| 日本亚洲免费无线码| 成年女人色毛片免费看| 国产成人无码区免费A∨视频网站| 国产一区视频在线免费观看 | 成人毛片免费视频| 日本免费电影一区| 亚洲精品国产电影| 亚洲成a人片在线观看日本| 亚洲一区二区成人| 国产亚洲中文日本不卡二区| 含羞草国产亚洲精品岁国产精品 | 亚洲av无码专区在线观看下载| 免费看内射乌克兰女| 99精品免费视品| **实干一级毛片aa免费| 免费黄网在线观看| 伊人亚洲综合青草青草久热| 久久亚洲精品无码AV红樱桃| 亚洲AV一二三区成人影片| 国产精品亚洲二区在线| 成人免费区一区二区三区| 无码人妻精品中文字幕免费东京热| 免费黄色网址入口| 亚洲综合另类小说色区| 亚洲人成在线播放|