The Postman Tests Tab
Validations in Postman are written in the Tests tab using JavaScript and the Chai Assertion Library.
pm.* methods evaluate the response after each request.
Status Code Assertion
pm.response.to.have.status(200);
Value Assertion
pm.expect(jsonData.id).to.eql(101);
Named Test Example
Wrap assertions inside pm.test().
pm.test("name", () => {
// Assertions
});
These become named validations that pass or fail during Collection Runner and Newman execution.
Core Validations
Practice everyday response validation exercises.
Exercise 1: Validate Status Codes
Validate success and failure status codes.
Exercise 2: Validate Response Time
Validate response-time SLA.
Example:
- Response Time < 2000 ms
Exercise 3: Validate Mandatory Fields
Verify mandatory response fields.
Exercise 4: Validate Response Headers
Validate header values.
Exercise 5: Validate Error Messages
Verify error-message consistency.
Exercise 6: Validate Null vs Empty
Differentiate between:
- Null
- Empty Values
Exercise 7: Validate Data Types
Validate:
- Numeric Values
- String Values
Exercise 8: Validate Nested JSON
Verify nested JSON structures.
Exercise 9: Validate Arrays
Validate array objects.
Exercise 10: Validate Conditional Responses
Verify conditional API responses.
RESTful Booker Assertion Set
Practice:
- Status Code Validation
- Response-Time Validation
- Booking ID Validation
- Nested JSON Validation
- Error Message Validation
Advanced Response Validations
Practice deeper response validation techniques.
Exercise 1: Validate Response Schema
Validate the JSON Schema or API contract.
Exercise 2: Validate Contract Changes
Detect API contract changes.
Exercise 3: Validate Dynamic Array Size
Verify array sizes dynamically.
Exercise 4: Validate Sorted Responses
Validate response sorting.
Exercise 5: Validate Pagination Metadata
Validate:
- Page Number
- Total Count
Example:
- ReqRes
Exercise 6: Validate Timestamps
Verify timestamps and date formats.
Exercise 7: Validate Enum Values
Validate enum values returned by the API.
Exercise 8: Validate Duplicate Data
Verify duplicate records are not returned.
Exercise 9: Validate Data Consistency
Validate data consistency across multiple APIs.
Exercise 10: Validate Backward Compatibility
Verify backward compatibility after API changes.
Purpose
These validations verify not only that the API returns a successful response, but also that the returned data is structurally and semantically correct.
Demo-API Mapping
Core Validations
Practice using:
- RESTful Booker
Exercises include:
- Status Code Validation
- Response-Time Validation
- Booking ID Validation
- Nested JSON Validation
- Error Message Validation
Pagination Validation
Practice using:
- ReqRes
Validate:
- Page Number
- Total Count
Schema Validation
Practice using:
- Swagger Petstore
Validate:
- JSON Schema
- API Contract
Data Consistency
Practice using:
- FakeStore API
Examples include:
- Product Price > 0
- Cart Total
FAQs
Where Do You Write Assertions in Postman?
Write assertions in the Tests tab using JavaScript and the Chai Assertion Library.
Examples:
pm.response.to.have.status(200);
pm.expect(jsonData.id).to.eql(101);
How Do You Validate Response Time?
Validate response time against the expected SLA.
Example:
pm.expect(pm.response.responseTime).to.be.below(2000);
A common validation is:
- Response Time < 2000 ms
How Do You Validate Nested JSON?
Parse the response using:
pm.response.json();
Then validate nested values.
Example:
pm.expect(jsonData.parent.child).to.eql(...);
How Do You Validate a Response Schema?
Perform JSON Schema or contract validation inside the Tests tab.
Validate:
- Structure
- Required Fields
- Data Types
Which Advanced Validations Are Most Important?
Practice:
- Schema Validation
- Contract Validation
- Dynamic Array Size
- Sorted Responses
- Pagination Metadata
- Timestamp Validation
- Enum Validation
- Duplicate Data Validation
- Cross-API Data Consistency