0
0
Postmantesting~8 mins

Status code assertion in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Status code assertion
Folder Structure
PostmanCollectionProject/
├── collections/
│   └── UserAPI.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── tests/
│   └── status_code_assertion.test.js
├── scripts/
│   └── pre-request-scripts.js
├── reports/
│   └── test-report.html
└── postman.config.json
Test Framework Layers
  • Collections: Group of API requests organized by feature or service.
  • Environments: Variables for different deployment stages (dev, staging, prod).
  • Tests: JavaScript files or inline scripts in Postman to assert response status codes and data.
  • Scripts: Pre-request or setup scripts to prepare data or headers before requests.
  • Reports: Generated test reports showing pass/fail results of assertions.
  • Config: Configuration file to manage global settings like base URLs and authentication tokens.
Configuration Patterns
  • Environment Variables: Use environment files to store base URLs, API keys, and tokens for each environment.
  • Global Variables: For values shared across all environments, like common headers.
  • Collection Variables: Scoped to the collection for reusable data like user IDs.
  • Pre-request Scripts: Dynamically set or update variables before sending requests.
  • Config File: postman.config.json to define default environment and report settings.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections from command line with detailed reports.
  • Report Formats: Generate HTML, JSON, or JUnit XML reports for CI tools.
  • CI/CD Pipelines: Integrate Newman runs in Jenkins, GitHub Actions, GitLab CI to automate tests on code changes.
  • Fail Fast: Configure pipelines to stop on failed status code assertions to prevent faulty deployments.
  • Notifications: Send test results via email or chat tools on test completion.
Best Practices
  • Assert Exact Status Codes: Always check for the precise expected status code (e.g., 200, 404) to catch errors early.
  • Use Environment Variables: Avoid hardcoding URLs or tokens; use variables for flexibility.
  • Keep Tests Simple: One assertion per test script for clarity and easier debugging.
  • Organize Collections: Group related API requests logically for maintainability.
  • Automate Reporting: Use Newman with CI to get consistent, automated feedback on API health.
Self Check

Where in this folder structure would you add a new test script to verify the status code for a new API endpoint?

Answer: Add the test script inside the tests/ folder, for example as new_endpoint_status_code.test.js.
Key Result
Use Postman collections with environment variables and Newman CLI for automated status code assertions and reporting.