Postman Interface Test Tutorial __ Test

Source: Internet
Author: User
Tags call back new set
Reprint Address: https://blog.csdn.net/wanglha/article/details/52203736
Part I: Basic articles

postman:4.5.1
1. Install Postman
Enter the Postman official website, if the Mac system can be directly clicked Mac App installation


If it is windows, you need to install the Chrome browser under Windows and then enter chrome://extensions in the Address bar to check the developer mode


Then the download of the Postman plugin decompression, browser Click to load the unpacked extender, select the plugin directory on it

2. Test the first API, take the Watercress developer API as an example


The method for this interface is get,postman default to get, and input the tested interface in the URL bar


In the interface document, Q and tag one of them is required, so this requires configuration parameters, click on the right side of the URL params, and input parameters


Click the Send button to send the request, where you can see the returned response, the response status code, and the response time


Response can also be viewed in different ways, and you can also see cookies,headers information


Each time with different parameters or settings, on the left side of the history will help us record an interface Test history, so that the next call back

Part II: Advanced text

1. Test the interface with your own script
Click the Tests button, you can see the right side of a snippets bar, which is the Postman built-in test script, auxiliary to test the interface


Select one of the scripts, modify the parameters in it, and click Send again, when response's tests appears with a 1/1 indicating that a test has been performed and passed the test, and the Green pass description verifies that the


Red fail shows that the test does not pass, an interface can write multiple scripts to test many times

2. Built-in script description

 1. Clears a global variable clear a global variable corresponding script: postman.clearglobalvariable ("Variable_key"); Parameter: Key 2 of the variable that needs to be cleared. Clears an environment variable clear an environment variable corresponding script: postman.clearenvironmentvariable ("Variable_ke
    Y "); Parameter: Key 3.response of environment variable that needs to be cleared contains content Response body:contains string corresponding script: tests["body matches string"] =responseb
    Ody.has ("String_you_want_to_search"); Parameters: expected content 4. Convert XML format Response to son format Response body:convert xml body to a JSON Object counterpart script: var jsonobject = XM
    L2json (responsebody); Parameters: (default does not need to set parameters, Response for the interface) need to convert XML 5.response equals expected content Response Body:is equal to a string corresponding script: tests["Body I
    s correct "] = Responsebody = =" Response_body_string "; Parameters: Response 6.json value of the resolution key is expected to be validated Response Body:json value check corresponding script: tests["Args key contains argument pass ed as url parameter "] = ' test ' in Responsejson.args parameter: Test replaces the measured value, args replaces the measured key 7. Check response header information for divination of Word segment Res Ponse Headers:content-typeHeader check corresponding script: tests["Content-type is present"] = Postman.getresponseheader ("Content-type"); Parameters: Expected header 8. Response time Response the less than 200ms corresponding script: tests["Response is less than 200ms"] = Res
    Ponsetime < 200; Parameters: Response Time 9. Set global variable variable corresponding script: postman.setglobalvariable ("Variable_key", "Variable_v
      Alue "); Parameter: The key value of the global variable 10. Setting environment variable set an environment variable corresponding script: postman.setenvironmentvariable ("Variable_key
      "," Variable_value "); Parameter: The key value of the environment variable 11. Judge Status Code status Code:code is 200 corresponding script: tests["status code is"] = Responsecode.code!
      = 400; Parameter: Status code 12. Check that the code name contains content status Code:code name has string corresponding script: tests["status code name has STR
      ing "] = ResponseCode.name.has (" Created "); Parameter: Expected code name contains string 13. Successful POST requests Status code:successful POST request corresponding script: tests["successful post R Equest "] = Responsecode.code = = 201 | |

     Responsecode.code = = 202;
         14. Tiny validator Use Tiny Validator for JSON data counterpart script: var schema = {"items": {
        ' Type ': ' Boolean '}};
        var data1 = [true, false];
        var data2 = [true, 123];
        Console.log (Tv4.error);
        tests["Valid Data1"] = Tv4.validate (data1, schema);
        tests["Valid Data2"] = Tv4.validate (data2, schema); Parameters: You can modify the key value pairs in the items to correspond to the parameters for validating JSON

3. Save the API, click the Save button, find the Save As button, click


At this point we can modify the request name and write the description


Next, create a new set of interfaces, write a collection name, click Save


Then on the left side of the function area, locate the Collections column and click


You can see the collection of APIs you just saved

4. Run API collection: Find the top functional area, locate the runner key, and click


Select the new API collection that you just created


Click Start Test to see the results of the run on the right side

5. Command line Run interface collection: First download the collection just created, click Export, and save to the specified path


Install the Postman command line run tool Newman


Note: If prompted without NPM command click on this link
After the Newman is installed, the command line executes and you see the execution results


If you want the report to add the-H filename parameter

Part III: Advanced Articles

1. The use of environmental variables: in the process of testing we may have more than one set of test environment, corresponding to each set of test environment IP may be different, but the interface of the request, and parameters are the same, the most stupid way is every environment to write a test set, But postman provides us with a more convenient way: To use environment variables
Enter the request column and find no environment


Click No environment, Find manager environments, click to enter the page
! [Uploading qq20160812-2_610634.png ...]



This page can be used to manage environment variables, click Add, add an environment variable


Enter the name of the environment variable here, the environment variable is in the form of a key value pair, enter the key value pairs of the environment variable, then click Add to Save


Back to the request test area, click No environment, select the newly created environment variables, and click on the right side of the eye to view the environment variables and global variables (later will introduce to you)


Now use the newly created environment variable to replace the URI, the reference variable uses the form of {{key}}, replace the https://echo.getpostman.com with {{test_env}}, and send the request


You can see that the results here are the same as using https://echo.getpostman.com, so that a set of interfaces can be written once and switched to different environments by changing the environment variables.

2. Use of global variables: Although we may have multiple test environments, our test data is used in one group for each environment, and for postman is equivalent to just one set of global variables, corresponding to multiple sets of environment variables
Again, click Manager Environment


Click Globals to enter the management page of the environment variable


The global variable globals is also saved as a key value pair, input key value pairs, click Save


Go back to the request test area, use global variables to replace the URL's parameters, refer to the environment variable reference method consistent, replace and click Send, view the results


Response still shows up in the right content.

3. You can use the Pre-request script feature to create environment variables and global variables, as well as to destroy environment variables and global variables, pre-request scripts are out-of-the-script, executed before the request
Click Pre-request script to go to the preset scripts page


Click on the script on the right, and set the global variable and environment variables, and replace the URL, click Send sent, see the results or in the correct form of the show


Similarly, we can use preset scripts in tests to set up the same way as to clear the environment variable usage.

4. Environment variables and global variables have been set up, we can also be referenced inside the tests, so that the problem solved the data transfer from top to bottom
Click Tests, switch to test script page, enter script





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.