How To Install Postman In Ubuntu 16.04 Using Terminal
How to Install Postman in Ubuntu 16.04 Using Terminal
Postman is a powerful tool for API testing and development. It allows you to send requests, inspect responses, and organize your API workflows. While Postman offers a desktop application, you can also install and use it from the command line, which can be useful for automated testing and scripting. This guide will walk you through installing Postman and setting it up on your Ubuntu 16.04 system using the terminal.
Prerequisites
-
Ubuntu 16.04 LTS (Xenial Xerus): Ensure you have Ubuntu 16.04 installed on your system.
-
Node.js and npm: You need Node.js and npm (Node Package Manager) installed. These are essential for using the Postman CLI. You can install them using the following command:
Terminal window sudo apt-get updatesudo apt-get install nodejs npm
Installing Postman CLI
-
Open Terminal: Open a terminal window by pressing Ctrl+Alt+T.
-
Download the Postman CLI: Use the following command to download and install the Postman CLI package globally:
Terminal window sudo npm install -g newmannpm
: Node Package Manager-g
: Global installation. This allows you to use the Postman CLI from any directory.newman
: The name of the Postman CLI package.
-
Verify Installation: Check if the installation was successful by running the following command:
Terminal window newman -vYou should see the installed version of Newman printed in the terminal.
Using the Postman CLI
1. Create a Collection:
- Collections in Postman organize a group of API requests. A collection can represent an entire API or a specific feature set. You can create a collection using the Postman desktop application or directly using the Postman CLI.
- The Postman CLI expects a collection in JSON format. A sample collection can look like this:
{ "info": { "_postman_id": "b0989f19-2c04-400a-b881-e20f41d7b0f6", "name": "My First Collection", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "Get User", "request": { "method": "GET", "header": [], "url": { "raw": "http://example.com/users", "protocol": "http", "host": [ "example", "com" ], "path": [ "users" ] }, "body": {} }, "response": [] } ] }
- Save this collection: Create a file named
my-collection.json
and paste the code above.
2. Sending Requests with Newman:
- Run a request from a collection: The
newman
command is used to send requests and get results from your Postman Collections. To execute the collection you just created, run the following command:
newman run my-collection.json
- Running a single request: You can also run single requests from a collection directly without needing the entire collection file:
newman run my-collection.json -e get-user.json
- The
-e
option specifies an environment file that contains variables that you can use in your tests. Theget-user.json
file would typically contain variables for endpoint URLs.
3. Environment Variables:
-
Environments allow you to manage configuration settings like API keys, base URLs, and other variables, ensuring you can seamlessly test across various environments (development, staging, production).
-
Create an Environment File: To create an environment variable file, you need to save a
.json
file with the following structure (example below):
{ "id": "630e4624-f71f-43b3-8e66-2535c4004507", "name": "My Dev Environment", "values": [ { "key": "baseUrl", "value": "http://example.com" } ]}
- Use Environments: You can use environment variables in your collection. For example:
{ "info": { "_postman_id": "b0989f19-2c04-400a-b881-e20f41d7b0f6", "name": "My First Collection", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "Get User", "request": { "method": "GET", "header": [], "url": { "raw": "{{baseUrl}}/users", "protocol": "http", "host": [ "{{baseUrl}}", "com" ], "path": [ "users" ] }, "body": {} }, "response": [] } ] }
- Run with specific environment: You can now run your collection using an environment file to make it more flexible:
newman run my-collection.json -e my-dev-environment.json
4. Assertions:
-
Assertions are used to validate different aspects of the response, ensuring that your API functions correctly. Newman supports several ways to write assertions using Javascript.
-
Create Assertions: You can add assertions to your collection using the
test
keyword within your JSON:
{ "info": { "_postman_id": "b0989f19-2c04-400a-b881-e20f41d7b0f6", "name": "My First Collection", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "Get User", "request": { "method": "GET", "header": [], "url": { "raw": "http://example.com/users", "protocol": "http", "host": [ "example", "com" ], "path": [ "users" ] }, "body": {} }, "response": [], "test": [ "pm.test('Status code is 200', function () {", " pm.response.to.have.status(200);", "});" ] } ] }
- Run with Assertions: When the
newman
command runs, it will execute all tests within your collection:
newman run my-collection.json
pm.response.to.have.status(200);
checks that the response status code is 200 (OK).
Generating Reports
-
Generate HTML reports:* You can create detailed HTML reports to visualize the results of your API testing. For this, you need the
newman-reporter-html
package:Terminal window sudo npm install -g newman-reporter-html -
Run tests and create HTML report: This command will generate an
output.html
file in the same location as your collection:Terminal window newman run my-collection.json -r html -o output.html
Note: The Postman CLI (Newman) is a powerful tool for testing APIs and integrating with your testing workflows.
How to Install Postman in Ubuntu 16.04 Using Terminal - Alternative Method
- Postman is now available as a snap package. Snaps offer a convenient and secure way to install applications. Here’s how to use the snap method:
sudo snap install postman
- You can now open Postman from the Ubuntu Dash or by typing
postman
in the terminal.
Other Useful Postman CLI Commands
- Get help:
newman --help
- List reporters:
newman reporters
(to see available report formats) - Run tests with a specific reporter:
newman run my-collection.json -r json
(for example, to get a JSON output report) - Environment Variables:
- Setting environments: Create a file
env.json
with environment variables and run:Terminal window newman run my-collection.json -e env.json - Using environments within tests: Access environment variable
baseUrl
in tests:pm.test("baseUrl is correct", function () {pm.expect(pm.environment.get("baseUrl")).to.equal("http://example.com");});
- Setting environments: Create a file
Additional Resources
- Postman official documentation: https://learning.postman.com/docs/getting-started/
- Postman API documentation (using Node.js): https://www.postman.com/docs/v3/postman-collection
Remember, using the Postman CLI is a more convenient and versatile method for automating API testing and integration. Start experimenting with the commands and explore its full capabilities to enhance your API testing processes.