Postman-based API Automation testing

Source: Internet
Author: User

1190000005055899

1. Installation

Two mounting options, I'm passionate about installing as a chrome plugin
Chrome Plugin
Mac App

2. Sending the request

Postman's most basic function 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 app requires login verification, you can fill in the authorization to meet your needs.
You can also use a cookie that has already been signed in by Chrome, and a cookie that synchronizes the browser requires another plugin Interceptor(Interceptor) to be installed. It can help you to request the data that already exists in the browser when you send the request, in addition it can write the browser's request to the Postman history (need to open "request Capture"
)。

3. Collection

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 perform directly from the collection.

The collection is not just a classification and storage feature, Postman supports testing in the entire collection with a single key run.

We can think of a request as a test case, so 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 used to share to your teammates or 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

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. The environment variable is provided for this postman so that you can modify the environment variable without modifying the request.

You can select the environment from the drop-down menu in the top right corner, and you can view the current environment variables by tapping the small eyes on the right.

5. API Testing

The postman Test sandbox is a JavaScript execution environment that allows you to write pre-requist and test scripts through JS scripts. Pre-requist can be used to modify some 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 JSON schema to verify that the syntax of the JSON schema can be referenced here

Test syntax:

// description 为该测试的描述// value 只要Boolean(value)不等于false,这个测试就是PASStests[description] = value// exampletests["Status code is 200"] = responseCode.code === 200;

Let's take the GitHub status interface as an example:
Url:https://status.github.com/api/status.json

tests["Status code is 200"] = responseCode.code === 200;// validate json schemavar schema = {  properties: {      status: {type: ‘string‘}, last_updated: {type: ‘string‘} }};tests["Valid data schema"] = tv4.validate(responseBody, schema);// check statusvar jsonData = JSON.parse(responseBody);tests["Github status is good"] = jsonData.status === ‘good‘;

Operation Result:

Example

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

1. Create an environment variable


Tap Manage Environments , and then tapAdd


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"}

This interface does not seem to support the creation of the user now, we assume that the creation is successful, because it does not affect our subsequent operations

3. Get the token of the user

Token is used to grant terminal requests and access rights. We can request {{url}}/blog/users/tokens/to get the token of the user via the post username and password, which will be used in other requests.

{  "username": "abhinav",  "password": "abc"}

4. Formatting JSON

We need to get the user token and user ID from the result of the request above, and save the two 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 test is executed in the main window or in the collection runner, then it is user_id token automatically added to the environment variable.
In order to create an article, we need to send a POST request to {{url}}/blog/posts, user_id and token add it in the URL parameter. The POST request body is as follows:

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

6. Check the return data

If the above request succeeds, it will return a JSON with it post_id . Here we will verify whether 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;}

7. Get an article and verify the JSON

We will get the article we created by using the article ID 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": "Properties": { "content" :  "string",  "Created_at":  "integer" ,  "id":  "integer"},  "required": [ Span class= "hljs-string" > "content",  "Created_at",  "id"}; var data =  Json.parse (responsebody); var result = Tv4.validateresult (data , schema); tests[ "Valid schema"] = Result.valid;      

8. One click to run and share the collection

We save each of these tests to the Postmantest collection so that we can open and run the tests you want at any time, and you can run them all with one click, or share them with your partner, or you can get embedded code (such as the buttons below).

All of the test cases in this article are here

Reference

Https://www.getpostman.com/docs

    • Published on May 04, 2016

Postman-based API Automation testing

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.