Master Postman in one article (a must-read for newbies)

What is Postman?

Postman is an extensible API testing tool that can be quickly integrated into CI/CD pipelines. It was started in 2012 as a side project of Abhinav Asthana to simplify API workflows in testing and development. API stands for Application Programming Interface, which allows software applications to communicate with each other through API calls.

Why use Postman?

Today, Postman software has more than 4 million users and has become the preferred tool for interface debugging/testing for the following reasons:

  1. Convenience - To use Postman tools, as long as you have the Postman application installed on your computer and log in to your account, you can easily access your files anytime, anywhere.

  2. Usage of collections – Postman allows users to create collections for their Postman API calls. Each collection can create subfolders and multiple requests. This helps organize your test suite.

  3. Collaboration – Collections and environments can be imported or exported to easily share files. Direct links can also be used to share collections.

  4. Create environments - Having multiple environments helps reduce duplication of testing because the same collection can be used for different environments. This is where parameterization takes place, which we will discuss in a future lesson.

  5. Create tests – Test checkpoints (such as verifying whether the HTTP response status is successful) can be added to each Postman API call, which helps ensure test coverage.

  6. Automated testing – By using Collection Runner or Newman, tests can be run in multiple iterations, saving time on repetitive testing.

  7. Debugging – The Postman console helps to check what data was retrieved, making it easy to debug tests.

  8. Continuous Integration – With its ability to support continuous integration, development practices can be maintained.

How to execute API using Postman

Below is the Postman workspace. Let’s explore step by step how to use the Postman tool.

picture

  1. New – Create a new request, collection or environment here.

  2. Import – used to import a collection or environment. There are options to import from files, folders, link or paste original text.

  3. Runner – Automated testing can be executed through Collection Runner. This will be discussed further in the next lesson.

  4. Open new window – Click this button to open a new tab, Postman window, or Runner window.

  5. My Workspace – You can create a new workspace individually or as a team.

  6. invite – Collaborate in the workspace by inviting team members.

  7. History – Requests you have sent in the past will appear in the history. This makes it easy to track what you do.

  8. collection – Organize your test suite by creating collections. Each collection may have subfolders and multiple requests. Requests or folders can also be copied.

  9. Requests tab – Displays the title of the request being opened. By default, "Untitled Request" will be displayed for requests without headers.

  10. HTTP Requests – Clicking this button will display a drop-down list of different requests, such as GET, POST, COPY, DELETE, etc. In Postman API testing, the most commonly used requests are GET and POST.

  11. Request URL – API can be entered here.

  12. Save – If the request has changes, you must click Save to avoid new changes being lost or overwritten.

  13. params – Parameters required for the request, such as key values, can be written here.

  14. Authentication – In order to access the API, appropriate authorization is required. It can be in the form of a username and password, a bearer token, etc.

  15. headers – Headers can be set according to the needs of the organization, such as content type JSON.

  16. body – Details commonly used in POST requests can be customized here.

  17. Request preprocessing scripts – These scripts will be executed before the request. Typically, a pre-request script that sets up the environment is used to ensure that the tests will run in the correct environment.

  18. Test Modules – These are scripts that are executed during requests. Conducting testing is important because it sets checkpoints to verify that the response status is OK, that the data retrieved is as expected, and other tests.

Use GET request

Get requests are used to retrieve information from a given URL. We will use the following URLs in all examples in this Postman tutorial

https://jsonplaceholder.typicode.com/users		

in workspace

  1. Set the HTTP request to GET.

  2. Enter the link in the Request URL field

  3. Click to send

  4. You will see a 200 OK message

  5. There should be 10 user results in body, indicating that the test you wrote ran successfully.

picture

Handle POST requests

Post requests differ from Get requests because data manipulation occurs when the user adds data to the endpoint.

Step 1) Click on the new tab to create a new request.

picture

Step 2) In new tab

  1. Set the HTTP request to POST.

  2. Enter the same link in the request URL: https://jsonplaceholder.typicode.com/users

  3. Switch to the "body" tab

picture

Step 3) In body,

  1. click original

  2. Select JSON

picture

Step 4) Copy and paste only one user result from the previous get request as shown below. Make sure you have copied the code correctly using pairs of braces and square brackets. Change id to 11 and name to whatever you want. You can also change other details, such as your address.

[
    {
        "id": 11,
        "name": "Krishna Rungta",
        "username": "Bret",
        "email": "[email protected]",
        "address": {
            "street": "Kulas Light",
            "suite": "Apt. 556",
            "city": "Gwenborough",
            "zipcode": "92998-3874",
            "geo": {
                "lat": "-37.3159",
                "lng": "81.1496"
            }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
            "name": "Romaguera-Crona",
            "catchPhrase": "Multi-layered client-server neural-net",
            "bs": "harness real-time e-markets"
        }
    }
]

picture

picture

Step 5) Next,

  1. Click Send.

  2. Status: Display 201 created

  3. The published data is displayed in the body.

picture

How to parameterize a request

Data parameterization is one of Postman's most useful features. Instead of creating the same request with different data, you can use variables with parameters. This data can come from data files or environment variables. Parameterization helps avoid repeating the same tests, and iteration can be used for automated testing.

Parameters are created by using double curly braces: { {sample}}. Let's look at an example of using parameters in a previous request:

picture

Now let's create a parameterized get request.

step 1)

  1. Set HTTP request to GET

  2. Enter this link: https://jsonplaceholder.typicode.com/users. Replace the first part of the link with a parameter, such as { {url}}. The request url should now be { {url}}/users.

  3. Click Send.

