0
0
PostmanHow-ToBeginner ยท 3 min read

How to Use Environment File with Newman in Postman

To use an environment file with newman, run the command newman run collection.json -e environment.json. The -e flag specifies the environment file that contains variables for your Postman collection.
๐Ÿ“

Syntax

The basic syntax to run a Postman collection with an environment file using Newman is:

  • newman run <collection-file> -e <environment-file>
  • collection-file: The JSON file exported from Postman representing your API tests.
  • environment-file: The JSON file exported from Postman containing environment variables.
bash
newman run collection.json -e environment.json
๐Ÿ’ป

Example

This example shows how to run a Postman collection named api-tests.json with an environment file named dev-environment.json. This allows the collection to use variables defined in the environment during the test run.

bash
newman run api-tests.json -e dev-environment.json
Output
newman v5.3.1 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ api-tests โ”‚ โ”‚ 1 request, 1 iteration โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†’ GET https://api.example.com/users 200 OK โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ iterations: 1 โ”‚ โ”‚ requests: 1 โ”‚ โ”‚ assertions: 1 โ”‚ โ”‚ failures: 0 โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โš ๏ธ

Common Pitfalls

  • Wrong file path: Ensure the environment file path is correct relative to where you run the command.
  • Incorrect file format: The environment file must be a valid Postman environment JSON export.
  • Missing variables: Variables used in the collection must exist in the environment file to avoid errors.
  • Using deprecated flags: Always use -e for environment files; older flags may not work.
bash
Wrong:
newman run api-tests.json --environment dev-environment.json

Right:
newman run api-tests.json -e dev-environment.json
๐Ÿ“Š

Quick Reference

FlagDescription
-e, --environment Specify the Postman environment JSON file to use
-c, --collection Specify the Postman collection JSON file to run
-r, --reporters Choose reporters like cli, json, html
--iteration-count Number of times to run the collection
โœ…

Key Takeaways

Use the -e flag with newman to specify the environment file for variable support.
Ensure environment and collection files are valid JSON exports from Postman.
Check file paths carefully to avoid file not found errors.
Variables in the collection must be defined in the environment file to work correctly.
Use newman CLI commands exactly as documented to avoid deprecated or incorrect flags.