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.
Join the DZone community and get the full member experience.
Join For FreePostman 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.
Click on the Add button and a new window appears where you can set the name of your environment and your variables.
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.
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.
- Click on the Test tab.
- At the right menu, there is a JavaScript snippet. Click over Response body: JSON value check and over Set an environment variable.
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.
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!
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