Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing APIs With Postman

DZone's Guide to

Testing APIs With Postman

Learn how to set up and use a simple API testing application, Postman, to create environment, pull data, and, of course, run our tests.

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Postman gives you the possibility to share your collection for testing APIs through the cloud allowing easy collaboration among all team members.

A very common scenario while testing APIs is the infrastructure might be present on your local machine, a staging setup, and a production setup. For this purpose, you can set different environments to switch contexts.

Creating Environments

You can hover over the ‘Setting’ icon to create a new environment.

setting enviroment postman

Click on the Add button and a new window appears where you can set the name of your environment and your variables.

manage enviroments postman

Repeat the same operation for a staging and a production setup.

Variables can be used in the following way – {{variableName}}. The string {{variableName}} will be replaced with its corresponding value. For example, for an environment variable ‘server’ with the value ‘localhost’ you will have to use {{server}} in the request URL field. {{server}} will be replaced by ‘localhost’ when the request is sent.

url vars postman

Variables can be used almost everywhere in Postman. They are available inside:

  • URLs.
  • URL parameters.
  • Header values.
  • form-data/url-encoded values.
  • Raw body content.
  • Helper fields.

In order to test our environments, select an environment from the environment selection drop-down and hit send. Once an environment is selected, Postman will replace all instances of a variable with its corresponding value.

Repeat until all environments are tested.

Extracting Data From Response and Chaining Request

Environment and global variables let you keep track of everything that affects API state. Some examples of common variables you would use with an API are session tokens and user IDs.

Call postman.setEnvironmentVariable(key, value) or postman.setGlobalVariable(key, value) to set a variable with values you have extracted from the response.

  1. Click on the Test tab.
  2. At the right menu, there is a JavaScript snippet. Click over Response body: JSON value check and over Set an environment variable.

test1_postman

Remove the line that starts with test, and set the value of “variable_key” to json.Data.value as you can see in the image below.

test2_postman

Now, you will have set the variable for the next request.

Writing and Running Tests

The easiest way to get started writing tests is to use the snippets. Click on Status code: Code is 200 and Postman will generate a line with a check that the code of the response is 200.

The collection runner runs requests in a collection in the order in which you set them. It also runs tests for every request and gives you an aggregate summary of what happened. It stores all your test runs so you can compare them and see what has changed. Need to test all those use cases? It’s super easy with the Collection Runner.

Select a collection, an environment if needed, and hit the Run button.

Postman displays the parameters you started the run with, it also shows you how many tests were passed out of all the tests that were executed. The average response time for the run is also displayed.

Check out the docs for more Postman features and feel free to comment should you have any questions!

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
api testing ,api tests ,rest api ,postman ,web services

Published at DZone with permission of Antonio García Maestre. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}