0
0
Postmantesting~10 mins

Mock vs stub comparison in Postman - Test Execution Compared

Choose your learning style9 modes available
Test Overview

This test compares the behavior of a mock server and a stub in Postman. It verifies that the mock server returns the predefined response and the stub simulates a simple response for a dependent API call.

Test Code - PyTest
Postman
import requests

# URL of the Postman mock server
mock_url = "https://abc123.mock.pstmn.io/resource"

# URL of the stub endpoint (simulated with a simple local server or mock)
stub_url = "https://stub-server.local/api/v1/dependency"

# Test the mock server response
mock_response = requests.get(mock_url)
assert mock_response.status_code == 200, f"Expected 200 but got {mock_response.status_code}"
assert mock_response.json().get('message') == 'Mock response', "Mock response message mismatch"

# Test the stub response
stub_response = requests.get(stub_url)
assert stub_response.status_code == 200, f"Expected 200 but got {stub_response.status_code}"
assert stub_response.text == 'Stub response', "Stub response content mismatch"
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest environment ready with mock server and stub endpoint URLs configured-PASS
2Send GET request to Postman mock server URLMock server is running and ready to respondCheck if status code is 200PASS
3Verify mock server response JSON contains 'message' with value 'Mock response'Response JSON received from mock serverAssert response JSON 'message' == 'Mock response'PASS
4Send GET request to stub endpoint URLStub endpoint is running and ready to respondCheck if status code is 200PASS
5Verify stub response text equals 'Stub response'Response text received from stub endpointAssert response text == 'Stub response'PASS
6Test ends successfully with all assertions passedBoth mock and stub responses verified-PASS
Failure Scenario
Failing Condition: Mock server or stub endpoint returns unexpected status code or response content
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the Postman mock server?
AIt modifies the request before sending
BIt simulates a slow network response
CIt returns a predefined JSON response with status 200
DIt stores data permanently
Key Result
Always verify that mock servers and stubs return the exact expected responses to ensure your tests simulate real API behavior correctly.