0
0
Postmantesting~8 mins

Mock vs stub comparison in Postman - Framework Approaches Compared

Choose your learning style9 modes available
Framework Mode - Mock vs stub comparison
Folder Structure for Postman Test Automation
  PostmanProject/
  ├── collections/
  │   ├── UserAPI.postman_collection.json
  │   ├── ProductAPI.postman_collection.json
  ├── environments/
  │   ├── dev.postman_environment.json
  │   ├── prod.postman_environment.json
  ├── mocks/
  │   ├── UserAPIMock.postman_mock.json
  │   ├── ProductAPIMock.postman_mock.json
  ├── tests/
  │   ├── user_api_tests.js
  │   ├── product_api_tests.js
  ├── scripts/
  │   ├── pre_request_scripts.js
  │   ├── test_scripts.js
  ├── README.md
  
Test Framework Layers in Postman
  • Collections: Group of API requests organized by feature or service.
  • Mocks: Simulated API endpoints that return predefined responses to test client behavior without real backend.
  • Tests: Scripts written in JavaScript to validate API responses and behavior.
  • Environments: Variables and settings for different deployment stages (dev, prod).
  • Scripts: Reusable JavaScript code for pre-request setup and test assertions.
Configuration Patterns in Postman
  • Environment Variables: Store URLs, tokens, and credentials for different environments.
  • Global Variables: Shared values across collections and requests.
  • Mock Servers: Created in Postman UI or via API to simulate backend responses for testing.
  • Collection Variables: Scoped variables for specific collections to isolate test data.
Test Reporting and CI/CD Integration
  • Newman CLI: Run Postman collections from command line with detailed reports.
  • Reporters: Use built-in reporters (JSON, HTML) or third-party tools for readable test results.
  • CI/CD Pipelines: Integrate Newman runs in Jenkins, GitHub Actions, or GitLab CI for automated testing.
  • Mock Server Logs: Monitor mock server requests and responses for debugging.
Best Practices for Mock vs Stub in Postman Framework
  1. Use Mocks to Simulate Full API Behavior: Create mock servers in Postman to simulate real API endpoints with dynamic responses.
  2. Use Stubs for Simple Fixed Responses: Use static responses in mocks or scripts to test specific scenarios quickly.
  3. Keep Mocks and Stubs Organized: Store mock definitions in the mocks/ folder and link them clearly to collections.
  4. Isolate Tests from Real Backend: Use mocks to avoid dependency on backend availability during testing.
  5. Maintain Environment Variables: Switch easily between real API and mock endpoints using environment variables.
Self Check Question

Where would you add a new mock server definition for a Payment API in this Postman framework structure?

Key Result
Organize Postman tests with collections, mocks, environments, and scripts for clear API testing and simulation.