1. Install

Two ways to install, I am keen to install chrome plugin Mac App in the form of chrome plugin

2. Send the request

Postman’s most basic function is to send HTTP requests. It supports GET/PUT/POST/DELETE and many other HTTP methods that I don’t know about.

You can send a request by filling in the URL, header, body, etc., which is good enough for simple tests.

If your application requires login authentication, you can fill in the Authorization to meet your requirements. You can also use cookies that are already logged in to the Chrome browser. To synchronize cookies from the browser, you need to install another plug-in, Interceptor. It allows you to Request data that already exists in the browser with the header when you send a Request. It also allows you to write the browser’s Request to postMan’s history (Request Capture is enabled).

Set 3.

Each time a request is configured, it can be saved to a collection so that the next test can find the test you want to execute directly from the collection.

Rather than just sorting and storing collections, Postman supports one-click running of tests across the entire collection.

If we can think of a request as a Test Case, then the collection is a Test Suite.

Each collection corresponds to a URL, and you can get your collection URL via the Share button, which can be shared with your teammates or used for Newman execution.

Newman is a command line tool for Postman that allows API testing to be added to your continuous integration tasks.

4. Environment variables

When doing API testing, you may often need to switch between different Settings. For example, you may need to use different configurations for your development environment, your test environment, and your production environment. Postman provides environment variables for this, so you can modify the environment variables without modifying the request.

You can select the environment from the drop-down menu in the upper right corner, and you can view the current environment variables by clicking on the little eye on the right.

5. The API test

Postman Test Sandbox is a JavaScript execution environment that allows you to write pre-requist and test scripts from JS scripts. Pre-requist can be used to modify some default parameters.

The Postman Sandbox integrates several libraries such as LoDash, SugarJs, tv4, and built-in functions such as xml2JSON..

Tv4 is used to verify JSON data by writing JSON Schema. For the syntax of JSON Schema, please refer to here

Test syntax:

Boolean(value) does not equal false, Passtests [description] = value // Example tests["Status code is 200"] = responsecode. code === 200;Copy the code

We making the status of the interface, for example: url: status.github.com/api/status….

tests["Status code is 200"] = responseCode.code === 200;

// validate json schema
var schema = {
  properties: {
      status: {type: 'string'},
      last_updated: {type: 'string'}
  }
};

tests["Valid data schema"] = tv4.validate(responseBody, schema);

// check status
var jsonData = JSON.parse(responseBody);
tests["Github status is good"] = jsonData.status === 'good';
Copy the code

Running results:

The sample

Inspired by httpbin.org/, Postman also provides a starting API, dump.getPostman.com/, which we will use to do a full test.

1. Create an environment variable



Click on theManage EnvironmentsAnd then clickAdd



Add a URL variable that we’ll use later

2. Request a new user

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

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

{
  "username": "abhinav",
  "password": "abc"
}Copy the code

This interface does not seem to support the creation of a user, we assume that it has been created successfully, because it does not affect our subsequent operations

3. Obtain the user’s Token

Token Is used to grant terminal request and access rights. Get tokens for users by Posting username and password request {{url}}/blog/users/tokens/. These tokens will be used in other requests.

{
  "username": "abhinav",
  "password": "abc"
}Copy the code

4. Format JSON

We need to get the user Token and user ID from the result of the above request and save these two values in environment variables 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;
}Copy the code

5. Create an article

User_id and token are automatically added to the environment variables if the above tests are performed in the main window or in the collection runner. To create a POST, we need to send a POST request to {{url}}/blog/posts and add the user_id and token to the URL parameters. The request Body for POST looks like this:

{
  "post": "This is a new post"
}Copy the code

6. Check the returned data

JSON with post_id will be returned if the above request succeeds. Here we will verify that the article was created successfully and save the article ID to the 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;
}Copy the code

7. Get an article and validate JSON

We will get the article we created by the article ID returned above. Here we will use Postman’s 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);
 
tests["Valid schema"] = result.valid; Copy the code

8. One-click run and share collections

We saved each of the above tests into a collection of PostManTests so that we can open and run any tests you want at any time, and run them all with one click, share the collection with your friends, and get the embedded code (button below).

All of the test cases for this article are here

reference

www.getpostman.com/docs