Because we haven't set the source of the parameter yet, there shouldn't be any response.

picture

Step 2) Using parameters requires setting environment variables

  1. Click on the "eye" icon

  2. Click Edit to set the variable to a global environment that can be used in all collections.

picture

Step 3) In the variable,

  1. Set the name to the url i.e. https://jsonplaceholder.typicode.com

  2. Click "save".

picture

Step 4) If you see the next page, click Close

picture

Step 5) Return the request and click Send to display the request results.

picture

How to create a Postman test

Postman tests are JavaScript code added to requests that help users verify results, such as success or failure status, comparison of expected results, and more. It usually starts with pm.test. It can be compared with assertion, verification commands available in other tools.

Let's use Postman to do some basic API testing of the parameterized requests from the previous lesson.

Step 1) Go to the GET user request from the previous tutorial.

  1. Switch to the Test tab, with the snippet code on the right.

  2. In the Code Snippets section, click Status Code: The code is 200.

picture

Step 2) Now click Send to display the test results.

picture

Step 3) Back to the Tests tab, let’s add another test. This time we compare expected results with actual results.

In the Code Snippets section, click Response body: JSON value check. We will check if Leanne Graham has user ID 1.

picture

Step 4)

  1. Replace "Your test name" in the code with "Check if the user with id1 is Leanne Graham" so that the test name specifies exactly what we are testing.

  2. Replace jsonData.value with jsonData[0].name. To get the path, first check the body in the get result. Since Leanne Graham's user ID is 1, jsonData is in the first result, which should start from 0. If you want to get the second result, use jsonData[1] etc. to get subsequent results.

  3. In eql, enter "Leanne Graham"

picture

Step 5) Click Send. There are now two test results that passed for the request.

picture

How to create collections

Collections play an important role in organizing test suites and can be imported and exported, making it easy to share collections between teams. In this tutorial, we will learn how to create and execute collections.

Let's start creating a collection:

Step 1) Click the New button in the upper left corner of the page.

picture

Step 2) Select collection and the creation collection window will pop up.

picture

Step 3) Enter the desired collection name and description and click Create.

picture

Step 4) Return to the previous Get request and click "Save"

picture

Step 5)

  1. Select the Postman test collection.

  2. Click "Save to Postman Test collection"

picture

Step 6) The Postman Test collection now contains a request.

picture

Step 7) Repeat steps 4-5 for the previous Post request so that the collection now has two requests.

picture

How to use Collection Runner to run a collection

There are two ways to run a collection, Collection Runner and Newman. Let's first execute the collection in Collection Runner.

Step 1) Click the "Runner" button next to the "import" button at the top of the page.

picture

Step 2) The Collection Runner page should look like this, below is the description of each field

picture

Step 3) Run the Postman test collection by setting the following:

  • Select Postman Test collection-set the number of iterations to 3

  • Set delay to 2500 milliseconds

  • Click the Run... button

picture

Step 4) After clicking the "Run" button, the "Run Results" page should be displayed and you can see the execution of the test.

  1. Once the test is complete, you can view the test status (passed or failed) and the results of each iteration.

  2. You will see the passing status of the get request

  3. Since we didn't do any testing on the Post, there's a message saying that the request didn't do any testing.

picture

How to run a collection using Newman

Another way to run a collection is through Newman. The main differences between Newman and Collection Runner are as follows:

  1. Newman is an add-on to Postman. You need to install it separately from the native application.

  2. Newman uses the command line, while Collection Runner has a GUI.

  3. Newman can be used for continuous integration.

To install Newman and run our collection from it, do the following:

Step 1) Install nodejs using this link: http://nodejs.org/download/

Step 2) Open the command line and enter

npm install -g newman

picture

Step 3) After installing Newman, let us return to the Postman workspace. In the "collection" box, click on the three dots, the option should now appear, select export.

picture

Step 4) Select Export collection as collection v2.1 (recommended) and click Export.

picture

Step 5) Select the desired location and click "Save". It is recommended to create a specific folder for Postman testing.

Step 6) We also need to export our environment. Click the eye icon next to the environment dropdown in Global and select Download as JSON. Select the desired save location and click Save. It is recommended that the environment and collection are located in the same folder.

picture

Step 7) The environment should now be exported to the same local directory as the Collection.

Step 8) Now go back to the command line and change directory to where you saved the collection and environment.

 cd C:\Users\Asus\Desktop\Postman Tutorial

Step 9) Run the collection using the following command:

 newman run PostmanTestCollection.postman_collection.json -e Testing.postman_globals.json
The results of the run should now look like this.

picture

As a guide, here are some basic Newman codes for execution:

  1. Only run the collection. This method can be used if there are no environment or test data file dependencies.

newman run <collection name>
  1. Run the collection and environment. The -e directive is for the environment.

newman run <collection name> -e <environment name>
  1. Run the collection with the desired number of iterations.

newman run <collection name> -n <no.of iterations>
  1. Run using data files.

newman run <collection name> --data <file name>  -n <no.of iterations> -e <environment name>
  1. Set delay time. This is important because if the test is run without a delay because the request starts before the previous request on the endpoint server has finished processing, the test may fail.

newman run <collection name> -d <delay time>

Finally: The complete software testing video tutorial below has been compiled and uploaded. Friends who need it can get it by themselves [guaranteed 100% free]

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

​​​​

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/132976001