DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Web Development Checklist
  • What Is React? A Complete Guide
  • What Is mTLS? How To Implement It With Istio

Trending

  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Web Development Checklist
  • What Is React? A Complete Guide
  • What Is mTLS? How To Implement It With Istio

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.

Antonio García Maestre user avatar by
Antonio García Maestre
·
Apr. 14, 17 · Tutorial
Like (11)
Save
Tweet
Share
40.37K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Testing

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

Opinions expressed by DZone contributors are their own.

Trending

  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Web Development Checklist
  • What Is React? A Complete Guide
  • What Is mTLS? How To Implement It With Istio

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: