Postman Installation and interface testing _postman

Source: Internet
Author: User
1. Installation

Two ways to install, I am keen to install the Chrome plugin in the form of Chrome plug-in Mac APP 2. Send Request

The most basic function of postman is to send HTTP requests, support Get/put/post/delete, and many HTTP methods I don't know.

By filling in the URL, header, body, etc. can send a request, which we usually do some simple test is enough.

If your application needs to use login verification, you can fill in the authorization to meet your needs.

Alternatively, you can use a cookie that is already logged in by the Chrome browser, and the Sync browser cookie needs to install another plugin Interceptor (Interceptor). It can help you to request the data that already exists in the browser with the header when you send the request, and it can write the browser's request to Postman's history (need to open "request Capture"

)。 3. Collection

Each time you configure a request, you can save it to a collection, so the next test can find the test you want to perform directly from the collection.

Collections do not only have classification and storage capabilities, postman supports the ability to run tests throughout the collection with one key.

We can take a request as a test case, and the collection is a test Suite.

Each collection corresponds to a URL that you can use to get your collection URL, which can be used to share with your teammates or for Newman execution.

Newman is a postman command-line tool that allows API testing to be added to your continuous integration task. 4. Environment variables

As an API test, you may often need to switch between different settings. For example, the API settings for the development environment, the test environment, and the API settings for the product environment, you may need to use different configurations in different test environments. This postman provides an environment variable so that you can modify the environment variable without modifying the request.

You can select the environment by clicking the dropdown menu in the upper right corner to view the current environment variables by tapping the small eyes on the right. 5. API Test

The Postman test sandbox is a JavaScript execution environment that allows you to write pre-requist and test scripts through a JS script. Pre-requist can be used to modify some of the default parameters.

The Postman Sandbox integrates several tool libraries, such as Lodash, Sugarjs, TV4, and some built-in functions such as Xml2json.

TV4 is used to validate JSON data by writing a JSON schema to validate the syntax of the JSON schema refer to Here

Test syntax:

Description for this test description
//value as long as Boolean (value) is not equal to false, this test is pass
tests[description] = value

//Example
tests["Status code is"] = Responsecode.code = = 200;

Let's take the GitHub status interface for example:

Url:https://status.github.com/api/status.json

tests["Status code is"] = Responsecode.code = =;

Validate JSON schema
var schema = {
  properties: {
      Status: {type: ' String '},
      last_updated: {type: ' Stri ng '}}}
;

tests["Valid Data Schema" = Tv4.validate (responsebody, schema);

Check Status
var jsondata = Json.parse (responsebody);
tests["Github status is good"] = Jsondata.status = = ' good ';

Run Results: Example

Inspired by http://httpbin.org/, Postman also provides a set of introductory API http://dump.getpostman.com/, which we will use to do a complete test of the API. 1. Create an environment variable

Click Manage environments, and then click Add

Add a URL variable and we will use 2 in the following. Request a new user

We need to create a user by sending a POST request to {{url}}/blog/users/) and need to attach the following parameters to the request body:

Note: Remember to switch the environment variable to dump.getpostman.com so we can get the {{URL}} variable

{
  "username": "Abhinav",
  "password": "ABC"
}

This interface does not seem to support creating users now, we assume that it has been created because it does not affect our subsequent operations 3. Get the user's token

Token is used to grant terminal requests and access permissions. We can use the post username and password request {{url}}/blog/users/tokens/to get the user's token, this token will be used in other requests.

{
  "username": "Abhinav",
  "password": "ABC"
}

4. Format JSON

We need to get the user token and user IDs from the above request results and save the values in the environment variable for later use. Add the following code to the Test Editor:

var data = Json.parse (responsebody);

if (data.token) {
  tests["body has token"] = true;
  Postman.setenvironmentvariable ("user_id", data.user_id);
  Postman.setenvironmentvariable ("token", Data.token);
}
else {
  tests["body has token"] = false;
}

5. Create an article

If the above tests are performed in the main window or in the collection runtime, then user_id and token are automatically added to the environment variable.

In order to create an article, we need to send a POST request to {{url}}/blog/posts, and add user_id and token to the URL parameter. The request body for the post is as follows:

{
  "POST": "This is a new post"
}

6. Check Return Data

If the above request succeeds, a JSON with POST_ID will be returned. Here we will verify that the article was created successfully and save the article ID to an environment variable. Add the following code to the Test Editor:

var data = Json.parse (responsebody);
 
if (data.post_id) {
  tests["post_id found"] = true;
 
  Postman.setenvironmentvariable ("post_id", data.post_id);
}
else {
  tests["post_id found"] = false;
}

7. Get an article and validate JSON

We'll get the article we created by using the ID of the article returned above. Here we will use the Postman built-in TV4 JSON validator to check the JSON of the server response.

Create a GET request to {{url}}/blog/posts/{{post_id}} and add the following code to the Test Editor:

var schema = {
  "type": "Object",
  "Properties": {
    "Content": "string",
    "Created_at": "Integer",
    "id": "Integer"
  },
  "required": ["Content", "Created_at", "id"]
};
 
var data = Json.parse (responsebody);
 
var result = Tv4.validateresult (data, schema);
 

8. One-click running and Sharing collection

We save each of these tests to a collection of postmantest so that we can open and run the tests you want at any time, and you can run them all at once, or share them with your little partner, or get embedded code (like the button below).

All of the test cases in this article are here

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.