0
0
Postmantesting~8 mins

Variable assignment in flows in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Variable assignment in flows
Folder Structure
postman-project/
├── collections/
│   └── my-api-collection.json       # Postman collection with requests and scripts
├── environments/
│   ├── dev.postman_environment.json # Environment variables for Dev
│   ├── qa.postman_environment.json  # Environment variables for QA
│   └── prod.postman_environment.json # Environment variables for Prod
├── globals/
│   └── globals.postman_globals.json # Global variables
├── tests/
│   └── scripts/                     # Custom JS scripts for variable assignment and helpers
├── README.md
└── postman.config.json              # Optional config for Postman CLI or Newman
Test Framework Layers
  • Collections: Define API requests and flows. Each request can have pre-request and test scripts to assign variables.
  • Environments: Store environment-specific variables (e.g., base URLs, tokens). Variables can be updated dynamically during flows.
  • Globals: Variables accessible across all collections and environments for sharing data.
  • Scripts: JavaScript code in pre-request or test scripts to assign, update, or clear variables using Postman API (pm.variables, pm.environment, pm.globals).
  • Tests: Assertions and variable assignments happen here to validate responses and set variables for next requests.
Configuration Patterns
  • Environment Files: Use separate environment JSON files for Dev, QA, Prod to manage variables like URLs and credentials.
  • Variable Scopes: Use local (request-level), environment, and global scopes carefully to control variable visibility and lifetime.
  • Dynamic Variable Assignment: Assign variables in test scripts using pm.environment.set('varName', value) or pm.variables.set('varName', value) to pass data between requests.
  • Secrets Management: Store sensitive data in environment variables and avoid hardcoding in scripts or collections.
  • Version Control: Keep collections and environment files under version control (e.g., Git) for collaboration and history.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections in CI pipelines using Newman. Supports JSON and HTML reports.
  • Reporters: Use built-in reporters (CLI, JSON, HTML) or third-party reporters for detailed test results.
  • CI/CD Integration: Integrate Newman runs in Jenkins, GitHub Actions, GitLab CI to automate tests on code changes.
  • Variable Injection: Pass environment variables dynamically in CI to run tests against different environments.
  • Failure Alerts: Configure notifications on test failures to keep the team informed.
Best Practices
  • Use Environment Variables for Dynamic Data: Assign variables in test scripts to pass data between requests instead of hardcoding.
  • Clear Variables When Not Needed: Remove or reset variables to avoid stale data affecting later tests.
  • Keep Scripts Simple and Readable: Write clear JavaScript in pre-request and test scripts for variable assignment.
  • Use Descriptive Variable Names: Helps understand what data is stored and passed in flows.
  • Test Variable Assignments Independently: Validate that variables are assigned correctly before using them in subsequent requests.
Self Check

Where in this folder structure would you add a new script to assign a variable after receiving an authentication token?

Key Result
Use environment and global variables with scripts in Postman collections to assign and manage variables dynamically in API test flows.