Postman & Newman Scripting — Part 1

karthik Penujuru
TestVagrant
Published in
4 min readOct 17, 2019

--

Most of the technical teams I have worked with had a similar question: Why do you need to test API’s? And what benefits we get of testing API’s when we already have a good set of UI tests?

Back in the days, we use to think, test automation means interacting with User Interface (UI) with selenium or some other tool. But actually, UI automation is a subset of test automation. And moreover, this UI automation has few drawbacks like:

  1. Time-intensive Development: Scripting of these UI tests takes a lot of time and maintaining those scripts is a much more painful task.
  2. Fragility: With each passing day we have new technologies taking over on front-end development. Like-we use to have JS Html and CSS but now we have React Based applications which make our automation scripts more fragility.
  3. Slow Execution: UI Scripts takes lots of time to execute and get results from it. Even with a pretty good performance speed of application and parallelization technique to execute those.
  4. Reduce test coverage: It’s hard to replicate every business logic in the UI automation because of environmental, UI interactions or a set of background work to be done for it.
Test Pyramid

According to Test Pyramid: UI tests shouldn’t be acting as end-to-end tests. And moreover, those have to be unit tests for our front-end with backend stubbed out.

Ok, now we got the answer to why! let’s look into how?

In the market, we have a good number of open-source API testing and automation tools. Out of which, Postman application is the most widely used tool for it. Most of us think Postman is for exploratory testing but it also has a great feature to automate the API’s.

API Automation Scripting using Postman tool:

  • Test Editor & Pre-request Script tab:

Postman has tabs named Tests & Pre-request Script in the request editor window where we can set some pre-request variables required for the HTTP request to be sent and in the tests folder to write the tests to be run. And in the response Tests tab tells us whether the test has been passed or failed.

Test Editor of Postman
  • Test Snippets:

To the right of the Tests editor, you can find a list of test snippets that will help us to write tests fast. You can find snippets like Checking the response code, the Response body contains the string e.t.c

Postman Test Snippets

And moreover, postman also allows us to write our custom snippets in JS.

  • Postman Test Environments:

To test any set of API’s we do need a set of test data and also we will be requiring to share data generated by one API to another. To overcome this postman has a feature called Environments.

Postman Environment

And postman has two different types of environments called Global and Environment. So Global variables are used across all the collections in the postman while the environment variables are used for one set of collection at one time.

Scope of variables in postman

And to call these environment variables in the API call we set the variables name enclosed in double flower brackets in the HTTP request like {{variable_name}}. And to call these environments variables in the Tests tab we can use the test snippet pm.environment.get("variable_name")

Postman also allows us to choose a data file in CSV format to get the test data while running the collection.

  • Chaining of Requests:

To check a business logic we may need to be executing a set of HTTP requests and for automating those we need to be chaining them. To do that we can add a test snippet like:

postman.setNextRequest("request_name");
  • Postman Runner window:

Now our scripts are ready to execute but how to run those as a set and get the results of it?

Postman has a feature called Postman Collection Runner which is at top left corner. When we choose it a separate window named collection runner will be opened to choose the set of collection for running, test environment or data file for running it, and the number of iterations e.t.c

Postman Collection Runner

After the run, we get an option named Export Results which will download the results of the run in a JSON file.

--

--

Recommended from Medium

Lists

See more recommendations