0
0
Postmantesting~8 mins

Condition block in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Condition block
Folder Structure
postman-project/
├── collections/
│   └── MyCollection.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── tests/
│   ├── condition-block-tests.js
│   └── utils.js
├── scripts/
│   └── condition-block.js
├── reports/
│   └── test-report.html
└── postman.config.json
Test Framework Layers
  • Collections: Group of API requests organized logically.
  • Environments: Variables for different deployment stages (dev, staging, prod).
  • Scripts: JavaScript files containing reusable condition blocks and helper functions.
  • Tests: Test scripts that use condition blocks to validate API responses.
  • Reports: Generated test reports after running collections.
  • Config: Configuration file for global settings like default environment and report options.
Configuration Patterns

Use postman.config.json to define:

  • Default environment (e.g., dev, staging, prod).
  • API base URLs per environment.
  • Authentication tokens or credentials stored securely in environment variables.
  • Settings for test run options like timeout and retries.

Example snippet from postman.config.json:

{
  "defaultEnvironment": "dev",
  "environments": {
    "dev": "environments/dev.postman_environment.json",
    "staging": "environments/staging.postman_environment.json",
    "prod": "environments/prod.postman_environment.json"
  },
  "report": {
    "format": "html",
    "output": "reports/test-report.html"
  }
}
Test Reporting and CI/CD Integration
  • Use Newman (Postman CLI) to run collections and generate reports.
  • Generate HTML or JSON reports for easy review.
  • Integrate Newman commands into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Fail the build if condition block tests fail to ensure quality gates.

Example Newman command:

newman run collections/MyCollection.postman_collection.json \
  -e environments/dev.postman_environment.json \
  --reporters cli,html \
  --reporter-html-export reports/test-report.html
Best Practices for Condition Block Framework Design
  • Reusable Condition Blocks: Write condition blocks as reusable JavaScript functions in scripts/condition-block.js to keep tests DRY (Don't Repeat Yourself).
  • Environment Variables: Use environment variables for dynamic data like tokens and URLs to avoid hardcoding.
  • Clear Assertions: Use clear and simple assertions inside condition blocks to check API response status, body, and headers.
  • Modular Tests: Organize tests in separate files under tests/ for maintainability.
  • Consistent Naming: Name collections, environments, and test files clearly to reflect their purpose.
Self Check

Where in this folder structure would you add a new condition block function to check if a response contains a specific JSON key?

Key Result
Organize Postman tests with reusable condition blocks in scripts, environment configs, and structured collections for maintainable API testing.