0
0
Postmantesting~10 mins

Creating mock servers in Postman - Test Execution Walkthrough

Choose your learning style9 modes available
Test Overview

This test checks if a Postman mock server is created successfully and responds with the expected mock data when a request is sent.

Test Code - Python requests + assert
Postman
import requests

# Step 1: Create a mock server via Postman API
mock_server_creation_url = "https://api.getpostman.com/mocks"
headers = {"X-Api-Key": "{{POSTMAN_API_KEY}}", "Content-Type": "application/json"}

mock_server_payload = {
    "mock": {
        "name": "Test Mock Server",
        "collection": {
            "uid": "{{COLLECTION_UID}}"
        }
    }
}

response_create = requests.post(mock_server_creation_url, json=mock_server_payload, headers=headers)
assert response_create.status_code == 200, f"Failed to create mock server: {response_create.text}"

mock_url = response_create.json()["mock"]["mockUrl"]

# Step 2: Send a GET request to the mock server
response_mock = requests.get(mock_url + "/test-endpoint")

# Step 3: Verify the response status and body
assert response_mock.status_code == 200, f"Unexpected status code: {response_mock.status_code}"
expected_body = {"message": "Hello from mock server!"}
assert response_mock.json() == expected_body, f"Response body mismatch: {response_mock.json()}"
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Send POST request to Postman API to create a mock serverPostman API receives request to create mock server with given collection and nameCheck response status code is 200 (OK)PASS
2Extract mock server URL from creation responseMock server URL is available for sending requests-PASS
3Send GET request to mock server endpoint '/test-endpoint'Mock server receives request and returns predefined mock responseVerify response status code is 200PASS
4Verify response JSON body matches expected mock dataResponse body contains {"message": "Hello from mock server!"}Assert response JSON equals expected bodyPASS
Failure Scenario
Failing Condition: Mock server creation fails or returns error status code
Execution Trace Quiz - 3 Questions
Test your understanding
What is the first step in creating a mock server in this test?
ASend a POST request to Postman API to create the mock server
BSend a GET request to the mock server endpoint
CVerify the response JSON body
DExtract the mock server URL from the response
Key Result
Always verify the mock server creation response before sending requests to ensure the mock environment is ready and valid.