0
0
Postmantesting~8 mins

JSON body in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - JSON body
Folder Structure
postman-project/
├── collections/
│   └── user-api.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   └── prod.postman_environment.json
├── tests/
│   └── user-api-tests.js
├── scripts/
│   └── pre-request-scripts.js
├── data/
│   └── user-data.json
└── README.md
Test Framework Layers
  • Collections: Group of API requests with JSON bodies defined for POST, PUT, PATCH methods.
  • Environments: Variables for base URLs, tokens, and other environment-specific data.
  • Tests: Scripts that run assertions on API responses to validate JSON body correctness.
  • Pre-request Scripts: Scripts to prepare or modify JSON bodies dynamically before sending requests.
  • Data Files: External JSON files used for data-driven testing of request bodies.
Configuration Patterns
  • Environment Variables: Store base URLs, authentication tokens, and other dynamic values to reuse in JSON bodies.
  • Global Variables: For values shared across collections, like common IDs or keys.
  • Data-driven Testing: Use external JSON files to feed multiple JSON bodies into requests for varied test cases.
  • Pre-request Scripts: Modify or generate JSON body content dynamically based on environment or test data.
Test Reporting and CI/CD Integration
  • Use Postman's built-in test results tab to view pass/fail status of JSON body validations.
  • Integrate with Newman CLI to run collections in CI/CD pipelines and generate JSON or HTML reports.
  • Use reporters like mochawesome or custom reporters with Newman for detailed JSON body test reports.
  • Configure CI tools (GitHub Actions, Jenkins) to run Postman tests automatically on code changes.
Best Practices
  • Keep JSON bodies clean and readable: Use proper indentation and consistent formatting.
  • Use variables for dynamic values: Avoid hardcoding values inside JSON bodies; use environment or global variables.
  • Validate JSON schema: Use JSON schema validation in tests to ensure the body structure is correct.
  • Data-driven tests: Separate test data from requests to easily test multiple scenarios.
  • Use pre-request scripts wisely: Generate or modify JSON bodies dynamically to keep tests flexible.
Self Check

Where in this folder structure would you add a new JSON data file to test different user creation scenarios?

Key Result
Organize Postman tests with collections, environments, scripts, and data files to manage JSON bodies effectively.