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


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


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

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产黄片不卡免费| 久久国产免费福利永久| 久久国产亚洲电影天堂| 国产乱子精品免费视观看片| 亚洲日韩AV一区二区三区中文 | 国产亚洲精品免费| 亚洲色精品88色婷婷七月丁香| 99久久人妻精品免费二区| 亚洲精品第一国产综合亚AV| 青青草原亚洲视频| 日韩精品无码区免费专区| 一个人看的免费视频www在线高清动漫| 久久噜噜噜久久亚洲va久| 国产亚洲精品免费| 8x8x华人永久免费视频| 国产午夜亚洲精品不卡电影| 亚洲综合在线视频| 亚洲精品无码久久毛片| 免费在线看v网址| 成人毛片100免费观看| 亚洲成_人网站图片| 亚洲国产精彩中文乱码AV| 国产精品无码素人福利免费| 免费91最新地址永久入口| 国产亚洲精品国产福利在线观看| 亚洲经典在线中文字幕| 丝袜熟女国偷自产中文字幕亚洲| 好男人看视频免费2019中文| 69视频在线是免费观看| 国产精品福利片免费看| 亚洲欧美黑人猛交群| 亚洲网站在线播放| 亚洲精品色午夜无码专区日韩| 日韩午夜免费视频| 在线观看成人免费视频不卡| a级毛片黄免费a级毛片| 日韩电影免费在线观看网址| 亚洲人精品亚洲人成在线| 日韩精品亚洲人成在线观看| 亚洲色婷婷一区二区三区| 国产美女无遮挡免费视频|