Mock Servers & Contract Testing
Postman Mock Servers simulate API endpoints, allowing you to develop and test your application without relying on a live API.
|
Create a mock server in Postman by defining the endpoint URL, request method, headers, and response body.
|
Use mock servers to test different scenarios, such as successful responses, error responses, and edge cases.
|
Verify interactions with the mock server using the Postman Console.
|
|
Create a Postman Collection that defines the expected request and response schemas for your API endpoints.
|
|
Write tests in Postman to validate that the actual responses from the API match the expected schemas.
|
Automate contract testing
|
Use the Postman Collection Runner or Newman to automate the execution of your contract tests.
|
Use libraries like tv4 or ajv within your Postman tests to validate the response body against a JSON schema.
|
Example:
const schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
};
pm.test("Validate schema", function () {
const jsonData = pm.response.json();
pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
|