0
0
Postmantesting~8 mins

JSON Schema validation in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - JSON Schema validation
Folder Structure
postman-project/
├── collections/
│   └── api-requests.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── tests/
│   └── json-schema-validation.test.js
├── scripts/
│   └── schema-definitions/
│       └── user-schema.json
├── reports/
│   └── test-report.html
└── postman.config.json
Test Framework Layers
  • Collections: Store API requests and organize them by feature or endpoint.
  • Environments: Define variables for different deployment stages (dev, staging, prod).
  • Tests: JavaScript test scripts that run after requests to validate responses, including JSON Schema validation.
  • Schema Definitions: JSON files defining expected response structures used for validation.
  • Reports: Generated test reports showing pass/fail results and details.
  • Configuration: Central config file for global settings like default environment and report options.
Configuration Patterns
  • Environment Variables: Use Postman environment files to store base URLs, API keys, and other sensitive data per environment.
  • Global Variables: For values shared across environments, like common headers or tokens.
  • Schema Versioning: Keep schema files versioned in scripts/schema-definitions/ to track changes and maintain backward compatibility.
  • Postman Config File: Use postman.config.json to define default environment, collection paths, and report output settings.
Test Reporting and CI/CD Integration
  • Newman CLI: Use Newman (Postman CLI) to run collections and output JSON or HTML reports.
  • Report Generation: Configure Newman to generate detailed HTML reports saved in reports/ folder.
  • CI/CD Integration: Integrate Newman runs into pipelines (GitHub Actions, Jenkins, GitLab CI) to automate tests on code changes.
  • Fail Fast: Configure tests to fail the pipeline immediately on JSON Schema validation errors.
  • Notifications: Send test results via email or messaging tools on failures or success.
Best Practices
  1. Separate Schema Files: Keep JSON Schemas in dedicated files for reusability and clarity.
  2. Validate Strictly: Use strict JSON Schema validation to catch unexpected response changes early.
  3. Use Environment Variables: Avoid hardcoding URLs or credentials; use environment variables for flexibility.
  4. Automate with Newman: Run tests automatically in CI/CD to ensure continuous validation.
  5. Clear Error Messages: Write test assertions that provide clear messages on schema validation failures.
Self Check

Where in this folder structure would you add a new JSON Schema file for validating the response of a new API endpoint called "orders"?

Key Result
Organize Postman tests with collections, environment configs, separate JSON schema files, and automate validation using Newman in CI/CD.